
Signature
Tomas Restrepo
tomasr@mvps.org
Right, so it doesn't matter what happens to the pointer, as long as the data
it points to is still there, but if the data gets moved, doom...
And that's what the garbage collector does isn't it.
But when the GC 'moves' a managed object legitimately, does it update the
pointer to point to the new location? Presumably this is transparent
normally in .NET but it can't happen with unmanaged pointers?
Cheers
> Bonj,
>
[quoted text clipped - 13 lines]
>
> Quite different, really.
Tomas Restrepo \(MVP\) - 19 Oct 2004 12:06 GMT
Bonj,
> Right, so it doesn't matter what happens to the pointer, as long as the data
> it points to is still there, but if the data gets moved, doom...
> And that's what the garbage collector does isn't it.
It does both... move them, and remove them
> But when the GC 'moves' a managed object legitimately, does it update the
> pointer to point to the new location?
Only managed handles in places that are under the GC control (i.e the
managed heap, or the managed stack). It won't update pointers in places it
it can't control (like in the CRT heap, etc.)
That's the reason to have pinning pointers: ensuring the gc doesn't move the
object referenced in memory, so that the pointers you pass to it to
unmanaged code remain valid...

Signature
Tomas Restrepo
tomasr@mvps.org
Arnaud Debaene - 19 Oct 2004 13:28 GMT
> Right, so it doesn't matter what happens to the pointer, as long as the data
> it points to is still there, but if the data gets moved, doom...
> And that's what the garbage collector does isn't it.
Yes, after having deleted unreferneced objects, the GC compacts the
heap by moving all the remaining objects in contiguous space.
> But when the GC 'moves' a managed object legitimately, does it update the
> pointer to point to the new location?
It doesn't update pointers, it updates managed references (or
handles). In managed C++, those handles are represented by "managed
pointers".
> Presumably this is transparent
> normally in .NET but it can't happen with unmanaged pointers?
Yes, that's the whole point of pinning pointers : temporaly prevent
the GC to move an object, so that unmanaged code can use the memory
without risk of access violation.
Arnaud
MVP - VC