473,320 Members | 1,955 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,320 software developers and data experts.

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_error( "Ahhh hell..." );
}
}

Project 2. Managed console app.
================================ consoleApp.cpp
#include "stdafx.h"
#include "../../VS2005_Unmanaged_Lib/VS2005_Unmanaged_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 1484

<ca*@aurigma.comwrote in message
news:11**********************@n60g2000hse.googlegr oups.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_error( "Ahhh hell..." );
}
}

Project 2. Managed console app.
================================ consoleApp.cpp
#include "stdafx.h"
#include "../../VS2005_Unmanaged_Lib/VS2005_Unmanaged_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
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
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...
1
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...
4
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...
5
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...
13
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...
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...
14
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
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.