Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2006

Tip: Looking for answers? Try searching our database.

How to store a pointer to a ref class (C++/CLI newbie)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ivan Vecerina - 29 Jun 2006 00:05 GMT
We are trying to use a .NET form ("Form") from a non-managed C++
class ("Engine").
The Engine class needs to create the form once (e.g. at construction),
then will be displaying the Form and running a modal loop a few
times. At destruction, the form will be closed and disposed of.

So I wanted to store a kind of pointer or reference to the
garbage-collected Form within the Engine class.

How do we achieve that?
The following naive solution is rejected by the compiler:

using MyNetTest::Form; // a .NET form generated using the Designer

class Engine {
public:
  //...

private:
   Form^  dlg;  //ERROR C3265
};

Error message:
error C3265: cannot declare a managed 'dlg' in an unmanaged 'Engine'
may not declare a global or static variable, or a member of a native
 type that refers to objects in the gc heap

There has to be some kind of built-in smart pointer/wrapper
that I can use to make Form a member of Engine, right?
What is the solution?

Thanks,
Ivan
Tamas Demjen - 29 Jun 2006 01:39 GMT
> class Engine {
> public:
[quoted text clipped - 3 lines]
>     Form^  dlg;  //ERROR C3265
> };

The solution is to use the gcroot template, which is a wrapper around a
low level GC handle:

class Engine {
public:
    //...

  private:
     gcroot<Form^>  dlg;
};

Note that gcroot requires manual deletion, it's like an unprotected
pointer that can leak:

Engine::~Engine() { delete dlg; }

Therefore I recommend that you use auto_gcroot, which is a smart pointer
around gcroot, then you don't have to worry about manual deletion:

  private:
     auto_gcroot<Form^>  dlg;

You have to #include <msclr\auto_gcroot.h> in order to use this class.

Tom
Ivan Vecerina - 29 Jun 2006 02:12 GMT
: > class Engine {
: > public:
[quoted text clipped - 3 lines]
: >     Form^  dlg;  //ERROR C3265
: > };
...
:   private:
:      auto_gcroot<Form^>  dlg;
:
: You have to #include <msclr\auto_gcroot.h> in order to use this class.

Cool. Googling for auto_gcroot brought other useful references.
 http://weblogs.asp.net/kennykerr/archive/2005/07/12/419102.aspx

Thank you !
Ivan

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.