473,405 Members | 2,154 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,405 software developers and data experts.

Unmanaged pointer cleanup in Managed class

Hello,

Newbie question here for disposing of unmanaged resources in MC++.NET.
I have a managed VS.NET 2003 MC++ wrapper class that wraps a unmanaged
C++ dll. What I am trying to figure out is what is the "best practice"
for disposing of pointers to unmanaged classes that you have newed in
your constructor in MC++

For a better description of what is the standard affair I have tried
looking online at:
http://msdn.microsoft.com/library/de...estructors.asp

but I would have to say its very lacking in detail or explanation. Does
anyone know of a better reference or can point me to for this?

My constructor looks something like this:

MClass::MClass()
{
pUnmanagedClass_ = new UnmanagedLib::UClass(); //unmanaged heap
pBridgeClass_ = new _UClassBridge(); //unmanaged heap
pUnmanagedClass_->RegisterUnmanagedCallBack(&(pBridgeClass_->UnmanagedBridgeCallback));
}

pUnanagedClass_ is a pointer to class in a unmanaged C++ DLL
pBridgeClass_ is a pointer to a __nogc class I created in MC++

Here is my destructor for this class:
MClass::~MClass()
{
pUnmanagedClass_->UnRegisterCallbacks();
pUnmanagedClass_->~UClass(); ///do I need this line

delete pUnmanagedClass_; //because it was newed
delete pBridgeClass_; //because it was newed
}

Since I newed the pointers I would guess that I would have to
explicitly call delete on them...is this the case? I also need to
ensure that destrutor gets called in the C++ DLL immediately
(pUnmanagedClass) so I added a line for this in the managed class
destructor...is this necessary or is it safe to assume it will get
called because the delete is called

Thanks

Nov 20 '05 #1
4 2041
in your destructor/finalizer, don't call the ~destructor, call delete

"Maxwell" <rb*@dslextreme.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hello,

Newbie question here for disposing of unmanaged resources in MC++.NET.
I have a managed VS.NET 2003 MC++ wrapper class that wraps a unmanaged
C++ dll. What I am trying to figure out is what is the "best practice"
for disposing of pointers to unmanaged classes that you have newed in
your constructor in MC++

For a better description of what is the standard affair I have tried
looking online at:
http://msdn.microsoft.com/library/de...estructors.asp

but I would have to say its very lacking in detail or explanation. Does
anyone know of a better reference or can point me to for this?

My constructor looks something like this:

MClass::MClass()
{
pUnmanagedClass_ = new UnmanagedLib::UClass(); //unmanaged heap
pBridgeClass_ = new _UClassBridge(); //unmanaged heap
pUnmanagedClass_->RegisterUnmanagedCallBack(&(pBridgeClass_->UnmanagedBridgeCallback));
}

pUnanagedClass_ is a pointer to class in a unmanaged C++ DLL
pBridgeClass_ is a pointer to a __nogc class I created in MC++

Here is my destructor for this class:
MClass::~MClass()
{
pUnmanagedClass_->UnRegisterCallbacks();
pUnmanagedClass_->~UClass(); ///do I need this line

delete pUnmanagedClass_; //because it was newed
delete pBridgeClass_; //because it was newed
}

Since I newed the pointers I would guess that I would have to
explicitly call delete on them...is this the case? I also need to
ensure that destrutor gets called in the C++ DLL immediately
(pUnmanagedClass) so I added a line for this in the managed class
destructor...is this necessary or is it safe to assume it will get
called because the delete is called

Thanks

Nov 21 '05 #2
JAL
Maxwell.. I am also new to C++/cli, but I notice that you are doing _two_
unmanaged allocations in the constructor. So if an exception is thrown
_after_ a first successful allocation, the object never exists and the
destructor is never called. So I think the unmanaged allocations in the
constructor need to be all or none, sort of like a transaction. If any part
of the construction fails, then things are rolled back to the "initial state"
and then rethrow the exception.

Hopefully, someone will chime in on this concern.

"Maxwell" wrote:
Hello,

Newbie question here for disposing of unmanaged resources in MC++.NET.
I have a managed VS.NET 2003 MC++ wrapper class that wraps a unmanaged
C++ dll. What I am trying to figure out is what is the "best practice"
for disposing of pointers to unmanaged classes that you have newed in
your constructor in MC++

For a better description of what is the standard affair I have tried
looking online at:
http://msdn.microsoft.com/library/de...estructors.asp

but I would have to say its very lacking in detail or explanation. Does
anyone know of a better reference or can point me to for this?

My constructor looks something like this:

MClass::MClass()
{
pUnmanagedClass_ = new UnmanagedLib::UClass(); //unmanaged heap
pBridgeClass_ = new _UClassBridge(); //unmanaged heap
pUnmanagedClass_->RegisterUnmanagedCallBack(&(pBridgeClass_->UnmanagedBridgeCallback));
}

