473,789 Members | 2,706 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

gcroot cleanup/destruction question (.net 2003)

6 New Member
> class UnmanagedClass
> {
> public:
> gcroot<ManagedC lass *>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::Xm lDocument 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 2464
rabidus
6 New Member
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<ManagedC lass *>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::Xm lDocument 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 Recognized Expert Specialist
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
rabidus
6 New Member
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 Recognized Expert Specialist
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
rabidus
6 New Member
ManagedClass is a visual studio .net 2003 class (System::Xml::X mlDocument 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
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>
5
2639
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 ()
0
1486
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...
2
8531
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 have created using gcroot. For example(from Managed VC++ 2003 Step by Step by Microsoft Press p510): class UnmanagedClass {
69
3253
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 at any point in the function (as well as at the end if successful). C++ has exceptions, the only way I can see to do this neatly in C is to use goto statements. Is my method of implementing cleanup good, or are their better ways. Here is an...
2
1988
by: rabidus | last post by:
> class UnmanagedClass > { > public: > gcroot<System::Xml::XmlDocument *>pMc; > > UnmanagedClass(System::Xml::XmlDocument * in) > { > pMc = in; > } >
6
7001
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
4598
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
6190
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
9666
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
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9984
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...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
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
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
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.