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

Unexpected stack unwinding sequence in mixed C++ -- can anyone comment?

Recently I ran into a problem that I traced to something which completely
surprised me: the relative order of stack unwinding and finally block
execution in mixed C++ appears to vary depending on how execution leaves the
corresponding try block.

Consider the following abbreviated code:

// some unmanaged class
__nogc class Foo
{
public:
~Foo();
};

// some managed function
void func()
{
Foo f;

try
{
// something
if ( condition1 )
{
throw __gc new Exception();
}
else if ( condition2 )
{
return;
}
}
__finally
{
// clean up
}
}
Given the scope of "f", I would expect the code in the __finally block to
execute before f's destructor is called. That's exactly what happens if the
code in the try block either runs through to the end, or throws an
exception. But if the "return" statement in the try block is executed, this
order is reversed, and ~Foo() runs *before* the __finally block. I did not
expect this!

Looking at the generated IL, this appears to be deliberate -- the "return"
is compiled as an explicit call tothe dtor followed by a "leave". But this
doesn't seem valid -- "f" is in scope when the __finally code executes, so
shouldn't it be guranteed to be valid (non-destructed) at that point? As it
is, __the finally code can easily access and use a destructed (though
presumably not deallocated) object, which shouldn't be possible. Unless it's
documented as a special case, it seem flat-out wrong.

The behavior is the same in Managed C++ (CLR 1.1) and the latest Whidbey
C++/CLI RC. I can't find any documentation addressing this. If anyone has
any insights or thoughts, please share them. I'm curious if this was done
for a good reason, and if it is documented anywhere, how it's justified,
etc.

Thanks


Nov 17 '05 #1
0 950

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

Similar topics

14
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not...
22
by: MSG | last post by:
Hello void f1(int n) { vector<int> x(n); /* C++ */ } void f2(int n) { int x; /* C99 only */ } void f3(int n) { int* x = new int; /* C++ */ delete x; }
5
by: Senthilvel | last post by:
Hi, The automatic variables declared in the try bock will be destroyed when an exception is thrown due to stack unwinding. So i expected the following code to call the destructor of B but it...
6
by: John Ratliff | last post by:
What happens to objects created on the stack when you throw an exception? For example, suppose I opened a file for reading with an fstream. If I perform a read operation and I don't like the...
5
by: Tom Lam lemontea | last post by:
Hi all, This is my very first post here, I've seriously tried some programming on C, and shown below is my very first program(So you can expect it to be very messy) that I wrote after I've learned...
18
by: Peteroid | last post by:
Apparently there is a limit to how complex a single header file with a huge class definition in it can be. The source to one of my header files is at a point that it compiles and runs fine, until I...
8
by: Edward Diener | last post by:
By reuse, I mean a function in an assembly which is called in another assembly. By a mixed-mode function I mean a function whose signature has one or more CLR types and one or more non-CLR...
5
by: DC | last post by:
Hi all, First of all, let me asure you you will not be doing my homework for me if you are good enough to reply - I have been a professional programmer for a couple of years now, I just never...
4
by: Tony | last post by:
Well the subject says it all. Do I have to have it if I use classes in C++? Tony
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
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
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
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...
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,...
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.