Home All Groups Group Topic Archive Search About

memeory release from un managed code

Author
19 Jan 2006 11:15 AM
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;
}

Author
19 Jan 2006 11:25 AM
Ken Tucker [MVP]
Hi,

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

Ken
---------------
Show quoteHide quote
"sanu" <sa***@eircom.net> wrote in message
news:1137669305.558396.304310@g44g2000cwa.googlegroups.com...
> 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;
> }
>
Author
19 Jan 2006 2:01 PM
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


Ken Tucker [MVP] wrote:
Show quoteHide quote
> Hi,
>
>         The framework's garbage collector should dispose of the string for
> you.
>
> Ken
> ---------------
> "sanu" <sa***@eircom.net> wrote in message
> news:1137669305.558396.304310@g44g2000cwa.googlegroups.com...
> > 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;
> > }
> >