pointer to pointer to a managed class

I

Itay_k

Hello,

I want a member in my class that will save pointer to pointer to
System::Drawing::Image class.
When I write on my class code:

System::Drawing::Image **bmp;

I get this error message:
error C3160: 'bmp' : cannot declare interior __gc pointer or reference as a
member of 'RCClientNS::RCClientComm'.

What is the solution?

Thank's alot,
Itay.
 
A

Adam Mitz [MSFT]

Use the provided (in vcclr.h) template gcroot<T> as the member of the unmanaged class. This allows the garbage collector to track the reference to the
managed object (so it can be updated if/when the GC decides to move the object).
See an example below.

Adam Mitz
Microsoft

#include <vcclr.h>
#using <mscorlib.dll>

using namespace System;

__nogc class UM{
public:
gcroot<Object*> ptr;
};

__gc class M{
public:
String* data;
void SetData(String* in_data){
data = in_data;
}
String* ToString(){
return data;
}
};

int _tmain()
{
M* m = new M;
m->SetData(S"Hello World!");
UM um;
um.ptr = m;
Console::WriteLine(um.ptr->ToString());
return 0;
}


--------------------
Reply-To: "Itay_k" <[email protected]>
From: "Itay_k" <[email protected]>
Subject: pointer to pointer to a managed class
Date: Mon, 28 Jul 2003 13:35:13 +0200
Lines: 18
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.3790.0
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Message-ID: <##[email protected]>
Newsgroups: microsoft.public.dotnet.languages.vc
NNTP-Posting-Host: 80.178.8.240.forward.012.net.il 80.178.8.240
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vc:26587
X-Tomcat-NG: microsoft.public.dotnet.languages.vc

Hello,

I want a member in my class that will save pointer to pointer to
System::Drawing::Image class.
When I write on my class code:

System::Drawing::Image **bmp;

I get this error message:
error C3160: 'bmp' : cannot declare interior __gc pointer or reference as a
member of 'RCClientNS::RCClientComm'.

What is the solution?

Thank's alot,
Itay.


--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 
I

Itay_k

Thanks you, but my class is managed.
I need to add this member (pointer to pointer) to a managed class, so why it
isn't working?

Itay.




Adam Mitz said:
Use the provided (in vcclr.h) template gcroot<T> as the member of the
unmanaged class. This allows the garbage collector to track the reference
to the
managed object (so it can be updated if/when the GC decides to move the object).
See an example below.

Adam Mitz
Microsoft

#include <vcclr.h>
#using <mscorlib.dll>

using namespace System;

__nogc class UM{
public:
gcroot<Object*> ptr;
};

__gc class M{
public:
String* data;
void SetData(String* in_data){
data = in_data;
}
String* ToString(){
return data;
}
};

int _tmain()
{
M* m = new M;
m->SetData(S"Hello World!");
UM um;
um.ptr = m;
Console::WriteLine(um.ptr->ToString());
return 0;
}
rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 
A

Adam Mitz [MSFT]

Sorry I misread your original post. It seems the 2nd level of indirection must be a __nogc* pointer. This compiles:

__gc class ImgPtr{
public:
System::Drawing::Image* __nogc* ppBmp;
};

int _tmain(){
ImgPtr* iptr = new ImgPtr;
*(iptr->ppBmp) = new System::Drawing::Bitmap(640, 480);
}

For more info, check out section 7.4 of the Managed Extensions for C++ Spec (Visual Studio help).
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vcmxspec/html/vcManagedExtensionsSpec_7_4.htm

Adam Mitz
Microsoft
--------------------
Reply-To: "Itay_k" <[email protected]>
From: "Itay_k" <[email protected]>
References: <##[email protected]> <[email protected]>
Subject: Re: pointer to pointer to a managed class
Date: Tue, 29 Jul 2003 22:12:47 +0200
Lines: 104
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.3790.0
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.languages.vc
NNTP-Posting-Host: 80.178.1.14.forward.012.net.il 80.178.1.14
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vc:26653
X-Tomcat-NG: microsoft.public.dotnet.languages.vc

Thanks you, but my class is managed.
I need to add this member (pointer to pointer) to a managed class, so why it
isn't working?

Itay.





unmanaged class. This allows the garbage collector to track the reference
to the
rights. Use of included script samples are subject to the terms specified at
message are best directed to the newsgroup/thread from which they
originated.


--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 
V

Vadym Stetsyak

While looking at the provided example question arises - is the reverse
possibel (to have/store/use a pointer to the unmanaged class in the managed
one)???

Adam Mitz said:
Use the provided (in vcclr.h) template gcroot<T> as the member of the
unmanaged class. This allows the garbage collector to track the reference
to the
managed object (so it can be updated if/when the GC decides to move the object).
See an example below.

Adam Mitz
Microsoft

#include <vcclr.h>
#using <mscorlib.dll>

using namespace System;

__nogc class UM{
public:
gcroot<Object*> ptr;
};

__gc class M{
public:
String* data;
void SetData(String* in_data){
data = in_data;
}
String* ToString(){
return data;
}
};

int _tmain()
{
M* m = new M;
m->SetData(S"Hello World!");
UM um;
um.ptr = m;
Console::WriteLine(um.ptr->ToString());
return 0;
}
rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 
B

Brandon Bray [MSFT]

Adam said:
Sorry I misread your original post. It seems the 2nd level of
indirection must be a __nogc* pointer. This compiles:

__gc class ImgPtr{
public:
System::Drawing::Image* __nogc* ppBmp;
};

int _tmain(){
ImgPtr* iptr = new ImgPtr;
*(iptr->ppBmp) = new System::Drawing::Bitmap(640, 480);
}

Of course, keep in mind that __nogc pointers to managed objects or other
__gc pointers is inherently dangerous. In order to ensure that the __nogc
pointer points to a valid object, the object it points to must be pinned. As
pinning can only be done by __pin pointers, which can only exist on the
stack, it is a really bad idea to store a __nogc pointer that points to a
managed object in a class or someother static or dynamic storage.

Rather than looking for a way to store a pointer to a pointer to a managed
class, I'd step back and ask why you want to do that? To do this correctly
with the CLR is a lot of work, and it will likely be better to just
rearchitect the program to avoid this necessity.

Cheerio!
 
A

Adam Mitz [MSFT]

Yes. Managed classes can have pointers to unmanaged classes as members. No special syntax is required.
--------------------
From: "Vadym Stetsyak" <[email protected]>
Subject: Re: pointer to pointer to a managed class
Date: Wed, 30 Jul 2003 22:20:43 +0300

While looking at the provided example question arises - is the reverse
possibel (to have/store/use a pointer to the unmanaged class in the managed
one)???


unmanaged class. This allows the garbage collector to track the reference
to the
rights. Use of included script samples are subject to the terms specified at
message are best directed to the newsgroup/thread from which they
originated.


--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top