473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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<ManagedClass *>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 8488
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_gcroot 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.googlegr oups.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<ManagedClass *>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<ManagedClass *>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
ManagedCompositeClass. To create the unmanged object in the
ManagedCompositeClass I would have to supply a managed object something
like

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

__gc class ManagedCompositeClass //Managed code
{
ManagedCompositeClass()
{
ManagedClass mClass = new ManagedClass();
UnmanageClass *pUc = new UnmanagedClass(mClass);
}
ManagedCompositeClass()
{
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
{
ManagedCompositeClass()
{
ManagedClass mClass = new ManagedClass();
ManagedClass __pin *pinnedMClass = mClass;
UnmanageClass *pUc = new UnmanagedClass(this);
}
ManagedCompositeClass()
{
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
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:...
2
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
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...
2
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...
0
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...
4
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...
6
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...
6
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...
6
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...

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.