473,811 Members | 3,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

gcroot use and clean up?

Hello,

Im using MC++ VS.NET 2003 and am quite confused with with gcroot
template and its use. The issue I am confused about is the need to (or
not) delete a pointer to a managed object that you have created using
gcroot. For example(from Managed VC++ 2003 Step by Step by Microsoft
Press p510):

class UnmanagedClass
{
public:
gcroot<ManagedC lass *>pMc;

UnmanagedClass( ManagedClass *)
{
pMc = in;
}

~ UnmanagedClass( )
{
// do you need this????
delete pMc; //this was NOT in the microsoft sample
}
}

In MS documetion I get the impression that you would not have to
explicitly call delete like the code above because they didn't in thier
example and stated "When the unmanaged object goes out of scope, the
gcroot is destroyed, which frees the GCHandle and in turn frees up the
managed object". So by reading that I would infer (perhaps incorrectly)
one does not have to explicitly call delete . Can anyone confirm if
this a correct?

However, when I read the other posts in this newsgroup I get the
distinct impression that one should definitely call delete
explicitly...Is this a VS 2003 vs 2005 thing, I do see references in
this newsgroup that in c++/CLI there is this auto_gcroot templete which
I cannot find in VS 2003?

Also one other question I have if you use gcroot like above would one
have to "pin " the managed object before passing it into this unmanaged
constructor or is that the hole point of using gcroot?

Thanks

Nov 23 '05 #1
2 8531
Hi Maxwell

Q1: Do you have to delete objects passed to gcroot?
A1: gcroot does not internally delete objects for you, so if your problem
domain requires the object to be deleted, you have to do it manually
In VC 2005, there is an msclr::auto_gcr oot from the header
msclr/auto_gcroot.h for exactly that purpose

Q2: Is it necessary to pin objects before they are passed to gcroot?
A2: No. gcroot is a native type, but all its functions are managed
functions. Therefore you do not pass a gc objcect to native code

HTH

Marcus Heege
"Maxwell" <rb*@dslextreme .com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Hello,

Im using MC++ VS.NET 2003 and am quite confused with with gcroot
template and its use. The issue I am confused about is the need to (or
not) delete a pointer to a managed object that you have created using
gcroot. For example(from Managed VC++ 2003 Step by Step by Microsoft
Press p510):

class UnmanagedClass
{
public:
gcroot<ManagedC lass *>pMc;

UnmanagedClass( ManagedClass *)
{
pMc = in;
}

~ UnmanagedClass( )
{
// do you need this????
delete pMc; //this was NOT in the microsoft sample
}
}

In MS documetion I get the impression that you would not have to
explicitly call delete like the code above because they didn't in thier
example and stated "When the unmanaged object goes out of scope, the
gcroot is destroyed, which frees the GCHandle and in turn frees up the
managed object". So by reading that I would infer (perhaps incorrectly)
one does not have to explicitly call delete . Can anyone confirm if
this a correct?

However, when I read the other posts in this newsgroup I get the
distinct impression that one should definitely call delete
explicitly...Is this a VS 2003 vs 2005 thing, I do see references in
this newsgroup that in c++/CLI there is this auto_gcroot templete which
I cannot find in VS 2003?

Also one other question I have if you use gcroot like above would one
have to "pin " the managed object before passing it into this unmanaged
constructor or is that the hole point of using gcroot?

Thanks

Nov 24 '05 #2
Marcus thanks much for the reply , the information helps alot. I dont
think I asked the question correctly for Q2... or did not supply all
the relevant info.

In the sample above, we have the class UmanagedClass:

///Unmanaged code
class UnmanagedClass
{
public:
gcroot<ManagedC lass *>pMc;
UnmanagedClass( ManagedClass *in)
{
pMc = in;
}
~ UnmanagedClass( )
{
// do you need this????
delete pMc; //this was NOT in the microsoft sample
}
}

On the mananged code side we have 2 objects ManagedClass and
ManagedComposit eClass. To create the unmanged object in the
ManagedComposit eClass I would have to supply a managed object something
like

//Managed code
__gc class ManagedClass
{
ManagedClass()
{
}
ManagedClass()
{
}
void doSomething()
{
Console::WriteL ine("Hello world");
};
}

