By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,670 Members | 1,501 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,670 IT Pros & Developers. It's quick & easy.

pinning types via 'Fixed'

P: n/a
Hello:

I have a question on pinning types via 'fixed'. In "Com and .NET
interoperability" by Andrew Troelsen, he provides a simple example of
..NET -> Native interoperability:

int[] theVals={1,2,3,4};
MyCustomDLLWrapper.AddArray(theVals,theVals.Length )

where MyCustomDLLWrapper wraps a native (C++) .dll:

public class MyCustomDLLWrapper
{
[DllImport("MyCustomDll.dll")]
public static extern int AddNumbers(int x[], int size);
}

Where AddNumbers is defined (obviously) in MyCustomDll.dll

Now, for correctness, shouldn't he be locking the reference to the
array in memory so it isn't garbage collected? So, shouldn't it be:

int[] theVals={1,2,3,4};
fixed(int* pinnedAry = theVals)
{
MyCustomDLLWrapper.AddArray(pinnedAry,theVals.Leng th)
}

?

Thanks!

Chris Finlayson
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
> I have a question on pinning types via 'fixed'. In "Com and .NET
interoperability" by Andrew Troelsen, he provides a simple example of
.NET -> Native interoperability:

int[] theVals={1,2,3,4};
MyCustomDLLWrapper.AddArray(theVals,theVals.Length )

where MyCustomDLLWrapper wraps a native (C++) .dll:

public class MyCustomDLLWrapper
{
[DllImport("MyCustomDll.dll")]
public static extern int AddNumbers(int x[], int size);
}

Where AddNumbers is defined (obviously) in MyCustomDll.dll

Now, for correctness, shouldn't he be locking the reference to the
array in memory so it isn't garbage collected? So, shouldn't it be:

int[] theVals={1,2,3,4};
fixed(int* pinnedAry = theVals)
{
MyCustomDLLWrapper.AddArray(pinnedAry,theVals.Leng th)
}

The array cannot be garbage collected since there is still a refererence on
it. But it might be moved in memory when not beeing fixed so it is IMHO
always nesseccary to fix objects when passing them to native functions.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 15 '05 #2

P: n/a
Arrays of blittable types are automatically pinned by the P/Invoke
infrastructure when you pass them to unmanaged export functions.
However, they are only pinned for the duration of the call.

-Rob Teixeira [MVP]

"Chris Finlayson" <fi********@yahoo.com> wrote in message
news:9d**************************@posting.google.c om...
Hello:

I have a question on pinning types via 'fixed'. In "Com and .NET
interoperability" by Andrew Troelsen, he provides a simple example of
.NET -> Native interoperability:

int[] theVals={1,2,3,4};
MyCustomDLLWrapper.AddArray(theVals,theVals.Length )

where MyCustomDLLWrapper wraps a native (C++) .dll:

public class MyCustomDLLWrapper
{
[DllImport("MyCustomDll.dll")]
public static extern int AddNumbers(int x[], int size);
}

Where AddNumbers is defined (obviously) in MyCustomDll.dll

Now, for correctness, shouldn't he be locking the reference to the
array in memory so it isn't garbage collected? So, shouldn't it be:

int[] theVals={1,2,3,4};
fixed(int* pinnedAry = theVals)
{
MyCustomDLLWrapper.AddArray(pinnedAry,theVals.Leng th)
}

?

Thanks!

Chris Finlayson

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.