none
Returning structures (containing structures) from a COM dll to a DotNet app. RRS feed

  • Question

  • I have a COM dll created in Visual Studio 6 (c++) that I am accessing from a DotNet application (VS2008). I have 3 nested structures in the COM idl and I have a function in the COM interface that the DotNet app calls and this function fills the structures and returns the data.  I made the arrays within all structures fixes because we don't want the user of the COM dll to have to be concerned with allocating/deallocating memory. This seems to work (I get the data returned) but my concern is if all memory is really freed once DotNet is done with it. Please see my structures below: These structures are declared in my COM idl.  First, I create my COM dll in my DotNet application and then declare my structure "ICOMLib.JobData Job;", but in the debugger I see Job.jf is "null". I thought I would see 50 of them (filled with garbage). When I call the function (in the COM dll) that fills the structure,"ICOM.GetResults( out Job)", the Job structure is filled correctly. But will the garbage collector in DotNet get rid of the memory? (I do not do any allocating of memory in the COM dll).

    Thanks,
    Alex


    typedef struct {
      int Len;
      int Start;
      int Width;
      float Spectrum[50];
    } SpecData;

    typedef struct {
      int numprod;
      BSTR Product_IDs[20];
      float concs[20];
      int numspec;
      SpecData specRs[5];
      float Thickness;
    } JobFormulaData;


    typedef struct {
      int numjf;
     JobFormulaData jf[50];
    } JobData;

    Wednesday, November 11, 2009 6:44 PM

Answers

  • Hi,
    In .NET, all reference objects are allocated on managed heap, and GC will collect objects that allocated on managed heap.
    Struct is value type, so, it is allocated on the stack, while array is reference type, we need a new keyword to allocate space for it on managed heap.
    For this case, the variable Job is allocated on stack, it memory will be reclaimed after the call return, variable jf is an array, we need to allocate memory for jf like this:
        jf = new JobFormulaData[50];
    when there is no root pointing to jf , the memory occupied by jf will be GCed.

    Thanks,
    Eric

    Please remember to mark helpful replies as answers and unmark them if they provide no help.
    Wednesday, November 18, 2009 9:46 AM

All replies

  • Hi,
    In .NET, all reference objects are allocated on managed heap, and GC will collect objects that allocated on managed heap.
    Struct is value type, so, it is allocated on the stack, while array is reference type, we need a new keyword to allocate space for it on managed heap.
    For this case, the variable Job is allocated on stack, it memory will be reclaimed after the call return, variable jf is an array, we need to allocate memory for jf like this:
        jf = new JobFormulaData[50];
    when there is no root pointing to jf , the memory occupied by jf will be GCed.

    Thanks,
    Eric

    Please remember to mark helpful replies as answers and unmark them if they provide no help.
    Wednesday, November 18, 2009 9:46 AM
  • When you are in doubt, just try it and measure it:
    Run your function in a loop 1000-times, or more. Before you call your function, run explicitly GC (System.GC.Collect) and check how much memory your app consumes. Do the same after you run your function in the loop. You should see that the memory consumption shouldn't be higher after the call then before the call.

    BTW: System.GC.Collect is great for testing and measurement purposes, but it should not be used in 'normal' code.

    -Karel

    Thursday, November 19, 2009 10:37 PM
    Moderator