CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2004
    Posts
    23

    Smile how to release the memory used by a structure?

    I am trying to catch some memory leakage in others code. It is really a suffer.
    My question is that the structure array is not given memory using new or malloc, it is given value directly, so do I need to release it?
    If I need to do it, how should I do it?
    Should I release the field one by one? or something else.
    Thank you very much.
    Can you please give some sample code about this ??

    the code is below:

    struct THREAD_PARMS {
    // Need change
    IClientVirtualDeviceSet *vds;
    const char * vdName;
    const char * filePath;
    bool doCompression ;
    int taskId;
    int taskScheduleId;
    int taskExecId;
    JavaVM *vm;
    jclass obj ;
    jobject execResult;
    char* lpInstanceName;
    int first;
    };

    THREAD_PARMS parms[32]; // each thread needs its own parm block
    HANDLE children[33]; // 32 is maximum number of streams.

    for (ix=0; ix<numFiles; ix++)
    {
    //get the JVM
    JavaVM *vm ;
    env->GetJavaVM(&vm) ;

    // All threads share the same virtual device set,
    // but must operate on different virtual devices.
    //
    if(lpInstanceName == NULL)
    {
    //printf("startSecondaries: Default Instance\n");
    parms[ix].vds = vds;
    parms[ix].vdName = vdNames[ix];
    parms[ix].filePath = filePaths[ix];
    parms[ix].doCompression = doCompression ;
    parms[ix].obj = obj;
    parms[ix].taskId = taskId;
    parms[ix].taskScheduleId = taskScheduleId;
    parms[ix].taskExecId = taskExecId;
    parms[ix].vm = vm;
    parms[ix].execResult = execResult ;
    parms[ix].lpInstanceName = lpInstanceName;
    parms[ix].first = 1;

    children[ix] = (HANDLE)_beginthreadex (
    NULL, 0, runSecondary, (void*)&parms[ix], 0, &threadId);
    }

  2. #2
    Join Date
    Oct 2004
    Location
    Long Beach, CA
    Posts
    179

    Re: how to release the memory used by a structure?

    No, you don't, 'cause the struct is stored on the stack, so once your program ends or the function returns, it'll ge destroyed automatically. And, oh yeah, next time use the "" tags when posting code. Or just put the .cpp file as an attachment if the code is *really* long.
    The nerds will rule over the common folk. The geeks will rule over the nerds. The supergeeks will rule over the geeks. The hackers will rule over the supergeeks. The superhackers will rule over the hackers. Mwhahaha; I'll rule you all!!!

  3. #3
    Join Date
    Oct 2004
    Posts
    23

    Re: how to release the memory used by a structure?

    Thank you. Got it.

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: how to release the memory used by a structure?

    Well...the rule of thumb is pretty easy...you have to release memory if
    • it was allocated with 'new'/'new []'
    • it was allocated with 'malloc()'/'calloc()'
    • the specification of the function explicitly states it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured