M
Maxwell
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
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