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

Dipose vs Destructor

I have a C++ CLI Managed DLL project which is consumed by C# clients.

What is the recommended way for ensuring proper clean up of managed
resources and native resources in a managed C++ class?

thanks

ScottM

May 17 '07 #1
2 2529
Hi Scott!
I have a C++ CLI Managed DLL project which is consumed by C# clients.

What is the recommended way for ensuring proper clean up of managed
resources and native resources in a managed C++ class?
The same as for C# assemblies: IDisposable

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
May 18 '07 #2
Jochen Kalmbach [MVP] wrote:
Hi Scott!
>I have a C++ CLI Managed DLL project which is consumed by C# clients.

What is the recommended way for ensuring proper clean up of managed
resources and native resources in a managed C++ class?

The same as for C# assemblies: IDisposable
Except that C++/CLI doesn't permit you to implement IDisposable. To quote
from (an early draft of) the C++/CLI specification (8.8.8/25):

C++/CLI implements the destructor and finalizer semantics in any ref class T
by using the CLI dispose pattern, which makes use of five functions
(Dispose(), Dispose(bool), Finalize(), __identifier("~T")(), and
__identifier("!T")()), all of whose definitions are generated by the
compiler, as required. These cleanup mechanisms are hidden from the C++/CLI
programmer.

In C++/CLI, the proper way to do cleanup is to place all of the cleanup code
in the destructor and finalizer, as follows:
.. The finalizer should clean up any resources that are represented by value
types ([CD]: e.g. HANDLEs to native resources).
.. The destructor should do the maximal cleanup possible. To facilitate this,
the programmer should call the finalizer from the destructor and write any
other cleanup code in the destructor. A destructor can safely access the
state of ref classes with references from the object, whereas a finalizer
cannot.

For ref classes, both the finalizer and destructor must be written so they
can be executed multiple times and on objects that have not been fully
constructed.

-cd
May 19 '07 #3

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

Similar topics

52
by: Newsnet Customer | last post by:
Hi, Statement 1: "A dynamically created local object will call it's destructor method when it goes out of scope when a procedure returms" Agree. Statement 2: "A dynamically created object...
11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
35
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a...
11
by: AB | last post by:
Hi All, I've got an array of objects, during the execution of the program I'd like to assign a particular object to a certain element in the object array. The sample code's like this... class...
23
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
8
by: gw7rib | last post by:
I've been bitten twice now by the same bug, and so I thought I would draw it to people's attention to try to save others the problems I've had. The bug arises when you copy code from a destructor...
7
by: sam | last post by:
Hi, See when i reading a sourcecode of a program, I read that the constructor is ordinary and after that the programmer has written virtual destructor for that constructor . Why we use the...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.