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

Testing ScopeGuard from CUJ article: SG destructor not called on exceptions??

Hi,

I wrote a small test script to utilize the ScopeGuard class from the
Dec 2000 CUJ article.

I noted that my cleanup function was being called on normal block
exit, but not on calling a function I wrote to throw an exception.

#include <iostream>
#include <exception>
#include "ScopeGuard.h"

using namespace std;

void CloseFile(FILE* file)
{
cout << "Closing file" << endl;
fclose(file);
cout << "Closed file" << endl;
}

void Throw(void)
{
throw std::exception();
}

int testScopeGuard (void)
{
//Open a file that we don't intend to close
FILE* file = fopen("abc.txt", "a+");
ON_BLOCK_EXIT(CloseFile, file);
cout << "Guard made" << endl;
Throw();
}

When I comment out the Throw() call, the function CloseFile is called
and I do get a log like :
Guard made
Closing file
Closed file

However, when I leave the Throw() call in, I get a log like:
Guard made
Aborted (core dumped)

I am using g++ on cygwin :
bash-2.05b$ g++ --version
g++ (GCC) 3.3.1 (cygming special)

Does this mean that on encountering an exception, the ScopeGuard
object made by ON_BLOCK_EXIT is not being destroyed?

Regards,
Vikram
Jul 22 '05 #1
6 1611

"Vikram Paranjape" <us***********@spamgourmet.com> wrote in message
news:16**************************@posting.google.c om...
Hi,

I wrote a small test script to utilize the ScopeGuard class from the
Dec 2000 CUJ article.

I noted that my cleanup function was being called on normal block
exit, but not on calling a function I wrote to throw an exception.

#include <iostream>
#include <exception>
#include "ScopeGuard.h"

using namespace std;

void CloseFile(FILE* file)
{
cout << "Closing file" << endl;
fclose(file);
cout << "Closed file" << endl;
}

void Throw(void)
{
throw std::exception();
}

int testScopeGuard (void)
{
//Open a file that we don't intend to close
FILE* file = fopen("abc.txt", "a+");
ON_BLOCK_EXIT(CloseFile, file);
cout << "Guard made" << endl;
Throw();
}

When I comment out the Throw() call, the function CloseFile is called
and I do get a log like :
Guard made
Closing file
Closed file

However, when I leave the Throw() call in, I get a log like:
Guard made
Aborted (core dumped)

I am using g++ on cygwin :
bash-2.05b$ g++ --version
g++ (GCC) 3.3.1 (cygming special)

Does this mean that on encountering an exception, the ScopeGuard
object made by ON_BLOCK_EXIT is not being destroyed?


I think its much more likely that either your implementation has a bug, or
gcc 3.3.1 has a bug.

I suggest that you post a complete compilable example, this is the best way
to get help with code that doesn't work. See the FAQ
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8.

john
Jul 22 '05 #2

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...
I suggest that you post a complete compilable example, this is the best way to get help with code that doesn't work. See the FAQ
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8.


Sorry about that (should have proofread the post before clicking the send
button) :-)
I am using the Scopeguard.h file as is from the CUJ article.
The compilable code is as follows (I missed the main function in my last
post):

#include <iostream>
#include <exception>
#include "ScopeGuard.h"

using namespace std;

void CloseFile(FILE* file)
{
cout << "Closing file" << endl;
fclose(file);
cout << "Closed file" << endl;
}

void Throw(void)
{
throw std::exception();
}

int testScopeGuard (void)
{
//Open a file that we don't intend to close
FILE* file = fopen("abc.txt", "a+");
ON_BLOCK_EXIT(CloseFile, file);
cout << "Guard made" << endl;
//Throw();
}

int main (void)
{
return testScopeGuard();
}

Jul 22 '05 #3
* us***********@spamgourmet.com (Vikram Paranjape) schriebt:

I wrote a small test script to utilize the ScopeGuard class from the
Dec 2000 CUJ article.

I noted that my cleanup function was being called on normal block
exit, but not on calling a function I wrote to throw an exception.

#include <iostream>
#include <exception>
#include "ScopeGuard.h"

using namespace std;

