473,385 Members | 1,642 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 cleanup/destruction question (.net 2003)

> class UnmanagedClass
> {
> public:
> gcroot<ManagedClass *>pMc;
>
> UnmanagedClass(ManagedClass * in)
> {
> pMc = in;
> }
>
> ~ UnmanagedClass()
> {
> delete pMc;
> }

I took this from another post, but I have a question on a very similar topic. When I try to run the following code, and call the destructor on UnmanagedClass, it tells me that there is no destructor defined for ManagedClass. (I'm substituting System::XML::XmlDocument for ManagedClass)

I am having a memory leak because I am unable to destroy the managed object created in the unmanaged class. Any help would be GREATLY appreciated. Thanks!!!!
Feb 1 '07 #1
5 2435
I should say, I think the problem comes from the fact that I'm not destroying the gcroot pointer, which has a reference to the managed object, and so the managed object is persisting in memory, even when the unmanaged class goes out of scope. Thanks again !!

> class UnmanagedClass
> {
> public:
> gcroot<ManagedClass *>pMc;
>
> UnmanagedClass(ManagedClass * in)
> {
> pMc = in;
> }
>
> ~ UnmanagedClass()
> {
> delete pMc;
> }

I took this from another post, but I have a question on a very similar topic. When I try to run the following code, and call the destructor on UnmanagedClass, it tells me that there is no destructor defined for ManagedClass. (I'm substituting System::XML::XmlDocument for ManagedClass)

I am having a memory leak because I am unable to destroy the managed object created in the unmanaged class. Any help would be GREATLY appreciated. Thanks!!!!
Feb 1 '07 #2
Ganon11
3,652 Expert 2GB
It doesn't look like pMc is actually a pointer, but some class holding a pointer - you may consider writing a deconstructor for gcroot.
Feb 1 '07 #3
Thank you very much for your quick reply!!

Yes, you are correct in that the gcroot object holds a pointer, and isn't a pointer itself. However, in gcroot.h (visual c++ code from microsoft), there is already a destructor defined for gcroot objects. However, when I I try to compile with the call:

delete pMc;

The compiler gives an error saying "there is no destructor for managed class ManagedClass"

Does anyone have any ideas as to why I'm getting that message, and any possible solutions to fix the leak that I am encountering? Thanks!
Feb 1 '07 #4
Ganon11
3,652 Expert 2GB
Is ManagedClass a class that you are writing and modifying? If so, is there a deconstructor for it?

Your gcroot pointer's deconstructor may be trying to delete the ManagedClass pointer it has, but is unable to because managedClass has no deconstructor.
Feb 1 '07 #5
ManagedClass is a visual studio .net 2003 class (System::Xml::XmlDocument to be exact), and there is no deconstructor for that type.

Normally the resources are freed up by the garbage collector for that type. However, I am using gcroot, in unmanaged code (on the native heap, outside the scope of the garbage collector), to get a pointer to a location on the managed heap, so I can instantiate a managed object.

The problem is that when the unmanaged class that contains the gcroot pointer is destroyed, there is some memory that is being leaked, presumably that which the gcroot pointer was pointing to, and I'm not sure how to remedy the problem. I'm almost certain it has something to do with the inability to delete the gcroot pointer.
Feb 1 '07 #6

Sign in to post your reply or Sign up for a free account.

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:...
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...
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...
2
by: Maxwell | last post by:
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...
69
by: MQ | last post by:
Hi all I am just wondering how most people implement cleanup in C functions. In particular, if the function opens a number of resources, these need to be released properly should an error occur...
2
by: rabidus | last post by:
> class UnmanagedClass > { > public: > gcroot<System::Xml::XmlDocument *>pMc; > > UnmanagedClass(System::Xml::XmlDocument * in) > { > pMc = in; > } >
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,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.