473,796 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exception safety in member function wrapper

I have always found Stroustrup's paper on generalized member function
wrapper (http://www.research.att.com/~bs/wrapper.pdf) an interesting
one. Recently I started to play with it. As I tried to put some extra
data members (such as a std::vector) to the Call_proxy<clas s template
it started to worry me because if the suffix in the destructor throws,
there can be a memory leak. (See relevant code below)

The Call_proxy<uses its own destructor to facilitate a call to a
suffix at the end of a statement (as an instance of Call_proxy<is
always a temporary.) This isn't a problem in the proposal in the paper
because the original version of the class template has no data member
that requires non-trivial destruction.

I am wondering if anyone has an idea as to how to improve the exception
safety. The requirements are:

1. If suffix() call throws, data members in Call_proxy<are properly
destroyed.

2. The exception thrown is to be catched by the caller (i.e. the
destructor must be allowed to throw.

RELEVANT CODE

/*************** *************** *************** **
Quoted from

http://www.research.att.com/~bs/wrapper.pdf

pp5.

Changes applied and commented
*************** *************** *************** ***/

template <class Tclass Wrap;

template <class T>
class Call_proxy{
T* p;
mutable bool own;

Call_proxy(T* pp):p(pp), own(true){}
Call_proxy(cons t Call_proxy& a): p(a.p), own(true){a.own = false;}
Call_proxy& operator=(const Call_proxy&);

std::vector<int extra; // extra data member (added by me)

public:
template <class Ufriend class Wrap;

~Call_proxy()
{
if(own) suffix(); // if this throws, extra will leak
}

T* operator->()const{retu rn p;}
};

END OF RELEVANT CODE

My current work around is to do a vector swap trick

~Call_proxy()
{
std::vector<int temp;
temp.swap(extra );

if (own) suffix(); // if throws temp will
// be destructed
// properly
}

But this is cumbersome and hardly general.

Regards,
Ben
Nov 26 '06 #1
3 1718
* benben:
>
template <class Tclass Wrap;

template <class T>
class Call_proxy{
T* p;
mutable bool own;

Call_proxy(T* pp):p(pp), own(true){}
Call_proxy(cons t Call_proxy& a): p(a.p), own(true){a.own = false;}
Call_proxy& operator=(const Call_proxy&);

std::vector<int extra; // extra data member (added by me)

public:
template <class Ufriend class Wrap;

~Call_proxy()
{
if(own) suffix(); // if this throws, extra will leak
}
U-huh. The following program,

#include <iostream>
#include <ostream>
#include <stdexcept>

void say( char const s[] ) { std::cout << s << std::endl; }

struct Tracer
{
Tracer() { say( "Constructe d." ); }
~Tracer() { say( "Destroyed. " ); }
};

struct Thrower
{
Tracer t;
~Thrower() { throw std::runtime_er ror( "Ballaha!" ); }
};

int main()
{
try
{
Thrower o;
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
}
}

should yield

Constructed.
Destroyed.
!Ballaha!

What's the result with your compiler?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 26 '06 #2
U-huh. The following program,
>
#include <iostream>
#include <ostream>
#include <stdexcept>

void say( char const s[] ) { std::cout << s << std::endl; }

struct Tracer
{
Tracer() { say( "Constructe d." ); }
~Tracer() { say( "Destroyed. " ); }
};

struct Thrower
{
Tracer t;
~Thrower() { throw std::runtime_er ror( "Ballaha!" ); }
};

int main()
{
try
{
Thrower o;
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
}
}

should yield

Constructed.
Destroyed.
!Ballaha!

What's the result with your compiler?
Same! I must say I am amazed! Apparently my worry was unnecessary and I
should have gone into researching more before posting.

I have always assumed that destruction of the members happens at the end
of the destructor, so if the destructor throws, it would never make its
way to destroying the members.

Apparently C++ is much better designed than I thought it was.

Many thanks to Alf for putting me out of misery!

Ben
Nov 26 '06 #3

"benben" <benhonghatgmai ldotcom@nospamw rote in message
news:45******** **************@ news.optusnet.c om.au...
>I have always found Stroustrup's paper on generalized member function
wrapper (http://www.research.att.com/~bs/wrapper.pdf) an interesting one.
Recently I started to play with it. As I tried to put some extra data
members (such as a std::vector) to the Call_proxy<clas s template it
started to worry me because if the suffix in the destructor throws, there
can be a memory leak. (See relevant code below)

The Call_proxy<uses its own destructor to facilitate a call to a suffix
at the end of a statement (as an instance of Call_proxy<is always a
temporary.) This isn't a problem in the proposal in the paper because the
original version of the class template has no data member that requires
non-trivial destruction.

I am wondering if anyone has an idea as to how to improve the exception
safety. The requirements are:

1. If suffix() call throws, data members in Call_proxy<are properly
destroyed.

2. The exception thrown is to be catched by the caller (i.e. the
destructor must be allowed to throw.

I never saw the need to abort the destruction of an object.
What would be the result?
Leaving a scope means destroying local objects.
Throwing an exception means leaving a scope.
Thus destructors should never throw.

Nov 27 '06 #4

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

Similar topics

37
5023
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well for us.
7
2327
by: Mikhail N. Kupchik | last post by:
Hi All. I have a question regarding Herb Sutter's idiom of implementation of operator= via nonthrowing swap() member function, to guarantee strict exception safety. The idea of the idiom is shown by the example: -- code fragment 1 ---------------------------------------------------- class MyClass
12
1883
by: Eric Lilja | last post by:
Hello, I'm working with a hash table that is encapsulated in a class. One of its member functions insert() throws an exception if the insertion fails (for example, if the value was already present in the hash table). Now I have client code that looks like this: bool exception_thrown = false; try { hash_table.insert(some_value);
4
2096
by: st_ev_fe | last post by:
Hi people, I've been doing C for about 7 years now. But I'm new to C++. I've decided that C++'s operator overloading could be very handy. I'm writing something much like auto_ptr, except for my own set of classes. Basically, it's a class that is supposed to be allocated automatically on the stack, and possesess one member which is a pointer to an object. Unlike auto_ptr, my object will actually call a lock or unlock function
4
6411
by: Divick | last post by:
Hi all, I want to subclass std::exception so as to designate the type of error that I want to throw, out of my classes, and for that I need to store the messages inside the exception classes. I want to use std::string to do that so that I don't have to deal with all the hustle of dealing with char *'s but as listed in the page (see link) below, it is not advisable to use std::string in my exception classes. The rational given is not...
11
1933
by: Matthias | last post by:
Hello, templated virtual member functions are not allowed. Now I am searching for a good workaround for this problem, but I can't find any. Here's my problem: class Base { template <typename T> virtual void setParam(std::string s, const T& value);
6
7684
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in a .NET application and thus am looking into writing a managed C++ wrapper for in vs2005. Furthermore, this library has many callback hooks which need to be implemented by the C++ wrapper. These callback functions are declared as "extern C...
6
3884
by: Olumide | last post by:
Hi - I've got a class that contains static member functions alone, all of whose arguments are passed by reference as shown below: class MySpiffyClass{ // no constructor, destructor or variables, just static members static void FirstFunction( args & ); static void SecondFunction( args & ); static void ThirdFunction( args & );
2
2968
by: Bob Altman | last post by:
Hi all, We have a native class modeled after the System::Exception class, and all exceptions that we throw derive from this class. For now this class is quite simple: just Description and InnerException public members. One of these days I'll dig into how to implement a StackTrace property (I assume that this is possible using something like dbghelp.dll, but I've never had the time to look into it). I've recently written a managed...
0
9673
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
9525
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
10221
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...
1
10169
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7546
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
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
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.