Passing Unmanaged Class by reference to Managed Class Constructor

I have a MainForm. It has two child forms.

The program reads in large SEGY files (2gb -> 100gb), appropiate data and read settings are stored through the class system that has a main controller "SEGY.H" & "SEGY.CPP". They a coded in pure C++.

My two child forms are managed. I want to pass a reference for SEGY from the MainForm to each child form when they are required to be shown to the user, obviously to avoid copying large amounts of memory.

 

I cannot pass a native class by reference to a managed class through the managed classes constructor. 

I have tried (code in mainform):
private: SEGY *Segy;        //it then gets constructor call when filename is chose

CHILDFORM ^childform1( Segy );    //constructor for 1st child form that needs to be shown

 

where I have tried the following as constructor arguments in CHILDFORM.H

CHILDFORM(SEGY* &Segy_inputSegyContainer);

CHILDFORM(SEGY* %Segy_inputSegyContainer);

The latter works and runs (%). Debug in MainForm.h on this line shows under Locals that this->Segy points to a SEGY class with correct values. Debug following CHILDFORM constructor through into CHILDFORM.cpp shows the reference gets curropted and Segy_inputSegyContainer points to a curropted class.

Please can you help me.



Answer this question

Passing Unmanaged Class by reference to Managed Class Constructor

  • JohnWP

    I think I may know the anwser. First off you should know the only valid native code in a managed class is a pointer to a unmanaged object. So any way you don't need to pass by refrence, all you have to do is pass a pointer by value. This is completly valid and will avoid copying the whole file.

    Try some thing like this:

    public ref class MyManaged

    {

    private:

      SEGY* Segy;

    public:

       MyManaged(SEGY* Segy)

       {

           this->Segy = Segy;

       }

    };

    Basically just forget passing by refrence, it is not nessasary in this case, so just remove the & and the %. BUT remember, what I just said will only hold true if you use dynamic allocation aka "new" when you first create your SEGYs, although I doubt you are making them on the stack as that would be both very uncommon and idiotic.

    good luck, let us know how you made out    


  • Fwank79

    Thank you for taking the time to reply to my post. I basically get the following error message in "MainForm.h" when I compile

    I have made a simple project that highlights the problem. The thing that makes me so cross about this is I do not feel I am doing anything radical or complicated. I simply want to use the class system to pass by reference to avoid copying memory. I just feel I haven't discovered the syntax yet.

    "cannot convert parameter 1 from 'SEGY *' to 'SEGY *&'

    An object from the gc heap (member of a managed class) cannot be converted to a native reference"

    The simple project that highlights the problem can be downloaded at http:\\www.will-turner.co.uk\microsoftDrivingMeMad.zip


  • Gus B

    CHILDFORM(SEGY* &Segy_inputSegyContainer);
    CHILDFORM(SEGY* %Segy_inputSegyContainer);

    Why not SEGY*


  • JDCAMP

    My test number 1:

    class Test1
    {
    public:
    Test1(int n) : data(n)
    {
    }

    int GetData(){return data;}

    private:
    int data;
    };

    ref class Test2
    {
    public:
    Test2(Test1& t) : test1(t)
    {
    }

    void Print()
    {
    Console::WriteLine(test1.GetData().ToString());
    }

    private:
    Test1& test1;

    };

    int main(array<System::String ^> ^args)
    {
    Test1 t1(5);
    Test2 t2(t1);
    t2.Print();

    return 0;
    }

    Test number 2:

    class Test1
    {
    public:
    Test1(int n) : data(n)
    {
    }

    int GetData(){return data;}

    private:
    int data;
    };

    ref class Test2
    {
    public:
    Test2(Test1* t) : test1(t)
    {
    }

    void Print()
    {
    Console::WriteLine(test1->GetData().ToString());
    }

    private:
    Test1* test1;

    };

    int main(array<System::String ^> ^args)
    {
    Test1 t1(5);
    Test2 t2(&t1);
    t2.Print();

    return 0;
    }

    Both versions successfully compile and run. What is your code exactly and what problem do you have


  • Passing Unmanaged Class by reference to Managed Class Constructor