Where is this managed object stored?
-
value class ValBase
{
public:
int a;
};ref class RefBase
{
public:
int a;
};int main(array ^args)
{RefBase^ RefBase1 = gcnew RefBase; //LEGAL. Ref type Managed Obj created on CLR heap.
ValBase^ ValBase1 = gcnew ValBase; //LEGAL. Value type Managed Obj created on CLR heap.RefBase* RefBase2 = new RefBase; //ILLEGAL: new cannot be used on Managed Ref Class
ValBase* ValBase2 = new ValBase; //This compiles okay but where is this "Managed Object" stored ? CLR heap or Native heap ?}
In the last assignment where is the managed object stored ? I am totally new to C++ CLI. Also, is it true that value types should use stack semantics to make code efficient ? i.e instead of ValBase^ ValBase1 = gcnew ValBase, I should just use ValBase ValBase1;
-
value class ValBase
{
public:
int a;
};ref class RefBase
{
public:
int a;
};int main(array ^args)
{RefBase^ RefBase1 = gcnew RefBase; //LEGAL. Ref type Managed Obj created on CLR heap.
ValBase^ ValBase1 = gcnew ValBase; //LEGAL. Value type Managed Obj created on CLR heap.RefBase* RefBase2 = new RefBase; //ILLEGAL: new cannot be used on Managed Ref Class
ValBase* ValBase2 = new ValBase; //This compiles okay but where is this "Managed Object" stored ? CLR heap or Native heap ?}
In the last assignment where is the managed object stored ? I am totally new to C++ CLI. Also, is it true that value types should use stack semantics to make code efficient ? i.e instead of ValBase^ ValBase1 = gcnew ValBase, I should just use ValBase ValBase1;
ValBase2 is stored on the unmanaged heap since you are using new instead of gcnew. Thus, you will have to explicitly delete it:
delete ValBase2;
."We make a living by what we get, we make a life by what we give." --Winston Churchill
-
ValBase2 is stored on the unmanaged heap since you are using new instead of gcnew. Thus, you will have to explicitly delete it:
delete ValBase2;
."We make a living by what we get, we make a life by what we give." --Winston Churchill
Thanks George. So, is there any way for a Native pointer to point to Managed Heap. I know this is not the correct thing to do but is it possible ? An example would be appreciated. I also think there is no way for a Handle (^) to point to native heap, is this correct ?
-
Thanks George. So, is there any way for a Native pointer to point to Managed Heap. I know this is not the correct thing to do but is it possible ? An example would be appreciated. I also think there is no way for a Handle (^) to point to native heap, is this correct ?
The
GCHandle
class could help you out. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Thanks George. So, is there any way for a Native pointer to point to Managed Heap. I know this is not the correct thing to do but is it possible ? An example would be appreciated. I also think there is no way for a Handle (^) to point to native heap, is this correct ?
The native pointer can point to any address. Please explain why you want to use native pointer to managed resource? It is very incorrect way. I think you should change your architecture instead of making such a thing. If you want to call managed code (method of managed object) from unmanaged context you should use callback. Handle can not point to native heap, but IntPtr can.