PinnedArrays in API examples

Post your .Net discussions here
Post Reply
picowally
Newbie
Posts: 1
Joined: Wed Mar 16, 2022 6:29 pm

PinnedArrays in API examples

Post by picowally »

Hello everyone!
Quick question: In the API examples for the 2000A-Series (possibly others, that's the one I know so far :D ) the arrays passed to the GetValues and GetTimesAndValues methods are pinned (PinnedArray) and have to be fully initialized beforehand.
Why is this necessary and in what situation would it become problematic if not using a pinned array?
Does it have performance/speed implications?
I am using GCLatencyMode.Batch and did not have any trouble so far. :roll:

Martyn
Site Admin
Site Admin
Posts: 4525
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: PinnedArrays in API examples

Post by Martyn »

You need to use pinned arrays because you are passing these from your managed code to an unmanaged C++ dll, and their location in memory needs to be fixed so that the dll can safely write to the space.
Martyn
Technical Support Manager

picowally
Newbie
Posts: 1
Joined: Wed Mar 16, 2022 6:29 pm

Re: PinnedArrays in API examples

Post by picowally »

Hey Martyn, thanks for the answer!
I am using unpinned arrays for a while and have not encountered a problem even once.
So I am wondering now, how one would go about writing tests for this scenario :wink:
But that's a question for a different forum, I guess :D

BTW, the quality of your products is immensely appreciated.

bennog
Advanced User
Advanced User
Posts: 210
Joined: Mon Nov 26, 2012 9:16 am
Location: Netherlands

Re: PinnedArrays in API examples

Post by bennog »

for C# you
use a function that allocate 250 array's of random length between 200000 and 300000 integers and write them all with some number not 0
Then exit this function.

Then call this function 100 more times.

So you C# have some cleaning up to do.

Benno

picowally
Newbie
Posts: 1
Joined: Wed Mar 16, 2022 6:29 pm

Re: PinnedArrays in API examples

Post by picowally »

Hi bennog!
Interesting idea. If I understand correctly, I would write a C function that does 'something' (like iterating, maybe) over the arrays, and that function, should start to see discrepancies, right?
If I put increments of 1 in the arrays and check on the C side, data would get discontinous.

Thanks for the answer.

bennog
Advanced User
Advanced User
Posts: 210
Joined: Mon Nov 26, 2012 9:16 am
Location: Netherlands

Re: PinnedArrays in API examples

Post by bennog »

it is only needed where you use managed code like C# that use garbage collect.
And if you fragment the memory on C# and allocate and release a lot of various memory sizes, C# has the nasty habit of moving memory around to different physical addresses. So unmaaged code writes to the wrong addresses.

If you use C or C++ there is no need because these don't have a garbage collect, even C++ smart pointers do not shuffle memory around.

Benno

picowally
Newbie
Posts: 1
Joined: Wed Mar 16, 2022 6:29 pm

Re: PinnedArrays in API examples

Post by picowally »

That explains why I have no problems so far, without using the pinned arrays. The GC mode GCLatencyMode.Batch basically disables the GC :D

bennog
Advanced User
Advanced User
Posts: 210
Joined: Mon Nov 26, 2012 9:16 am
Location: Netherlands

Re: PinnedArrays in API examples

Post by bennog »

Remember Murphy's law

https://en.wikipedia.org/wiki/Murphy%27s_law

That is all I want to add at this moment, in my experience most times sooner than later, at a moment you can not use it.

Benno

picowally
Newbie
Posts: 1
Joined: Wed Mar 16, 2022 6:29 pm

Re: PinnedArrays in API examples

Post by picowally »

I'm curious what the decision was, not to use the "fixed" statement here, as it would make "unpinning" of the arrays unnecessary and we could just use regular GC routines.

bennog
Advanced User
Advanced User
Posts: 210
Joined: Mon Nov 26, 2012 9:16 am
Location: Netherlands

Re: PinnedArrays in API examples

Post by bennog »

Probably because fixed keyword only works in unsafe code.
unsafe code makes some programmers scared.

https://docs.microsoft.com/en-us/dotnet ... -statement

And fixed uses pinned do do there job so it also adds an extra layer on top of pinned.

Bemmp

picowally
Newbie
Posts: 1
Joined: Wed Mar 16, 2022 6:29 pm

Re: PinnedArrays in API examples

Post by picowally »

Ah yes, the unsafe-scare :) I totally forgot about that. Good point…
But as I understood it, there's actually more difference between pinning via GCHandle and fixed segments. THE Hans Passant has an interesting answer over at SO, but I am not knowledgeable enough about GC internals to fully grasp it. :roll:

bennog
Advanced User
Advanced User
Posts: 210
Joined: Mon Nov 26, 2012 9:16 am
Location: Netherlands

Re: PinnedArrays in API examples

Post by bennog »

I mostly 99% program in C / C++ and only for GUI stuff I use C#

So my C# answers are from some experience but not a lot I guess about 1 or 2k hours in my whole programming history.

Benno

d.marcato
Newbie
Posts: 0
Joined: Mon Apr 08, 2024 3:55 pm

Re: PinnedArrays in API examples

Post by d.marcato »

Is not using pinned arrays the reason why one might get the CallbackOnCollectedDelegate error?

d.marcato
Newbie
Posts: 0
Joined: Mon Apr 08, 2024 3:55 pm

Re: PinnedArrays in API examples

Post by d.marcato »

d.marcato wrote:
Tue Apr 09, 2024 6:07 am
Is not using pinned arrays the reason why one might get the CallbackOnCollectedDelegate error?
Apparently they are - so guys please use the pinned arrays to be safe

Post Reply