pUnanagedClass_ is a pointer to class in a unmanaged C++ DLL
pBridgeClass_ is a pointer to a __nogc class I created in MC++

Here is my destructor for this class:
MClass::~MClass()
{
pUnmanagedClass_->UnRegisterCallbacks();
pUnmanagedClass_->~UClass(); ///do I need this line

delete pUnmanagedClass_; //because it was newed
delete pBridgeClass_; //because it was newed
}

Since I newed the pointers I would guess that I would have to
explicitly call delete on them...is this the case? I also need to
ensure that destrutor gets called in the C++ DLL immediately
(pUnmanagedClass) so I added a line for this in the managed class
destructor...is this necessary or is it safe to assume it will get
called because the delete is called

Thanks

Nov 21 '05 #3
Maxwell wrote:
Hello,

Newbie question here for disposing of unmanaged resources in MC++.NET.
I have a managed VS.NET 2003 MC++ wrapper class that wraps a unmanaged
C++ dll. What I am trying to figure out is what is the "best practice"
for disposing of pointers to unmanaged classes that you have newed in
your constructor in MC++

For a better description of what is the standard affair I have tried
looking online at:
http://msdn.microsoft.com/library/de...estructors.asp

but I would have to say its very lacking in detail or explanation.
Does
anyone know of a better reference or can point me to for this?

My constructor looks something like this:

MClass::MClass()
{
pUnmanagedClass_ = new UnmanagedLib::UClass(); //unmanaged heap
pBridgeClass_ = new _UClassBridge(); //unmanaged heap
pUnmanagedClass_->RegisterUnmanagedCallBack(&(pBridgeClass_->UnmanagedBridgeCallback));
}

pUnanagedClass_ is a pointer to class in a unmanaged C++ DLL
pBridgeClass_ is a pointer to a __nogc class I created in MC++

Here is my destructor for this class:
MClass::~MClass()
{
pUnmanagedClass_->UnRegisterCallbacks();
pUnmanagedClass_->~UClass(); ///do I need this line

delete pUnmanagedClass_; //because it was newed
delete pBridgeClass_; //because it was newed
}

Since I newed the pointers I would guess that I would have to
explicitly call delete on them...is this the case? I also need to
ensure that destrutor gets called in the C++ DLL immediately
(pUnmanagedClass) so I added a line for this in the managed class
destructor...is this necessary or is it safe to assume it will get
called because the delete is called


A couple points:

1. You don't need to explicitly call the destructor - the delete expression
will take care of that (in fact, you need to NOT call it, since otherwise
it'll be called twice and classes are not generally expected to handle
having their destructor run twice).

2. If the unmanaged classes are holding important resources (other than just
unmanaged heap), you should consider making your managed class IDisposable
and disposing of the unmanaged class in your IDisposable::Dispose. The
~MClass "destructor" is the finalizer under MC++, and there's no guarantee
that a finalizer will ever run (since there's no guarantee that the GC will
ever run).

3. As another poster pointed out, you're doing multiple allocations in your
constructor with no try/catch protection. You have potential resource leaks
if an exception is thrown in your constructor. If you need to make it
bullet proof, I'd suggest picking up a copy of Herb Sutter's excellent book
"Exceptional C++" - about 1/2 of the book is about building exception
safety.

-cd
Nov 21 '05 #4
Wow that was great info, That definintely helped tons. Thanks for
pointing out the constructor issue. I was just assuming like in C# if
there was a exception in the constructor the class would not be created
and thus anything that was created in the constructor would be
destroyed automagically...guess not with MC++ because that was
unmanaged code which would make sense.

I'll definintely check out that book,

Thanks again for the replies

Nov 21 '05 #5

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

Similar topics

15
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that...
8
by: Don | last post by:
I have a third party C++ DLL that I am trying to use from C#. The specific function I am trying to use is declared in C++ as follows: ladybugConvertToMultipleBGRU32( LadybugContext ...
7
by: Lev | last post by:
Hi, I have an unmanaged pointer to a class that I want to hold in a managed class. I pass the pointer (from unmanaged code) in the constructor of the managed class, and at that point it has...
2
by: Brett Styles | last post by:
Hi Guys, I am trying to access a class in an unmanaged dll. I have created a wrapper managed class to access the functions I need but no matter what I try from the MSDN samples I can not get it to...
3
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
1
by: Sparhawk | last post by:
Hi, my company is going to migrate a large VC++ application to .NET to make use of Windows Forms (the old class library is not updated any more). We are not planning to migrate the rest of the...
3
by: Thorsten | last post by:
HI I'm a C# developer and unfortunately I have to write now some code in managed and unmanaged C++. In this area I'm Newbie and therefore please forgive me if this is a really simple...
12
by: DaTurk | last post by:
Hi, I have a rather interesting problem. I have a unmanged c++ class which needs to communicate information to managed c++ via callbacks, with a layer of c# on top of the managed c++ ultimatley...
3
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
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
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,...
0
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...
0
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...
0
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,...

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.