Re: pinning types via 'Fixed'

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: 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)
  • Re: Garbage Collection Issues in long-standing services
    ... The problem with sockets is the unmanaged memory buffers used by the ... (probably a byte array) ... The result is that, depending of the number of buffers, ... > reference between CS and AP, so I don't explicitly set each reference to ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Image::Magick memory leak question
    ... >have drasticly lowered the ammount of memory that it's leaking. ... You are undefining a reference to the ... elements in the array when you undefine it. ... So the amount of leak is limited to just the empty array object's memory ...
    (comp.lang.perl.misc)
  • Re: Fast string operations
    ... Looping: I thought looping over arrays in managed code was "slow" ... array handling and such. ... The problem with TrimHelper is that it always returns a new string instance. ... The customer perceives this as a memory leak. ...
    (microsoft.public.dotnet.languages.csharp)

Loading