Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / August 2006

Tip: Looking for answers? Try searching our database.

Will SysAllocString allocation be GC'ed

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peggy - 29 Aug 2006 10:10 GMT
Hi

I have an unmanaged dll which returns a BSTR* allocated using a
SysAllocString on the unmanaged code.

Now when this BSTR is returned to the Managed world, will the memory
allocated to the BSTR be freed by GC or do I need to take care of
clearing off the Allocated memory?

Regards
Pawan Arya
Willy Denoyette [MVP] - 29 Aug 2006 20:05 GMT
| Hi
|
[quoted text clipped - 7 lines]
| Regards
| Pawan Arya

Unmanaged resources aren't freed by the GC, it's your responsability to free
the BSTR*.

Willy.
Mattias Sjögren - 29 Aug 2006 21:15 GMT
>Now when this BSTR is returned to the Managed world, will the memory
>allocated to the BSTR be freed by GC or do I need to take care of
>clearing off the Allocated memory?

The CLR will free the BSTR during the marshaling process if the
function has been declared correctly.

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Peggy - 30 Aug 2006 00:17 GMT
> The CLR will free the BSTR during the marshaling process if the
> function has been declared correctly.

Hi Mattias

Here's the declaration and how it is used in the C# code.

//*******DECLARATION and method IMPLEMENTATION**********//

HRESULT GetValue([in] BSTR bstrName, [out,retval] BSTR* pbstrValue);

STDMETHODIMP MyNativeClass::GetValue ( BSTR bstrName, BSTR* pbstrValue)
{
   std::wstring strValue = GetValue( std::wstring( CW2W(bstrName) ),
true );
   *pbstrValue = ::SysAllocString( strValue.c_str() );
    return S_OK; }

//*******USAGE in C# code**********//

if (objNative.GetValue( "_Source" ) == string.Empty)
OR
m_SomeVariable.AssignVal( objNative.GetValue( "_Source" ) )

Now, will the String onject be collected by GC.

Regards,
Pawan Arya
TDC - 30 Aug 2006 13:29 GMT
Well, you didn't actually show your C# p/invoke declaration (assuming
you don't have an interop assembly), but it looks *highly* likely from
your C# usage that the BSTR will get marshalled correctly (copied into
managed memory and then freed).

Tom

> > The CLR will free the BSTR during the marshaling process if the
> > function has been declared correctly.
[quoted text clipped - 24 lines]
> Regards,
> Pawan Arya

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.