R
Robbie Hatley
I've been playing with "auto_ptr" and the "Resource Acquisition
Is Initialization" concept. On page 199 of Lippman's book
"Effective C++" he says "All active local class objects of a
function are guaranteed to have their destructors applied
before termination of the function by the exception handling
mechanism.".
I don't think that's true, though. The standard doesn't seem
to require it. Section 15.2 just says that all objects
constructed SINCE ENTERING A TRY BLOCK will be destructed
on transfer of control to a catch block. That's very different
from Lippman's statement.
I wrote this:
#include <iostream>
#include <string>
#include <memory>
#include <cstdlib>
using std::cout;
using std::endl;
class Vogon
{
public:
Vogon(int x) : Jeltz(x) {cout << "In parameterized constructor." << endl;}
~Vogon() {cout << "In destructor." << endl;}
void React ()
{
if (17 == Jeltz) throw 17; // Barf if 17==Jeltz
cout << "You entered " << Jeltz
<< ". (But don't enter \"17\"!)" << endl;
}
void Poetry () {cout << "Oh, furdled gruntbuggly!" << endl;}
private:
int Jeltz;
};
int main(int, char* A[])
{
int X = atoi(A[1]);
std::auto_ptr<Vogon> Vog (new Vogon(X));
Vog->React(); // If user used command-line arg. "17", throw uncaught
exception.
Vog->Poetry(); // Even though the sound of it is somewhat quite atrocious.
return 0; // Release resources???
}
But if you type "auto_ptr-test 17" it barfs without ever calling
the destructor:
wd=C:\RHE\src\test
%auto_ptr-test 45
In parameterized constructor.
You entered 45. (But don't enter "17"!)
Oh, furdled gruntbuggly!
In destructor.
wd=C:\RHE\src\test
%auto_ptr-test 17
In parameterized constructor.
terminate called after throwing an instance of 'i'
Abort!
Exiting due to signal SIGABRT
Raised at eip=0001648e
eax=006dfe6c ebx=00000120 ecx=00000000 edx=00000000 esi=00000000 edi=006dffc4
ebp=006dff18 esp=006dfe68 program=C:\BIN-TEST\AUTO_P~1.EXE
cs: sel=01a7 base=01680000 limit=006effff
ds: sel=01af base=01680000 limit=006effff
es: sel=01af base=01680000 limit=006effff
fs: sel=017f base=0000e150 limit=0000ffff
gs: sel=01bf base=00000000 limit=0010ffff
ss: sel=01af base=01680000 limit=006effff
App stack: [006e0000..00660000] Exceptn stack: [000538c8..00051988]
Call frame traceback EIPs:
0x000163b4
0x0001648e
0x0001335b
0x00009d6b
0x00005338
0x00005362
0x00001e51
0x000016a3
0x00012ab8
wd=C:\RHE\src\test
%
(Interestingly enough, the destructor IS called if I enclose
most of the body of main() in a try block, just like 15.2
says.)
So either Lippman's right, and my compiler is busted... or I'm
misunderstanding something... or the concept that local objects
in a function are always destructed on exception is just wrong.
Anyone here have further knowlege/experience with this matter?
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
Is Initialization" concept. On page 199 of Lippman's book
"Effective C++" he says "All active local class objects of a
function are guaranteed to have their destructors applied
before termination of the function by the exception handling
mechanism.".
I don't think that's true, though. The standard doesn't seem
to require it. Section 15.2 just says that all objects
constructed SINCE ENTERING A TRY BLOCK will be destructed
on transfer of control to a catch block. That's very different
from Lippman's statement.
I wrote this:
#include <iostream>
#include <string>
#include <memory>
#include <cstdlib>
using std::cout;
using std::endl;
class Vogon
{
public:
Vogon(int x) : Jeltz(x) {cout << "In parameterized constructor." << endl;}
~Vogon() {cout << "In destructor." << endl;}
void React ()
{
if (17 == Jeltz) throw 17; // Barf if 17==Jeltz
cout << "You entered " << Jeltz
<< ". (But don't enter \"17\"!)" << endl;
}
void Poetry () {cout << "Oh, furdled gruntbuggly!" << endl;}
private:
int Jeltz;
};
int main(int, char* A[])
{
int X = atoi(A[1]);
std::auto_ptr<Vogon> Vog (new Vogon(X));
Vog->React(); // If user used command-line arg. "17", throw uncaught
exception.
Vog->Poetry(); // Even though the sound of it is somewhat quite atrocious.
return 0; // Release resources???
}
But if you type "auto_ptr-test 17" it barfs without ever calling
the destructor:
wd=C:\RHE\src\test
%auto_ptr-test 45
In parameterized constructor.
You entered 45. (But don't enter "17"!)
Oh, furdled gruntbuggly!
In destructor.
wd=C:\RHE\src\test
%auto_ptr-test 17
In parameterized constructor.
terminate called after throwing an instance of 'i'
Abort!
Exiting due to signal SIGABRT
Raised at eip=0001648e
eax=006dfe6c ebx=00000120 ecx=00000000 edx=00000000 esi=00000000 edi=006dffc4
ebp=006dff18 esp=006dfe68 program=C:\BIN-TEST\AUTO_P~1.EXE
cs: sel=01a7 base=01680000 limit=006effff
ds: sel=01af base=01680000 limit=006effff
es: sel=01af base=01680000 limit=006effff
fs: sel=017f base=0000e150 limit=0000ffff
gs: sel=01bf base=00000000 limit=0010ffff
ss: sel=01af base=01680000 limit=006effff
App stack: [006e0000..00660000] Exceptn stack: [000538c8..00051988]
Call frame traceback EIPs:
0x000163b4
0x0001648e
0x0001335b
0x00009d6b
0x00005338
0x00005362
0x00001e51
0x000016a3
0x00012ab8
wd=C:\RHE\src\test
%
(Interestingly enough, the destructor IS called if I enclose
most of the body of main() in a try block, just like 15.2
says.)
So either Lippman's right, and my compiler is busted... or I'm
misunderstanding something... or the concept that local objects
in a function are always destructed on exception is just wrong.
Anyone here have further knowlege/experience with this matter?
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant