GeFree() crashing Cinema 4D?



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 19/03/2008 at 11:43, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   9+ 
    Platform:   Windows  ; Mac  ;  Mac OSX  ; 
    Language(s) :     C++  ;

    ---------
    Howdy,

    Here's the code snippet from TagData::Message() :

    > \> case MSG_TRANSLATE_POINTS: \> { \>      TranslationMaps \*tMap = (TranslationMaps\* )data; \>      if(tMap) \>      { \>           if(tMap->m_oPointCount != tMap->m_nPointCount) \>           { \>                // Allocate temporary remapping storage array                \>                LONG \*remapIndex = (LONG\* )GeAlloc(sizeof(LONG)\*tMap->m_nPointCount); \>                 \>                // Initialize remapping indices \>                for(i=0; i<tMap->m_nPointCount; i++) \>                { \>                     remapIndex[i] = i; \>                } \>                 \>                // Remap changed indices \>                for(i=0;i<tMap->m_mPointCount;i++) \>                { \>                     if(tMap->m_pPointMap[i].nIndex != tMap->m_pPointMap[i].oIndex) \>                     { \>                          remapIndex[tMap->m_pPointMap[i].nIndex] = tMap->m_pPointMap[i].oIndex; \>                     } \>                } \>                                          \>                // Print remapped indices to console                \>                for(i=0; i<tMap->m_nPointCount; i++) \>                { \>                     GePrint("remapIndex["+LongToString(i)+"] = "+LongToString(remapIndex[i])); \>                } \>                 \>                // free temporary remapping storage array \>                if(remapIndex) GeFree(remapIndex); \>           } \>      } \>      else GePrint("no translation map data"); \>      break; \> } \>

    The debugger shows a crash somewhere in Cinema 4D (don't know where because it's assembly code), but when I step backwards, the last function call from my code was the GeFree() function.

    if I comment this line out:

    > \> //if(remapIndex) GeFree(remapIndex); \>

    ... it doesn't crash and prints the information to the console, but now the memory for the storage array hasn't been freed.

    Why is the above code crashing? I've used GeAlloc()/GeFree() before for other temporary storage arrays with no problems at all.

    Adios,
    Cactus Dan



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 19/03/2008 at 12:04, xxxxxxxx wrote:

    Sounds to me like you are filling memory that is not available or you are accesing memory that is not existant or c4d accesses memory based on your filled data that is not existant. This sometimes leads to crashes later in the pipeline. And as this happens with memory in your case, I highly assume you are exceeding the arrays limits somewhere.

    I haven´t worked with translationmaps or the modeling lib much yet, but could it be that the old point count is higher than the new point count? And if so, when remapping the indices, couldn´t then an old index be higher than the actual point count of the new translation map and therefore an access to that index by c4d will crash as the index is not there any longer?

    Just a spontaneous idea when looking at the code.

    HTH
    Samir



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 19/03/2008 at 12:15, xxxxxxxx wrote:

    Howdy,

    Well, no, the remapping is correct, because if I comment out the GeFree() line, it prints the correct information to the console. I'm testing it on a simple cube and the console prints this if I delete point 0:
    remapIndex[0] = 7
    remapIndex[1] = 1
    remapIndex[2] = 2
    remapIndex[3] = 3
    remapIndex[4] = 4
    remapIndex[5] = 5
    remapIndex[6] = 6

    Points 1 through 6 didn't change, but since point 0 was deleted, point 7 was remapped to point 0 in the array.

    But with calling GeFree() to free the allocated memory, it crashes. :o(

    Adios,
    Cactus Dan



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 19/03/2008 at 13:01, xxxxxxxx wrote:

    If you are certain that it is the GeFree(remapIndex), I would do two things to verify what's happening:

    // 1. Check the count:
    if (tMap->m_nPointCount == 0) break; // (or something)
    // Allocate temporary remapping storage array              
    LONG *remapIndex = (LONG* )GeAlloc(sizeof(LONG)*tMap->m_nPointCount);
    // 2. Check the allocation:
    if (!remapIndex) break; //(or something)

    I've had unchecked 0 counts used for array allocation cause me grief in the past. Also, you really don't have to check remapIndex before calling GeFree(). As noted in the docs, if remapIndex is NULL, nothing happens.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 19/03/2008 at 13:11, xxxxxxxx wrote:

    Howdy,

    Hmmmm, yeah something must be wrong with the count, because if I do something simple like this:

    > \> Bool LookAtCamera::Message(GeListNode \*node, LONG type, void \*data) \> { \>      LONG i; \>       \>      switch (type) \>      { \>           case MSG_TRANSLATE_POINTS: \>           { \>                // Allocate temporary array                \>                LONG \*temp = (LONG\* )GeAlloc(sizeof(LONG)\*8); \>                // Initialize array values \>                for(i=0; i<8; i++) \>                { \>                     temp[i] = i; \>                } \>                for(i=0; i<8; i++) \>                { \>                     GePrint("temp["+LongToString(i)+"] = "+LongToString(temp[i])); \>                } \>                if(temp) GeFree(temp); \>                break; \>           } \>      } \>       \>      return TRUE; \> } \>

    it doesn't crash.

    Adios,
    Cactus Dan



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 19/03/2008 at 13:53, xxxxxxxx wrote:

    Howdy,

    AHA!

    It looks like some of the index values in the translation map are negative. When I comment out the array value assignments and add GePrint() statements to print the index values from the translation map, then delete a point, the console prints this:
    old count = 8; new count = 7
    old index = 4; new index = -1
    old index = 7; new index = 4

    When I add a check to see if the new index is less than 0, and if it is skip it, then it doesn't crash anymore. Yee-Ha!

    Adios,
    Cactus Dan



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 19/03/2008 at 14:01, xxxxxxxx wrote:

    Yippiee!! :)



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 19/03/2008 at 14:23, xxxxxxxx wrote:

    Howdy,

    Yep, looks like you were leading me in the right direction there, Samir.

    Evidently, if points are deleted, then some of the values in the .nIndex are negative, and if points are added, some of the values in .oIndex are negative.

    I don't know why I didn't think of that, because I've used -1 values in arrays before to let me know to skip those array members. :o(

    Adios,
    Cactus Dan



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 19/03/2008 at 14:56, xxxxxxxx wrote:

    Negative indices are a **baaaad* thing for arrays. Glad that you nipped it, Dan! :)


Log in to reply