I have a  wrapper DLL that interfaces to another DLL that contains the following function:char * Foobar(void){    // BLAH is the function from the DLL this wrapper interfaces too    char *array = 0; // Set up an array pointer for dynamic memory allocation.    int NumOfChar = 0;    // Build array    for (int n=0; (*(BLAH+n) != '\0'); n++)    {            NumOfChar++; // keep track of how big BLAH is.    }    NumOfChar++;    // +1 so I can re-add the NULL    // Try to allocate an array based on the size of BLAH This is dynamic...    if (!(array = new char[NumOfChar]))    {           // If there's a problem allocating the memory, pop up a message.        MessageBox (0, Error: out of memory., Crap, MB_OK);     }    else    {        memcpy(array, BLAH,(NumOfChar)); // copy the contents of BLAH to array.    }    array[NumOfChar+1]='\0'; // ensure the last character is a NULL    FreeLibrary(hGetProcIDDLL); // release the DLL    return array;   }I am calling this DLL from LabVIEW which is don't think is specific to this question, as the DLL can be called by any program. My question is once Foobar returns the pointer to the array, how can I make sure that the array is deallocated afterwards? This seems to be a memory leak, because next time this routine is called, the next array won't necessarily overwrite the old array. It will just use up more and more memory, am I correct in this logic?Thank you
Worried about memory leak in DLL that creates a dynamic sized array
memory;dll
Yes, you're allocating memory but not freeing it, so there is a memory leak.A good approach is to allocate and deallocate the memory in the same place, i.e. wherever the DLL is called from - in your case, LabVIEW. That is, make your function void Foobar(char *array) (or int Foobar(char *array) and return the array length). Then you just need to make sure you call the function with big enough array. Perhaps add another parameter do indicate the maximum allowed length, int Foobar(char *array, int maxLen) to react instead of crashing in case of insufficient length.