Re: Cannot convert parameter from cli::interior_ptr<Type> to HRASC

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



"Ben Voigt" <rbv@xxxxxxxxxxxxx> wrote in message news:%23K4d%23l6cHHA.4388@xxxxxxxxxxxxxxxxxxxxxxx

"Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx> wrote in message news:%23kdbjO6cHHA.2068@xxxxxxxxxxxxxxxxxxxxxxx
"Ben Voigt" <rbv@xxxxxxxxxxxxx> wrote in message news:%23Tasm$vcHHA.3648@xxxxxxxxxxxxxxxxxxxxxxx

"Tamas Demjen" <tdemjen@xxxxxxxxx> wrote in message news:OwM41UvcHHA.4984@xxxxxxxxxxxxxxxxxxxxxxx
Whitney Kew wrote:

I ended up doing what you suggested, so thank you. However, could I have used a pin_ptr<> here? Your response implies that doing so wouldn't be ideal; why is that? I'm more curious than anything. Why is pinning an entire object a bad thing? Appreciate any thoughts....

Pinning tells the garbage collector that the object can't be moved in the memory, because there is a native pointer to it. In a heavily multi-threaded project, such as a server/service, very extensive pinning might hurt performance in some cases, because it limits the way the gabage collector can arrange your objects.

It's the same issue as passing objects by value vs by reference. When the object is an integer, a pointer, or even a double, I tend to pass it by value, while anything larger than that is most efficiently handled when passed by reference.

Similarly, if you have a large managed object or array that needs to be passed to a native function, just pin it. Pinning is very efficient if you don't keep it pinned for longer than necessary. On the other hand, if you just need to pass a single pointer or integer to a native function, it's probably easier and cleaner to introduce a temporary and copy it by value.

Pinning appears to copy the object to the large object heap, so for large objects it's trivial, and for small objects it incurs the copy that you probably chose pass-by-pointer to avoid. It also causes additional fragmentation.

Not at all, "pinning" puts a *reference* to the instance in the "handle table", which happens to be part of the LOH in the current implementation of the CLR, the pinned object stay's where it is, wherever that may be. Unpinning removes the reference from the handle table. Pinning objects for a long period can lead to fragmentation, as the GC cannot relocate the object in the heap during compactation.

From testing with PtrToStringChars with and without pinning, the pointer moved significantly for small strings, and not at all for large strings, and the pinned small string looked like it moved into the area with the large strings. Maybe a GC collection happened to occur in between, but I doubt it, those are usually triggered by allocation and in any case I didn't have a high enough number of allocations to require collection.



Pinning is the act of fixing the location of the object instance instance in the GC heap.
Again all there is done is taking the address and wrap it in a GCHandle and store this handle in the handle table.
The handle table contains stuff like:
Strong Handles, Pinned Handles, Async Pinned Handles, Ref Count Handles, Weak Long Handles, Weak Short Handles.
The easiest way to watch all these is by running your code in windbg.exe debugger and load the SOS.DLL debugger extension.
All you need is simple program like this:
....
GC::Collect(); // clean-up the garbage so far
GC::WaitForPendingFinalizers();
array<int>^ ia = gcnew array<int>(10);
GCHandle^ pinnedObj = GCHandle::Alloc(ia, GCHandleType::Pinned);
DebugBreak();
Console::WriteLine("0x{0:x4}", (int)pinnedObj->AddrOfPinnedObject());
....
load the sos extension and issue a !dso followed by a !do command, specifying the reference of the array instance.
!eeheap returns the locations of the heaps, the array should live in the Gen0 heap.
!gcroot <objref> returns the number of roots and should show you something like:
....
edi:Root:026b38b8(System.Int32[])
Scan Thread 0 OSTHread 9b8
Scan Thread 2 OSTHread 348
DOMAIN(004B3400):HANDLE(Pinned):211590:Root:026b38b8(System.Int32[])
see here the object is rooted and pinned at location 026b38b8 (in the GC heap)
!gchandles will shows all handles in the handle table, you should find the array in this table as well.



Willy.

.



Relevant Pages

  • Re: Returning an array of strings in C
    ... # I am writing a function that needs to return an array of strings and I ... How does the function return the array? ... the caller has to free to reclaim the heap. ... Death is the worry of the living. ...
    (comp.lang.c)
  • Re: CPtrArray/Heap failed...
    ... I am creating strings on heap and storing address in CPtrArray object. ... just CStringArray to store the array of strings? ...
    (microsoft.public.vc.mfc)
  • RE: When do I need to pin?
    ... when is manual pinning required in PInvoke, ... But why do we still need to pin the byte array with the 'fix' ... Microsoft Online Community Support ... You can send feedback directly to my manager at: ...
    (microsoft.public.dotnet.framework.interop)
  • Re: unmanaged vs managed.
    ... Is using fixed or is pinning an object the same thing?(Ignoring the fact ... This leads to heap fragmentation. ... Would using unmanaged memory be better in this case? ... Allocate a big array and keep it around. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Garbage collectable pinned arrays!
    ... LOH (all version of the CLR) are at a fixed address for their life ... but then they don't require pinning. ... from the LOH, OLE task allocator, etc, etc. ... But now you are allocating from the unmanaged heap (COM heap or CRT ...
    (microsoft.public.dotnet.languages.csharp)