K
Klaus Ahrens
hi all,
acoording to the c++ standard
"15.3.
- -10- Referring to any non-static member or base class of an object in
the handler for a function-try-block of a constructor or destructor for
that object results in undefined behavior."
it is not allowed to refer to l (delete l in the following code
#include <iostream>
#include <memory>
using namespace std;
struct L {
L() { cout<<"L() at "<<this<<endl; }
~L() { cout<<"~L() at "<<this<<endl; }
};
class X {
public:
L* l;
X() try : l(new L) {
cout<<"X() at "<<this<<endl;
}
catch (...) { delete l; throw; }
~X(){
cout<<"~X() at "<<this<<endl;
delete l;
}
};
int main(){
try {
X* p = new X;
delete p;
}
catch(...) { std::cout<<"some exception occured"<<std::endl; }
}
i wonder why this has been declared such, because it leads directly
to leaking local ressources. i know herb sutters arguments from 'more
exc. c++' (item 18) where a local pointer could refer to a meanwhile
destoyed subobject (as i understand it). but this seems to be a very
rare situation.
moreover compiled with g++ (3.3.5) the output is
L() at 0x804a018
X() at 0x804a008
~X() at 0x804a008
~L() at 0x804a018
without the leaking L
any idea ?
--
mfg
k ahrens
_____________________________________________
\ phone +49 30 2093 3113 \ \
\ fax +49 30 2093 3112 \__________________\
\ mailto:[email protected] \
\ http://www.informatik.hu-berlin.de/~ahrens \
\ ____________________________________________\
acoording to the c++ standard
"15.3.
- -10- Referring to any non-static member or base class of an object in
the handler for a function-try-block of a constructor or destructor for
that object results in undefined behavior."
it is not allowed to refer to l (delete l in the following code
#include <iostream>
#include <memory>
using namespace std;
struct L {
L() { cout<<"L() at "<<this<<endl; }
~L() { cout<<"~L() at "<<this<<endl; }
};
class X {
public:
L* l;
X() try : l(new L) {
cout<<"X() at "<<this<<endl;
}
catch (...) { delete l; throw; }
~X(){
cout<<"~X() at "<<this<<endl;
delete l;
}
};
int main(){
try {
X* p = new X;
delete p;
}
catch(...) { std::cout<<"some exception occured"<<std::endl; }
}
i wonder why this has been declared such, because it leads directly
to leaking local ressources. i know herb sutters arguments from 'more
exc. c++' (item 18) where a local pointer could refer to a meanwhile
destoyed subobject (as i understand it). but this seems to be a very
rare situation.
moreover compiled with g++ (3.3.5) the output is
L() at 0x804a018
X() at 0x804a008
~X() at 0x804a008
~L() at 0x804a018
without the leaking L
any idea ?
--
mfg
k ahrens
_____________________________________________
\ phone +49 30 2093 3113 \ \
\ fax +49 30 2093 3112 \__________________\
\ mailto:[email protected] \
\ http://www.informatik.hu-berlin.de/~ahrens \
\ ____________________________________________\