Changeset 536 for trunk/src


Ignore:
Timestamp:
Oct 24, 2006, 7:36:57 AM (18 years ago)
Author:
dkearney
Message:

fixed memory errors found by valgrind in the Rappture Dictionary module, this change probably propagated through the fortran and matlab/octave bindings too.

Location:
trunk/src/core
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/RpBindingsDict.cc

    r135 r536  
    6767
    6868
    69     RpLibrary* retVal = NULL;
    70 
    71     retVal = *(ObjDict_Lib.find(objKey).getValue());
    72 
    73     if (retVal == *(ObjDict_Lib.getNullEntry().getValue())) {
    74         retVal = NULL;
    75     }
    76 
    77    return retVal;
     69    RpDictEntry DICT_TEMPLATE_L* libEntry = &(ObjDict_Lib.getNullEntry());
     70    RpDictEntry DICT_TEMPLATE_L* nullEntry = &(ObjDict_Lib.getNullEntry());
     71
     72    libEntry = &(ObjDict_Lib.find(objKey));
     73
     74
     75    if ( (!libEntry->isValid()) || (libEntry == nullEntry) ) {
     76        return NULL;
     77    }
     78
     79   return *(libEntry->getValue());
    7880
    7981}
     
    158160getObject_UnitsStr(int objKey) {
    159161
     162    /*
    160163    std::string basisName = *(ObjDictUnits.find(objKey).getValue());
    161164
     
    165168    }
    166169
    167    return RpUnits::find(basisName);
     170    return RpUnits::find(basisName);
     171    */
     172
     173    RpDictEntry DICT_TEMPLATE_U* unitEntry = &(ObjDictUnits.getNullEntry());
     174    RpDictEntry DICT_TEMPLATE_U* nullEntry = &(ObjDictUnits.getNullEntry());
     175
     176    unitEntry = &(ObjDictUnits.find(objKey));
     177
     178
     179    if ( (!unitEntry->isValid()) || (unitEntry == nullEntry) ) {
     180        return NULL;
     181    }
     182
     183    return RpUnits::find(*(unitEntry->getValue()));
    168184
    169185}
  • trunk/src/core/RpDict.h

    r511 r536  
    9898        // const void* setValue(const void* value);
    9999        const ValType* setValue(const ValType& value);
     100        bool isValid() const;
    100101
    101102        // erases this entry from its table
     
    108109        friend class RpDictIterator<KeyType,ValType>;
    109110
    110         // no_arg constructor
    111         // use the key and clientData's default [no-arg] constructor
    112         RpDictEntry()
    113            : nextPtr (NULL),
    114              tablePtr (NULL),
    115              hash (0) // ,
    116              // clientData (),
    117              // key ()
    118         {
    119         }
    120 
    121111        // one-arg constructor
    122112        // maybe get rid of this one?
     113        // use the clientData's default [no-arg] constructor
     114        /*
    123115        RpDictEntry(KeyType newKey)
    124            : nextPtr    (NULL), 
    125              tablePtr   (NULL), 
    126              hash       (0), 
    127              clientData (NULL),
     116           : nextPtr    (NULL),
     117             tablePtr   (NULL),
     118             hash       (0),
     119             // clientData (NULL),
    128120             key        (newKey)
    129121        {
    130122        }
     123        */
    131124
    132125        // two-arg constructor
    133126        RpDictEntry(KeyType newKey, ValType newVal)
    134            : nextPtr    (NULL), 
    135              tablePtr   (NULL), 
    136              hash       (0), 
     127           : nextPtr    (NULL),
     128             tablePtr   (NULL),
     129             hash       (0),
    137130             clientData (newVal),
    138              key        (newKey)
     131             key        (newKey),
     132             valid      (&clientData)
    139133        {
    140134        }
     
    142136/*
    143137        RpDictEntry(KeyType* newKey, ValType* newVal)
    144            : nextPtr    (NULL), 
    145              tablePtr   (NULL), 
    146              hash       (0), 
     138           : nextPtr    (NULL),
     139             tablePtr   (NULL),
     140             hash       (0),
    147141             clientData (*newVal),
    148142             key        (*newKey)
     
    151145
    152146        RpDictEntry(KeyType newKey, RpDict* table)
    153            : nextPtr    (NULL), 
    154              tablePtr   (table), 
    155              hash       (0), 
     147           : nextPtr    (NULL),
     148             tablePtr   (table),
     149             hash       (0),
    156150             // clientData (NULL),
    157151             key        (newKey)
     
    165159            tablePtr    = entry.tablePtr;
    166160            hash        = entry.hash;
    167             clientData  = (ValType) entry.getValue();
    168             key         = (KeyType) entry.getKey();
     161
     162            if (entry.valid != NULL) {
     163                clientData  = (ValType) entry.getValue();
     164                key         = (KeyType) entry.getKey();
     165                valid       = &clientData;
     166            }
     167            else {
     168                valid = NULL;
     169            }
     170
    169171        }
    170172
     
    177179                                     * chain. */
    178180
    179         RpDict<KeyType,ValType> 
    180             *tablePtr;              /* Pointer to table containing entry. */
     181        RpDict<KeyType,ValType>*
     182            tablePtr;              /* Pointer to table containing entry. */
    181183
    182184        unsigned int hash;          /* Hash value. */
    183185
    184186        ValType clientData;        /* Application stores something here
    185                                      * with Tcl_SetHashValue. */
     187                                    * with Tcl_SetHashValue. */
    186188
    187189        KeyType key;               /* entry key */
    188190
    189 
    190 
     191        ValType* valid;            /* is this a valid object */
     192
     193
     194        // no_arg constructor
     195        //
     196        RpDictEntry()
     197           : nextPtr (NULL),
     198             tablePtr (NULL),
     199             hash (0),
     200             valid (NULL)
     201             // clientData (),
     202             // key ()
     203        {
     204        }
    191205
    192206};
     
    207221        // returns !0 on failure (object cannot be inserted or dne)
    208222        //
    209         /*virtual*/ RpDict<KeyType,ValType>& 
    210                         set(    KeyType& key, 
    211                                 ValType& value, 
    212                                 int *newPtr=NULL );   
     223        /*virtual*/ RpDict<KeyType,ValType>&
     224                        set(    KeyType& key,
     225                                ValType& value,
     226                                int *newPtr=NULL );
    213227
    214228        // find an RpUnits object that should exist in RpUnitsTable
     
    217231                        find( KeyType& key );
    218232
    219         /*virtual*/ RpDictEntry<KeyType,ValType>& operator[]( KeyType& key) 
    220         { 
    221             return find(key); 
     233        /*virtual*/ RpDictEntry<KeyType,ValType>& operator[]( KeyType& key)
     234        {
     235            return find(key);
    222236        }
    223237
     
    236250
    237251        // default constructor
    238         RpDict () 
     252        RpDict ()
    239253            : SMALL_RP_DICT_SIZE(4),
    240254              REBUILD_MULTIPLIER(3),
     
    245259              downShift(28),
    246260              mask(3)
    247         { 
     261        {
    248262
    249263            staticBuckets[0] = staticBuckets[1] = 0;
     
    253267            nullEntry = new RpDictEntry<KeyType,ValType>();
    254268
     269            assert(nullEntry != NULL);
     270
    255271            // std::cout << "inside RpDict Constructor" << std::endl;
    256272
     
    264280
    265281        // destructor
    266         /*virtual*/ ~RpDict() 
     282        /*virtual*/ ~RpDict()
    267283        {
    268284            // probably need to delete all the entries as well
     
    366382                                int* newPtr)
    367383{
    368     RpDictEntry<KeyType,ValType> *hPtr;
    369     unsigned int hash;
    370     int index;
     384    RpDictEntry<KeyType,ValType> *hPtr = NULL;
     385    unsigned int hash = 0;
     386    int index = 0;
    371387
    372388    assert(&key);
     
    461477
    462478template <typename KeyType, typename ValType>
    463 RpDictEntry<KeyType,ValType>& 
     479RpDictEntry<KeyType,ValType>&
    464480RpDict<KeyType,ValType>::find(KeyType& key)
    465481{
    466     RpDictEntry<KeyType,ValType> *hPtr;
    467     unsigned int hash;
    468     int index;
     482    RpDictEntry<KeyType,ValType> *hPtr = NULL;
     483    unsigned int hash = 0;
     484    int index = 0;
    469485
    470486    assert(&key);
     
    513529 * Side Effects:
    514530 *  moves iterator to the beginning of the hash table.
    515  * 
     531 *
    516532 *
    517533 *************************************************************************/
     
    567583RpDictIterator<KeyType,ValType>::next()
    568584{
    569     RpDictEntry<KeyType,ValType>* hPtr;
     585    RpDictEntry<KeyType,ValType>* hPtr = NULL;
    570586
    571587    while (srchNextEntryPtr == NULL) {
     
    599615RpDict<KeyType,ValType>::clear()
    600616{
    601     RpDictEntry<KeyType,ValType> *hPtr;
     617    RpDictEntry<KeyType,ValType> *hPtr = NULL;
    602618    RpDictIterator<KeyType,ValType> iter((RpDict&)*this);
    603619
     
    614630/**************************************************************************
    615631 *
    616  * RpDict & getNullEntry()
     632 * RpDictEntry & getNullEntry()
    617633 *  get the nullEntry hash entry for initialization of references
    618634 *
     
    657673RpDictEntry<KeyType,ValType>::erase()
    658674{
    659     RpDictEntry<KeyType,ValType> *prevPtr;
    660     RpDictEntry<KeyType,ValType> **bucketPtr;
     675    RpDictEntry<KeyType,ValType> *prevPtr = NULL;
     676    RpDictEntry<KeyType,ValType> **bucketPtr = NULL;
    661677    int index = 0;
    662678
     
    704720    // clientData = NULL;
    705721    // key = NULL;
     722    valid = NULL;
    706723
    707724    // delete the object.
     
    740757 *
    741758 *  retrieve the value of the current object
     759 *  it is the caller responsibility to check isValid() to see if the
     760 *  object actually holds a valid value.
    742761 *
    743762 * Results:
     
    778797{
    779798    clientData = value;
     799    valid = &clientData;
    780800    return (const ValType*) &clientData;
    781801}
     
    793813}
    794814
     815/*
     816 *----------------------------------------------------------------------
     817 *
     818 * bool RpDictEntry::isValid()
     819 *
     820 *  is this a valid object, return true or false
     821 *
     822 * Results:
     823 *  tells the user if the object is valid true or false
     824 *
     825 * Side effects:
     826 *  None.
     827 *
     828 *----------------------------------------------------------------------
     829 */
     830
     831template <typename KeyType, typename ValType>
     832bool
     833RpDictEntry<KeyType,ValType>::isValid() const
     834{
     835    if (valid) {
     836        return true;
     837    }
     838    return false;
     839}
     840
    795841
    796842/*************************************************************************/
     
    824870{
    825871    int oldSize=0, count=0, index=0;
    826     RpDictEntry<KeyType,ValType> **oldBuckets;
    827     RpDictEntry<KeyType,ValType> **oldChainPtr, **newChainPtr;
    828     RpDictEntry<KeyType,ValType> *hPtr;
    829 
    830     void *key;
     872    RpDictEntry<KeyType,ValType> **oldBuckets = NULL;
     873    RpDictEntry<KeyType,ValType> **oldChainPtr = NULL, **newChainPtr = NULL;
     874    RpDictEntry<KeyType,ValType> *hPtr = NULL;
     875
     876    // void *key = NULL;
    831877
    832878    oldSize = numBuckets;
     
    863909            *oldChainPtr = hPtr->nextPtr;
    864910
    865             key = (void *) hPtr->getKey();
     911            // key = (void *) hPtr->getKey();
    866912
    867913            index = randomIndex(hPtr->hash);
     
    905951    const char *stopAddr = (const char *) keyPtr + sizeof(&keyPtr) - 1 ;
    906952    const char *str = (const char *) keyPtr;
    907     unsigned int result;
     953    unsigned int result = 0;
    908954    int c;
    909955
     
    942988{
    943989    const char *str = (const char *) (keyPtr->c_str());
    944     unsigned int result;
    945     int c;
     990    unsigned int result = 0;
     991    int c = 0;
    946992
    947993    result = 0;
     
    9641010{
    9651011    const char *str = (const char *) (keyPtr);
    966     unsigned int result;
    967     int c;
     1012    unsigned int result = 0;
     1013    int c = 0;
    9681014
    9691015    result = 0;
  • trunk/src/core/RpUnits.cc

    r534 r536  
    680680
    681681    // dict pointer
     682    /*
    682683    const RpUnits* unitEntry = NULL;
     684    const RpUnits* nullEntry = NULL;
     685    */
     686    RpDictEntry<std::string,RpUnits*>* unitEntry = &(dict->getNullEntry());
     687    RpDictEntry<std::string,RpUnits*>* nullEntry = &(dict->getNullEntry());
    683688    double exponent = 1;
    684689    int idx = 0;
     
    693698    }
    694699
     700    /*
    695701    unitEntry = *(dict->find(key).getValue());
     702    nullEntry = *(dict->getNullEntry().getValue());
     703    */
     704
     705    unitEntry = &(dict->find(key));
    696706
    697707    // dict pointer
    698     if (unitEntry == *(dict->getNullEntry().getValue()) ) {
    699         unitEntry = NULL;
    700     }
    701 
    702     return unitEntry;
     708    if ( (!unitEntry->isValid()) || (unitEntry == nullEntry) ) {
     709        // unitEntry = NULL;
     710        return NULL;
     711    }
     712
     713    return *(unitEntry->getValue());
    703714}
    704715
  • trunk/src/core/RpUnits.h

    r534 r536  
    649649        static const RpUnits* grabUnits (std::string inStr, int* offset);
    650650        static int negateListExponents(RpUnitsList& unitsList);
    651         static int RpUnits::printList(RpUnitsList& unitsList);
    652 
    653         static int RpUnits::compareListEntryBasis( RpUnitsList& fromList,
    654                                                    RpUnitsListIter& fromIter,
    655                                                    RpUnitsListIter& toIter);
    656 
    657         static int RpUnits::compareListEntrySearch( RpUnitsList& fromList,
    658                                                     RpUnitsListIter& fromIter,
    659                                                     RpUnitsListIter& toIter);
    660 
    661         void RpUnits::connectConversion(conversion* conv) const;
    662         void RpUnits::connectIncarnation(const RpUnits* unit) const;
     651        static int printList(RpUnitsList& unitsList);
     652
     653        static int compareListEntryBasis( RpUnitsList& fromList,
     654                                          RpUnitsListIter& fromIter,
     655                                          RpUnitsListIter& toIter);
     656
     657        static int compareListEntrySearch( RpUnitsList& fromList,
     658                                           RpUnitsListIter& fromIter,
     659                                           RpUnitsListIter& toIter);
     660
     661        void connectConversion(conversion* conv) const;
     662        void connectIncarnation(const RpUnits* unit) const;
    663663
    664664        // return the conversion object that will convert
Note: See TracChangeset for help on using the changeset viewer.