Re: pinning types via 'Fixed'
From: cody (dont.spam.me.deutronium_at_gmx.de)
Date: 02/07/04
- Next message: cody: "Re: Why are there no indexed properties?"
- Previous message: Fede: "CryptoStream question"
- In reply to: Chris Finlayson: "pinning types via 'Fixed'"
- Next in thread: Rob Teixeira [MVP]: "Re: pinning types via 'Fixed'"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 7 Feb 2004 22:32:55 +0100
> 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.Length)
> }
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
- Next message: cody: "Re: Why are there no indexed properties?"
- Previous message: Fede: "CryptoStream question"
- In reply to: Chris Finlayson: "pinning types via 'Fixed'"
- Next in thread: Rob Teixeira [MVP]: "Re: pinning types via 'Fixed'"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|