Home All Groups Group Topic Archive Search About

how to pin object so the gc will not move it in memory ?

Author
5 Apr 2005 6:28 PM
roni
im allocating an object that is a wrapper of unmanaged code.

how can i pin that object in memory ,so the gc will not move it.

the reason is some other data members of the object,are sent to unmanaged
code.

hope im clear

Author
5 Apr 2005 5:47 PM
Herfried K. Wagner [MVP]
"roni" <ronix***@hotmail.com> schrieb:
> im allocating an object that is a wrapper of unmanaged code.
>
> how can i pin that object in memory ,so the gc will not move it.
>
> the reason is some other data members of the object,are sent to unmanaged
> code.

Take a look at 'GCHandle.Alloc' + 'GCHandleType.Pinned'.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
5 Apr 2005 6:40 PM
Mattias Sjögren
>the reason is some other data members of the object,are sent to unmanaged
>code.

Note that the runtime will pin arguments you pass to native code for
the duration of the call, so you only have to do it manually (the way
Herfried suggested) if the object must remain pinned after the call
returns.



Mattias

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