void CloseFile(FILE* file)
{
cout << "Closing file" << endl;
fclose(file);
cout << "Closed file" << endl;
}

void Throw(void)
{
throw std::exception();
}

int testScopeGuard (void)
{
//Open a file that we don't intend to close
FILE* file = fopen("abc.txt", "a+");
ON_BLOCK_EXIT(CloseFile, file);
cout << "Guard made" << endl;
Throw();
Insert dummy 'return 0;' here to satisfy comp
}

When I comment out the Throw() call, the function CloseFile is called
and I do get a log like :
Guard made
Closing file
Closed file

However, when I leave the Throw() call in, I get a log like:
Guard made
Have you tried catching the exception in main?

It might be that the std::cout isn't flushed like it should be.
Aborted (core dumped)

I am using g++ on cygwin :
bash-2.05b$ g++ --version
g++ (GCC) 3.3.1 (cygming special)
With MingW g++ 3.2.3 and exception catching in main there is seemingly no
problem:
C:\...\test> g++ --version
g++ (GCC) 3.2.3 (mingw special 20030504-1)
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
C:\...\test> g++ -pedantic -ansi -o main.exe main.cpp

C:\...\test> main
Guard made
Closing file
Closed file
! St9exception

C:\...\test> _
It also compiles and runs fine with Visual C++ 7.1, when the appropriate
fix for the MSVC compiler bug is made in [ScopeGuard.h], namely
#ifdef _MSC_VER
#define ANONYMOUS_VARIABLE(str) CONCATENATE(str, __COUNTER__)
#else
#define ANONYMOUS_VARIABLE(str) CONCATENATE(str, __LINE__)
#endif

Does this mean that on encountering an exception, the ScopeGuard
object made by ON_BLOCK_EXIT is not being destroyed?


Probably not.

Try a debugger, try catching that exception in main.

Jul 22 '05 #4
* al***@start.no (Alf P. Steinbach) schriebt:

Have you tried catching the exception in main?


Sorry, I didn't think: without catching that exception somewhere you
are _not_ guaranteed anything other than possibly a call to std::terminate;
in particular, you're not guaranteed stack cleanup.

Jul 22 '05 #5
Works fine on my copy of gcc 3.3.1 (also cygwin).

john
Jul 22 '05 #6

"Alf P. Steinbach" <al***@start.no> wrote in message
news:40****************@news.individual.net...
* al***@start.no (Alf P. Steinbach) schriebt:

Have you tried catching the exception in main?
Sorry, I didn't think: without catching that exception somewhere you
are _not_ guaranteed anything other than possibly a call to

std::terminate; in particular, you're not guaranteed stack cleanup.


Now that makes sense to me. I wrote both versions of main 1. without the
enclosing try-catch block and 2. with the said block
and found that the version with the try catch block was correctly calling
the cleanup code.

So, I guess, the moral is ScopeGuard alone isn't really the silver bullet
for handling cleanup in the presence of exceptions.
It is extremely useful however, if the main() is enclosed in a try catch
block (and in a multithreaded environment, the entry function for each
thread is enclosed in a try catch block). ScopeGuard is still very useful,
though, becuase you can now be sure exceptions won't leave the threads in an
unpredictable state at each branch in the code where temporaries are being
created (if you used the appropriate SG functionality).

I guess I missed the necessity of the enclosing try..catch block in the CUJ
article.

Thanks a lot , John and Alf. for your help.

Regards,
Vikram
Jul 22 '05 #7

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

Similar topics

16
by: Timothy Madden | last post by:
Hy I have destructors that do some functional work in the program flow. The problem is destructors should only be used for clean-up, because exceptions might rise at any time, and destructors...
13
by: Radde | last post by:
HI, Do anyone have scenrio where destructor throws execption which will lead to memory leak..Does this make sense.. Cheers up..
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() } }
17
by: Phlip | last post by:
C++ers: I have this friend who thinks C++ cannot throw from a destructor. I think throwing from a destructor is bad Karma, and should be designed around, but that C++ cannot strictly prevent...
4
by: Subra | last post by:
Hi, I am learning C++ and need export advice on the below program. I have read it that whenever a exception is genrated and control enteres the catch block it will call destructors for all the...
1
by: caa | last post by:
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.