memeory release from un managed code

S

sanu

Hi,

Need your help,
From our vb.net web service we are calling a unmanaged C++ win32 dll
function. The dll function takes a string (consists of huge data)
parameter byref. Below code sample.

Do we have to take care in .net for the memory allocated in the c++ dll
(SysAllocStringByteLen ?). Or does .net on exiting handles this. How
does we release the memory allocated.. Please..

Thanks
saish (Developer)


Vb.net code:
---------------
Declare Function fnstrOutput Lib "C:\net\dlls\xmlref.dll" _
Alias "STR_OUTPUT" (ByRef outString As String) As Integer
...
...

<WebMethod()> _
Public Function Teststring() As String

Dim outstr as string

rc = fnstrOutput(outstr)

return "return : " + ctype(rc,string)

End Function


C++ win32 dll code:
---------------------------
extern "C"
{
extern int WINAPI fnOutput(BSTR *poutputDoc);
}

//******* Test passing Output string ByRef *****************

int WINAPI fnOutput(BSTR *poutDoc)
{


SysFreeString(*poutDoc);

// SysAllocStringByteLen() function takes ANSI string and returns

// a BSTR.
// poutXML is a DOMDOCUMENTPTR type ...

*poutDoc = SysAllocStringByteLen(poutXML->xml, poutXML->xml.length()
-1);

return 90;
}
 
K

Ken Tucker [MVP]

Hi,

The framework's garbage collector should dispose of the string for
you.

Ken
---------------
 
S

sanu

Hi Ken,

Thanks for your reply.

I had been reading the various forums saying we have to release the
memory ( using SysFreeString), which are created by un-managed code (in
my case win32 c++ dll). But was somewhat confused how this was
possible. Please excuse my lack of knowledge ..But how does the
(garbage collector ?) vb.net program releases (de-allocates) the memory
allocated by the un managed code.

Thanks
saish
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top