Returning string from dll - memory leak?

From: Anders Thomsen (junk_at_mexp.dk)
Date: 04/30/04


Date: Fri, 30 Apr 2004 13:11:49 +0200

Hi,

I'm trying to return a string from a c++ dll in a method I load from VB.NET.
This is the method is defined like this:
extern "C" __declspec(dllexport) LPSTR GetData(BSTR hostExport, int
portExport)

The code I use for returning the string is this:
...
LPSTR lpReturnStr;
lpReturnStr = (LPSTR)malloc(strlen(csRef) + 1); //csRef is a CString
strcpy(lpReturnStr, csRef);
return lpReturnStr;

My question is, if the second line will cause a memory leak, as I do not
manually release the memory created by malloc.
Does the caller (in this case, the VB.NET application) free the string, or
should I do this in an other way?

Thanks



Relevant Pages

  • Re: Memory Leak in VB6 with strings and UDTs
    ... I have found what I think is a memory leak at the core of Visual Basic 6, ... When, in a loop over my arrays, I set BString1 to an element of an array, ... I have tried casting the number to a string with CStr but that doesn't help ... Dim bytearrayAs Byte ...
    (microsoft.public.vb.general.discussion)
  • Re: another memory leak, weird`
    ... when I input a few charactors, like 10 charactors, no memory leak ... The string 'a' has not yet gone out of scope and has not been destroyed ... the heap is reported as a leak. ...
    (microsoft.public.vc.language)
  • Re: Is how to lay out the constant string compiler dependent?
    ... memory of the string that you have used except store the address in a ... the memory as the general memory leak. ... a literal array that is modifiable cannot ...
    (comp.lang.c)
  • Dispose Unmanaged resources
    ... We all know that in .NET, we don't need to worry about memory leak ... illustrate the step necessary in disposing unmanaged resources. ... public static extern bool COMDbConnection (string connectionString, ...
    (microsoft.public.dotnet.general)
  • Re: Is how to lay out the constant string compiler dependent?
    ... memory of the string that you have used except store the address in a ... the memory as the general memory leak. ... strings are not the same in SunOS, so it seems that how to lay out the ...
    (comp.lang.c)