473,770 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Destructor is not called when an exception is propagating from unmanaged to mixed code.

caa
I have made a simple text example. I have two projects. First is an
unmanaged static lib, containing function F(), which created an object
of type A and throws an exception. The second project is a C++/ME
console application that calls F() and catches all exceptions with a
"catch(...) " filter. The thing that I dont understand is why doesn't
the auto object created on stack get destroyed. See my sources:

Project 1. Unmanaged lib.
=============== =============== === Lib.cpp
#include "Lib.h"
#include <stdio.h>
#include <stdexcept>

namespace ns
{
class A
{
public:
A()
{
}

~A()
{
FILE * f = fopen( "c:/Temp/info.txt", "w+" );
fprintf( f, "destructor called!" );
fclose( f );
}
};

void F()
{
A a;
throw std::domain_err or( "Ahhh hell..." );
}
}

Project 2. Managed console app.
=============== =============== == consoleApp.cpp
#include "stdafx.h"
#include "../../VS2005_Unmanage d_Lib/VS2005_Unmanage d_Lib/Lib.h"

int main()
{
try
{
ns::F();
}
catch (...)
{
System::Console ::WriteLine( S"Caught it" );
}
}

When I run the console app I dont see no files beeing created. SO I
guess the destructor doesn't get called. Well... why? Just in case, I
m compiling with VS2005.

Jul 17 '07 #1
1 1526

<ca*@aurigma.co mwrote in message
news:11******** **************@ n60g2000hse.goo glegroups.com.. .
>I have made a simple text example. I have two projects. First is an
unmanaged static lib, containing function F(), which created an object
of type A and throws an exception. The second project is a C++/ME
console application that calls F() and catches all exceptions with a
"catch(...) " filter. The thing that I dont understand is why doesn't
the auto object created on stack get destroyed. See my sources:
What /EH option was the static library compiled with? /EHa is pretty much
required in a managed program.
>
Project 1. Unmanaged lib.
=============== =============== === Lib.cpp
#include "Lib.h"
#include <stdio.h>
#include <stdexcept>

namespace ns
{
class A
{
public:
A()
{
}

~A()
{
FILE * f = fopen( "c:/Temp/info.txt", "w+" );
fprintf( f, "destructor called!" );
fclose( f );
}
};

void F()
{
A a;
throw std::domain_err or( "Ahhh hell..." );
}
}

Project 2. Managed console app.
=============== =============== == consoleApp.cpp
#include "stdafx.h"
#include "../../VS2005_Unmanage d_Lib/VS2005_Unmanage d_Lib/Lib.h"

int main()
{
try
{
ns::F();
}
catch (...)
{
System::Console ::WriteLine( S"Caught it" );
}
}

When I run the console app I dont see no files beeing created. SO I
guess the destructor doesn't get called. Well... why? Just in case, I
m compiling with VS2005.

Jul 17 '07 #2

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

Similar topics

7
1899
by: Douglas Peterson | last post by:
Take a look at this code, it looks funny as its written to be as short as possible: -- code -- struct Base { ~Base() { *((char*)0) = 0; } }; struct Derived : public Base
11
5312
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 4-month old C#/MC++ .NET project project. I'd like to thank in advance anyone who takes the time to read and/or respond to this message. At a couple points, it may seem like a rant against C# / .NET, but we are pretty firmly stuck with this approach...
1
2769
by: Yoni Rabinovitch | last post by:
Hi, We have a large code base of "regular" C++ code (not MFC, COM or ATL). With VC6.0, we used to compile the "regular" C++ code as static libraries (.lib files), and then we would link the .lib files with an MFC GUI "front end". The "regular" C++ can also run as a standalone .EXE (with no GUI).
4
3810
by: Joe | last post by:
I am looking for the quintessential blueprint for how a C++ like destructor should be implemented in C#. I see all kinds of articles in print and on the web, but I see lots of discrepencies. For example ... C# Essentials (O'Reilly) - "Finalizers are class-only methods". Not sure what "class-only method" means in this context, but ususally I would assume that to mean it is static and would only be called once for the life of the...
5
1326
by: pronto | last post by:
Hello All. I've got some unexpected (to me) behavior. In C++, destructor of the class that wasn't created is not called, in C# some how I've got destructor call for class that wasn't initialized properly (by constructor). Please look examples. Is that a normal ? .. or garbage collector take care of all ? Am I doing something wrong ?
13
1473
by: Peter Hemmingsen | last post by:
Hi, I have written a dotnet class that in its constructor consumes a license from a central license pool. In the destructor it free the license again. But since I don't know when the destructor is called (that is up to the garbage collector) the object (and hence the application) consume a license although it is not in use any more. I could of course write a specific "logoff" method to run the specific part of the destructor code that...
35
3327
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 member. However, I set a breakpoint at the destructor of this instance's class and it was never called!!! I can see how it might not get called at a deterministic time. But NEVER? So, I guess I need to know the rules about destructors. I would...
14
3210
by: gurry | last post by:
Suppose there's a class A. There's another class called B which looks like this: class B { private: A a; public : B() { a.~A() } }
3
2426
by: Pixel.to.life | last post by:
All, A question on JIT debugging with VS2005. I have a managed app that builds great on one machine (Vista Home basic, VS2005, JIT enabled for managed/unmanaged code). I can also debug it on this machine with breakpoints and all.
0
9617
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
9453
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10099
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...
0
9904
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
8929
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
6710
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();...
1
4007
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
2
3607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
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.