Re: pinning types via 'Fixed'

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: cody (dont.spam.me.deutronium_at_gmx.de)
Date: 02/07/04


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


Relevant Pages

  • Re: How to give selective access to the methods in a class?
    ... different memory locations at different times. ... The reference still leads to the ... So array resizing ... program pushes certain objects into the circular buffer, ...
    (comp.lang.java.programmer)
  • Re: Java Memory question
    ... freed when the last reference to an object is lost. ... an Object array, or as objects in a class. ... Or is there a better way to move it without eating double memory? ... Instance variables exist from the time they are created (when the object ...
    (comp.lang.java.help)
  • Re: multidimensional array check
    ... In perl every value you store no mater if it is a scalar an array ... computers memory. ... This reference tells perl where in the memory $x is stored. ...
    (perl.beginners)
  • Re: Java Memory question
    ... All objects in Java are garbage collected, so the memory will be ... freed when the last reference to an object is lost. ... an Object array, or as objects in a class. ... Instance variables exist from the time they are created (when the object ...
    (comp.lang.java.help)
  • Re: High Memory Consumption of Classes and Arrays
    ... > that classes and arrays in dotnet consume very much memory. ... you consider the reference as well). ... a reference to an array which contains the actual items. ...
    (microsoft.public.dotnet.framework.performance)