none
VC++: managed class with a data member of an unmaged class type RRS feed

  • Question

  • I've defined an unmanaged class called DVDClass.  In one of my managed classes, I've declared a data member as a pointer that would point to an instance of DVDClass.

    In the managed class, the data member is declared as follows:

    public
    : DVDClass *MyDVDRenderer;

    and I instanciate the unmanaged class as follows:

    MyDVDRenderer =
    new DVDClass();

    DVDClass has data members that are pointers to COM objects, as well as data members that are interface pointers.  These data members are declared as follows:

    IDvdGraphBuilder *pDvdGraphBuilder;
    IGraphBuilder *pDVDFilterGraphManager;

    And these pointers receive their values as follows:

    HRESULT hr = CoCreateInstance(CLSID_DvdGraphBuilder, NULL, CLSCTX_INPROC_SERVER, IID_IDvdGraphBuilder, (
    void **)&pDvdGraphBuilder);
    hr = pDvdGraphBuilder->GetFiltergraph(&pDVDFilterGraphManager);

    Now the problem I'm having is that these data members of DVDClass that are pointers, loose their values between function calls from the managed class to the unmaged class.  I understand that this might happen when the garbage collector moves managed objects to a different location in the managed heap.

    How can I prevent the garbage collector from tampering with the pointers in the unmaged object, the unmaneged object being pointed to by a data member of the managed class?

    Sunday, October 18, 2009 2:54 PM

Answers

  • There is something else going on.  The garbage collector can only move managed objects, it doesn't touch unmanaged memory.  The kind that gets allocated with new or CoCreateInstance().  An object of your managed class will definitely get moved, but the DVDClass pointer value gets moved along with it.  The pointer value doesn't change, it still points to the unmanaged memory where the DVDClass object was allocated.

    Pointer trouble like this is invariably caused by heap corruption.  Always tough to debug.  Use the debugger's data breakpoint feature.

    Hans Passant.
    • Marked as answer by Ledigimate_ Sunday, October 18, 2009 7:19 PM
    Sunday, October 18, 2009 5:11 PM
    Moderator
  • Problem solved.

    The managed class has two data members of type DVDClass, and I've mistaken one for the other, trying to reference one of its COM object pointers to which no value have yet been assigned.  So actually no pointers have "lost" any values.  I was just referencing the wrong pointer, which happened to have a NULL value at that point.

    Silly me.
    • Marked as answer by Ledigimate_ Sunday, October 18, 2009 7:19 PM
    • Unmarked as answer by Ledigimate_ Sunday, October 18, 2009 7:20 PM
    • Marked as answer by eryang Tuesday, October 27, 2009 9:48 AM
    Sunday, October 18, 2009 7:19 PM

All replies

  • There is something else going on.  The garbage collector can only move managed objects, it doesn't touch unmanaged memory.  The kind that gets allocated with new or CoCreateInstance().  An object of your managed class will definitely get moved, but the DVDClass pointer value gets moved along with it.  The pointer value doesn't change, it still points to the unmanaged memory where the DVDClass object was allocated.

    Pointer trouble like this is invariably caused by heap corruption.  Always tough to debug.  Use the debugger's data breakpoint feature.

    Hans Passant.
    • Marked as answer by Ledigimate_ Sunday, October 18, 2009 7:19 PM
    Sunday, October 18, 2009 5:11 PM
    Moderator
  • Problem solved.

    The managed class has two data members of type DVDClass, and I've mistaken one for the other, trying to reference one of its COM object pointers to which no value have yet been assigned.  So actually no pointers have "lost" any values.  I was just referencing the wrong pointer, which happened to have a NULL value at that point.

    Silly me.
    • Marked as answer by Ledigimate_ Sunday, October 18, 2009 7:19 PM
    • Unmarked as answer by Ledigimate_ Sunday, October 18, 2009 7:20 PM
    • Marked as answer by eryang Tuesday, October 27, 2009 9:48 AM
    Sunday, October 18, 2009 7:19 PM