native members of managed classes?

Hi jedediah!
What's the preferred way to effectively have a native class as a member of a managed class
What do you mean

class NativeClass
{
};

public ref class ManagedClass
{
  protected private:
    NativeClass *_pNativeMember;
};





Answer this question

native members of managed classes?

  • ykessler

     jedediah wrote:
     NNTP User wrote:
    Hi Paul!
    A very good an deep explanation can be found here:
    http://www.bluebytesoftware.com/blog/PermaLink.aspx guid=88e62cdf-5919-4ac7-bc33-20c06ae539ae


    Sweet merciful ***, all that nonsense just to do what C++ destructors do in a concise and practically foolproof way How many .net code monkeys are going to be able to wrap their head around that or even know that it has to be done. I guess GC has effectively eliminated memory leaks at the expense of every other kind of resource flooding up to our knees.

    At any rate, thanks for the detailed info although I'm still not sure exactly what I have to do to guarantee that native member gets cleaned up.


    The code monkies should probably stick with C#; and besides, they'll probably still be shooting themselves in the feet with that.

    The C++/CLI team had to figure out a way to get deterministic destruction working for managed classes on top of the existing CLI/CLR architecture. The IDisposable pattern already fit the bill... remember, managed C++/CLI code is running in the managed environment, and C++ wasn't really designed for it in the first place.

  • farah78m

    What's the preferred way to effectively have a native class as a member of a managed class

  • J Mike Rowland

    Hi Jochen,

    Thanks for the wonderful link...
    I pretty much learned some new things with that...
    Thanks again...



    cheers,


    Paul June A. Domag
    nth geographics and geometrics

  • Leedo

     Jesse Towner wrote:
    The code monkies should probably stick with C#; and besides, they'll probably still be shooting themselves in the feet with that.

    The C++/CLI team had to figure out a way to get deterministic destruction working for managed classes on top of the existing CLI/CLR architecture. The IDisposable pattern already fit the bill... remember, managed C++/CLI code is running in the managed environment, and C++ wasn't really designed for it in the first place.


    It's the C#/VB monkies I'm worried about. C++/CLI seems to do the best job of making disposing transparent but it seems to be a house of cards with the other languages.

  • eechocheng

     jedediah wrote:

    It's the C#/VB monkies I'm worried about. C++/CLI seems to do the best job of making disposing transparent but it seems to be a house of cards with the other languages.


    I definitely Agree with that... Idea
    When Visual C++ entered the .net framework in VS2003 I knew that it would rule the .net langauge eventually... (thus here comes C++/CLI)




    cheers,


    Paul June A. Domag

  • Chuks

     NNTP User wrote:
    Hi Paul!
    A very good an deep explanation can be found here:
    http://www.bluebytesoftware.com/blog/PermaLink.aspx guid=88e62cdf-5919-4ac7-bc33-20c06ae539ae


    Sweet merciful ***, all that nonsense just to do what C++ destructors do in a concise and practically foolproof way How many .net code monkeys are going to be able to wrap their head around that or even know that it has to be done. I guess GC has effectively eliminated memory leaks at the expense of every other kind of resource flooding up to our knees.

    At any rate, thanks for the detailed info although I'm still not sure exactly what I have to do to guarantee that native member gets cleaned up.

  • tanglereid

     jedediah wrote:
    What's the preferred way to effectively have a native class as a member of a managed class


    To further elaborate, you cannot create a native class as a member of a managed class. Instead create a pointer to your native class as a member of your managed class... eg..

    class NativeClass
    {
    };

    ref class ManagedClass
    {
      public:
    //    NativeClass _pNativeMember; -- cannot do this replace with
       NativeClass* _pNativeMember;

       ManagedClass() {
    //    Initialize your pointer here...
          _pNativeMember = new NativeClass();
       }
       
       ~ManagedClass() {
    //    Destroy the object in the native heap..
          delete _pNativeMember;
       }
    };


    You cannot directly create a native object on the stack and make it as a member of your managed class because managed classes are placed in the managed heap... and it is constantly being moved by the GC...



    cheers,


    Paul June A. Domag
    nth geographics and geometrics

  • Michael Dausmann

    Hi Paul!
       ~ManagedClass() {
    //    Destroy the object in the native heap..
          delete _pNativeMember;
       }
    };

    You should also mention that if you do this, you should be familar with finilaziation and IDispose-Pattern!

    A very good an deep explanation can be found here:
    http://www.bluebytesoftware.com/blog/PermaLink.aspx guid=88e62cdf-5919-4ac7-bc33-20c06ae539ae

    Greetings
      Jochen

  • native members of managed classes?