__gc class ManagedComposit eClass //Managed code
{
ManagedComposit eClass()
{
ManagedClass mClass = new ManagedClass();
UnmanageClass *pUc = new UnmanagedClass( mClass);
}
ManagedComposit eClass()
{
delete *pUc;
}

}

Since I am passing in a managed class to the UnmanagedClass would I
have to worry about garabage collection of the managed class or is that
what the gcroot is for in the unmanaged class?

For example should the code be like this instead in order to be safe:

__gc class ManagedClass //Managed code
{
ManagedComposit eClass()
{
ManagedClass mClass = new ManagedClass();
ManagedClass __pin *pinnedMClass = mClass;
UnmanageClass *pUc = new UnmanagedClass( this);
}
ManagedComposit eClass()
{
pinnedMClass=0;
delete *pUc;
}

}

Now the code Aobve for the pinned stuff doesnt seeem to make sense to
me, but I wanted to make sure I didn't have to "Pin" a managed object
before passing it to a unmanaged object and its starts calling stuff on
that managed object.

I think Myabe I just dont understand when you pin and when you dont or
how gcroot falls in there....I see that gcroot allows you to use a
managed object in unmanaged code. That much I get, but when using that
managed object in the example I provided where you have to pass it in a
constructor of a unmanaged object, should you pin it first?

Nov 25 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1623
by: igal.ioffe | last post by:
Hi All, I've run into a problem, converting a native project into mixed mode, where usage of gcroot point in mixed mode causes a fatal CLR engine exception. Here is a tiny code extract: ------------------ mixeddll.cpp ---------- #include <vcclr.h>
2
2740
by: Steve McLellan | last post by:
Hi, I see that GCHandle can encapsulate a weak reference, but gcroot appears to only allow 'normal' GCHandles. Is this correct or is there something I've missed? Ta, Steve
5
2641
by: Gerhard Menzl | last post by:
Has anyone ever tried to sort a Standard Library container of gcroots? I have run into the problem that somewhere deep in the Library logic (in line 338 of <memory>, to be precise) the destructor of a class _Temp_iterator tries to obtain the address of a gcroot, but fails because gcroot::operator& is private. -- Gerhard Menzl #dogma int main ()
2
2010
by: Dave | last post by:
I have a few C# .NET components which might implement one or several interfaces, let say I1, I2 and I3. The unmanaged C++ client consumes this component using gcroot template pointer. How client application can find out what interfaces implement certain instance of the pointer? I need some kind of QueryInterface method which allows me to query for certain interface. How can I do that if I use gcroot template? Thanks.
0
1487
by: Rob Haynes | last post by:
I have a DLL with vanilla C functions implemented. I'd like these functions to become wrappers for a managed object. So, I was thinking that the first call would save a pointer statically to the managed object that future calls could use. I found this: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmex/html/vcconconvertingmanagedextensionsforcprojectsfrompureintermediatelanguagetomixedmode.asp and have implemented it...
4
4562
by: John | last post by:
I'm having a major problem trying to use value types like System::Drawing::Rectangle with std::vector. Is it possible to use STL containers with these type of objects, or am I just doing something wrong? Thanks! using namespace System::Drawing; #include <vector>
6
7003
by: =?Utf-8?B?QWw=?= | last post by:
I am storing an array of strings in an unmanaged MFC class using gcroot as follows: gcroot<array<System::String^>^m_pArr; Nothing out of the ordinary there. However, if I try to use "delete m_pArr" in the code in order to deterministically delete it, I get the following error in VS05: error C2440: 'delete' : cannot convert from 'gcroot<T>' to 'void *' If I use the same with something other than an array i.e. using gcroot and
6
4599
by: =?Utf-8?B?RmFiaWFu?= | last post by:
Hello, I have a class hierarchy distributed over 3 native C++ dlls. The base class has a .NET Windows.Form for status output via a gcroot<>. The gcroot is declared private - the sub classes only have access via a protected "print"-method. I need the different dlls as the sub classes implement the base class's pure virtual methods using different technologies. To use the native classes from outside their dlls I use the...
6
6193
by: Bob Altman | last post by:
Hi all, In C++/CLI (VS 2005) I have a C++ module compiled with /clr that contains a module-level gcroot variable: static gcroot<MyList^m_myList = gcnew MyList; In the above statement, MyList is a managed class that inherits from Generic::List. I want to iterate through m_myList like this:
0
9731
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10136
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6893
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5697
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3020
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.