Re: Garbage collectable pinned arrays!
- From: "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx>
- Date: Tue, 12 Feb 2008 21:19:00 +0100
"Atmapuri" <janez.makovsek@xxxxxxx> wrote in message news:DC92D886-0A02-44D9-8450-33C1FE91AA05@xxxxxxxxxxxxxxxx
Hi!
No, you don't because somehow you must pin the object to prevent premature collection. It's not because an object that is not pinned, is not referenced by the unmanaged code.
Consider following sample:
int[] ia = new ia[200000]; // large object stored on the LOH at a fixed location (not moveable.
UnmanagedFunctionThatTakesAnArrayOfInts(ia);
if the interop layer would not pin the array, the GC would be free to collect the object and as such invalidate the pointer you have passed to unmanaged code. The reason for this is that the JIt doesn't know about unmanaged code, as a result he *marks* the reference "ia" as a candidate for collection (signals the GC that the object ia refers to may be collected at the moment of the call.
That is not the case, if you put
GC.KeepAlive(ai);
at the end of your code, which not costs "Nothing" (!!!)
Oh, and you forget one of these KeepAlives and hell breaks loose. And again why would you need this for, the arguments passed through PInvoke are protected when needed for the duration of the call .
Willy.
.
- References:
- Re: Garbage collectable pinned arrays!
- From: Jesse McGrew
- Re: Garbage collectable pinned arrays!
- From: Atmapuri
- Re: Garbage collectable pinned arrays!
- From: Willy Denoyette [MVP]
- Re: Garbage collectable pinned arrays!
- From: Atmapuri
- Re: Garbage collectable pinned arrays!
- From: Willy Denoyette [MVP]
- Re: Garbage collectable pinned arrays!
- From: Atmapuri
- Re: Garbage collectable pinned arrays!
- Prev by Date: Re: Multithreaded GC!
- Next by Date: using old ocx control
- Previous by thread: Re: Garbage collectable pinned arrays!
- Next by thread: Re: Garbage collectable pinned arrays!
- Index(es):
Relevant Pages
|