Hello,

I'm having several issues with the GC, and some ideas to try to solve it
that I need to clarify. I had a C# method that I stripped down in order to
reimplement it in C++, using the 'extern' keyword and the
[MethodImplAttribute(MethodImplOptions.InternalCall)]modifier, just as
several methods of some Rotor classes are already 'natively' implemented.

This method receives an object parameter, whose type is a normal C# class.
This class has a C++ counterpart that I use to manipulate its data in C++,
and I'm sure that the mapping from the C# class to the C++ one is
implemented and done correctly. In some point of the execution, I need to
'Clone' this parameter in order to store a separate copy of it, but the copy
is not an exact one, because I don't need (and I can't) to clone all the
object's attributes, so the situation is like this:

C# class:

Class C
{
 string param1;
 Object param2;
}

C++ class:

Class C
{
 StringObject *param1;
 Object *param2;

 Public C Clone ();
}

Method implementation:

Method(C *classObj, ...)
{
 ....
 //If some conditions are true, then I need to clone classObj
 newObj = classObj->Clone();
 //Store the copy (newObj) in a handle table in order to avoid it to be
GC-collected.
}

C's Clone method is implemented as follows:

C *C::Clone()
{
... //Create a new Object loading the type and so on.
 //Copy the attributes
 newObject->param1 = param1;
 newObject->param2 = param2;

 return newObject;
}

Although both objects are inserted in a handle table in order to avoid them
to be collected by the GC, during execution it seems that the copy that I
made loses his attribute values. So, the pointers seem to be not valid
anymore and I understand that the objects that are pointed to in that
location where moved. The question is: What do I need to do in order to
avoid this problem? Is there any way to safely do what I'm pretending to do
or do I need to use any other procedure? I also GCPROTECT both objects
during the operations.

Hope that I explained well the situation. Thank you very much for your help,

J. Redondo

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to