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) != ''); 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]=''; // 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
2
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.
3
Another approach is to return the char* wrapped in a smart pointer, like
// If you want several scopes keeping references to the same memory
std::shared_ptr<char> Foobar(void)
//or (if you only want a single reference to be available)
std::unique_ptr<char> Foobar(void)
Notices, that the previous two types (std::shared_ptr
& std::unique_ptr
) were introduced recently to the STL.
If that is the case, you can use boost, then you can have
boost::shared_ptr<char> Foobar(void) // for example.
Note for VC++: Both the client code and the DLL (with Foobar) must be compiled with the same version of the MSVC runtime, like both MSVC2013 or both MSVC2015 (which is not a problem in modern VC++, since that is enforced by linker I think). The client also must be compiled with MD runtime.
For more details on this, see https://blogs.msdn.microsoft.com/oldnewthing/20060915-04/?p=29723