C++ SDK



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

    On 06/10/2004 at 02:14, xxxxxxxx wrote:

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

    ---------
    Hello........
    I've got a problem in Memory Handling in Cinema 4D SDK (C++).
    Really, I never use BaseObject::Free() to release the memory although I use BaseObject::Alloc to allocate the memory. After running the code, I look at the file c4d_debug.txt ( the same path as .cdl file).But nothing is written. So I want to know whether it means that it's OK if we don't relaease the mem we allocate using Alloc().
    Although I usually do not encounter the problem, sometimes when using my plugins, a dialog box showing an instruction at 0XXXXXXX cannot read or write at 0XXXXXXXXX, and then C4D terinated abnormally ( without saving anything!).
    So I try to use BaseObject::Free().
    Let's say........
    ----------------------------------------------------------
    for (.........)
    {
    Obj=BaseObject::Alloc(Ocube); //like that
    ........
    }
    //outside the loop
    BaseObject::Free(Obj);
    ---------------------------------------------------------
    Please assume the Obj is in the accessible scope of the both places of usage. I found that if I create 3 Obj (i.e i looping iterates 3 times) I got only two Obj.
    Please show me the way to solve that problem...and the similar.... I don't want to tell about the similar problems not to make u down.
    So, in gist,..........
    if I don't use BaseObject::Free(), I encounter the problem of terminating abnormally.
    if I use it, it works in an awkward way ( but because of my un-knowing how to)
    So , hoping you can help me and thanks:
    ZawMinTun



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

    On 08/10/2004 at 05:25, xxxxxxxx wrote:

    If you want to debug memory leaks, put the c4d_debug.txt in the directory where C4D is started from. If you got the right directory, C4D will show an additional debug window while it's running. Please note that the memory leak checking will only work, when you compile your plugin with a _DEBUG define.

    The Alloc()/Free() example you gave will not work properly, because every iteration of the for-loop will allocate a new BaseObject, but the Obj pointer to the previous one will be lost, because it is overwritten. After leaving the loop, you will only have the pointer to the last allocated object, all the previous ones will never be freed, so the memory is lost.

    Solution: Alloc() and Free() have to be properly paired, for every call to Alloc() you need a corresponding Free() somewhere, unless you deliberately give up ownership of the object. As an alternative, use the AutoAlloc object, this will solve these problems, since an object allocated with AutoAlloc will be freed automatically.

    If C4D crashes on you when you call Free(), you most likely passed a dangling pointer to Free(), that is a pointer to an unaccessible memory region. Prominent causes for dangling pointers are wrong pointer arithmetics or pointers to objects which have already been freed. Anyway, such a crash indicates a problem with the memory/pointer handling in your plugin.

    Michael


Log in to reply