473,902 Members | 3,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

resource acquisition is initialization?

I have a very simple program listed below, which basically tries to
use resources but prevent from memory leak should exception occurs.
class T is the resource and class User is to use the resource, my
expectation is that when t2 fails, the destructor of t1 should be
called, which didn't happen.

I use g++ 3.2.3 on solaris 2.8, thanks.

yang

#include <string>
#include <iostream>

using namespace std;

class T {
private:
string name;

public:
T(const string& name) {
this->name = name;
cout<<"allocate resource for " << name << endl;
if( name.compare("b anana") == 0 ) {
throw exception();
}
}

operator string() {
return name;
}

~T() {
cout<<"dealloca te resource from " << name << endl;
}
};
class User {
private:
T t1, t2;

public:
User(const string& s, const string& p): t1(s), t2(p) {
cout<<"user inited" << endl;
try{
throw exception();
}catch(exceptio n& e) {
}
}

void use( ) {
cout<< "use "<< (string)t1 << " and " << (string)t2 <<
endl;
throw exception();
}

~User() {
cout <<"user destroyed" << endl;
}
};
int main(int argc, char **argv) {
/*
User *u = NULL;
if( argc >= 3 ) {
if( argc > 3 ) {
User x(argv[1], argv[2]);
x.use();
return 0;
}
}
u = new User( argv[1], argv[2] );
}else{
u = new User(string("ap ple"), string("banana" ));
}

u->use();
delete u;
*/
User u("apple", "banana");
u.use();
}
Jul 22 '05 #1
11 1564
yang su wrote:
I have a very simple program listed below, which basically tries to
use resources but prevent from memory leak should exception occurs.
class T is the resource and class User is to use the resource, my
expectation is that when t2 fails, the destructor of t1 should be
called, which didn't happen.

I use g++ 3.2.3 on solaris 2.8, thanks.


Could be a bug in the compiler. I surrounded the contents of 'main'
with try {} catch(...) {} to prevent unhandled exception, and it worked
as expected under VC++ 7.1

Victor
Jul 22 '05 #2
yang su wrote:
I have a very simple program listed below, which basically tries to
use resources but prevent from memory leak should exception occurs.
class T is the resource and class User is to use the resource, my
expectation is that when t2 fails, the destructor of t1 should be
called, which didn't happen.


I think the problem is that you don't catch the exception. If an
uncaught exception is found, the process is terminated before the
variable can be destroyed. If I add a try/catch block in main around
the creation and usage of u, the object gets destroyed as expected.

Jul 22 '05 #3
yang su wrote:
I have a very simple program listed below, which basically tries to
use resources but prevent from memory leak should exception occurs.
class T is the resource and class User is to use the resource, my
expectation is that when t2 fails, the destructor of t1 should be
called, which didn't happen.


Not quite. t2 throws an exception for which no exception handler exist.
If something like that happens, the runtime system has to call the
terminate() function, which aborts the program. The fine point is: The
c++ standard says, that it is implementation defined, whether the stack
is unwound or not in this case!

So if you want guaranteed clean-up even in case of not-having an
appropriate exception handler, you'll have to wrap anything into a
catchall block:

int main()
try {
User u("apple", "banana");
u.use();
}
catch(...)
{
// call abort() or terminate() as you like..
}

Marco

Jul 22 '05 #4

"yang su" <y2*******@yaho o.com> wrote in message
news:16******** *************** ***@posting.goo gle.com...
I have a very simple program listed below, which basically tries to
use resources but prevent from memory leak should exception occurs.
class T is the resource and class User is to use the resource, my
expectation is that when t2 fails, the destructor of t1 should be
called, which didn't happen.

I use g++ 3.2.3 on solaris 2.8, thanks.

yang

[snip]

class User {
private:
T t1, t2;


It is not defined whether t1 or t2 is constructed first. Only if t1 is
constructed before t2 will an exception in t2 cause the destructor for t1 to
be called.

Try this

T t1;
T t2;

Now t1 must be constructed before t2.

john
Jul 22 '05 #5
John Harrison wrote:
class User {
private:
T t1, t2;


It is not defined whether t1 or t2 is constructed first.


That's wrong. t1 is constructed first.

Jul 22 '05 #6
Rolf Magnus wrote:
John Harrison wrote:
class User {
private:
T t1, t2;


It is not defined whether t1 or t2 is constructed first.


That's wrong. t1 is constructed first.


Hmm, ok. I'm not sure about that actually. You mean the order is not
defined because they are declared as T t1, t2; instead of T t1; T t2;?

Jul 22 '05 #7

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:cf******** *****@news.t-online.com...
Rolf Magnus wrote:
John Harrison wrote:
class User {
private:
T t1, t2;
It is not defined whether t1 or t2 is constructed first.


That's wrong. t1 is constructed first.


Hmm, ok. I'm not sure about that actually. You mean the order is not
defined because they are declared as T t1, t2; instead of T t1; T t2;?


That's what I mean, but I'm relying on my sometimes faulty recollection.
I'll look it up later.

John
Jul 22 '05 #8
John Harrison wrote:
"yang su" <y2*******@yaho o.com> wrote in message
news:16******** *************** ***@posting.goo gle.com...
I have a very simple program listed below, which basically tries to
use resources but prevent from memory leak should exception occurs.
class T is the resource and class User is to use the resource, my
expectation is that when t2 fails, the destructor of t1 should be
called, which didn't happen.

I use g++ 3.2.3 on solaris 2.8, thanks.

yang
[snip]

class User {
private:
T t1, t2;

It is not defined whether t1 or t2 is constructed first.


Really? Care to elaborate or give a quote from the Standard? For some
reason I always thought that initialisation (construction) happens in the
order of declaration. Besides, in a declaration statement there is
a sequence point after every declarator.
Only if t1 is
constructed before t2 will an exception in t2 cause the destructor for t1 to
be called.

Try this

T t1;
T t2;

Now t1 must be constructed before t2.

john


I _really_ think there is no difference between

A a, b;

and

A a; A b;

It would be illogical to have them behave differently depending on where
the declaration happens.

V
Jul 22 '05 #9
>>> class User {
private:
T t1, t2;

It is not defined whether t1 or t2 is constructed first.


Really? Care to elaborate or give a quote from the Standard?


No, seems I was wrong. I can't think where I got the idea from.

john
Jul 22 '05 #10

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

Similar topics

4
1528
by: Julie | last post by:
(I searched the FAQ, but didn't find anything relevant -- if there is, please post a link.) Is there a design paradigm that indicates that a class that manages an external resource should automatically perform clean up in the destructor? For example, suppose the following file-wrapper class. // begin
1
2152
by: Ryan Melville | last post by:
Hi, I need to use WIA (Windows Image Acquisition) from managed C++. Is there a "new and improved" way to access WIA from managed C++ (i.e., through .net)? Or, is it the same COM calls as from unmanaged C++? If it's the same COM calls, is there a "new and improved" way to access COM through managed C++? Or, do I do the same CComPtr magic as with unmanaged C++? Or, can I not use ATL facilities from managed C++ and I would have to...
7
2258
by: jude | last post by:
Hello, We are starting to discuss a new ASP.NET application that will be a data acquisition display application. The data to be displayed will come from multiple sources--database tables, serial and Ethernet PLCs, OPC servers, etc. The application will need to display graphic gauges and charts, in addition to the actual values retrieved from the various data sources. The application will need to continually update itself in a real-time...
3
5725
by: Roberto Hernández | last post by:
I try to use the Windows Image Acquisition (WIA) with a sample in vb.net but it takes only back photos and also at low resolution. How can I put ther resolution at 640x480? I have a Labtec webcam plus that works fine with other software. I downloaded two samples from the internte and both of them do the same problem The samples are: http://www.vbforums.com/showthread.php?t=378126 http://www.vbforums.com/attachment.php?attachmentid=44367...
0
1201
by: Scott | last post by:
Biopotential Biofeedback Data Acquisition DataQ Data Acquisition Custom Software 1 to 32 channels (AuroScroll 4ch/time) 35+ DataQ Devices supported Record / Playback Functions 0-15 Plot bits + Analog Adjustable Trigger Modes Adj. Sample Rate, Gain And Zoom Plus Much More ! Custom Developed Versions
36
2376
by: Ulysses | last post by:
Could someone tell me how to go about getting data from a data acquisition card using C?...<Just general information would also help........I'm just working on an idea at the moment>.
3
1230
by: George2 | last post by:
Hello everyone, Through my testing and study of RAII (Resource Acquisition Is Initialization) pattern, I think it can solve resource release issue in the following two situations, 1. Local function object (on stack); 2. Object (either on heap or stack) pointer by auto_ptr; But it has the limitation that the object pointed by a normal pointer and allocated on heap (using new or new) can not be automatically released, either the...
1
1855
by: George2 | last post by:
Hello everyone, I think unmanaged resource means the resources (e.g. memory and file handler) which is used directly (new, FILE*) other than using a wrapper class (Resource Acquisition Is Initialization) or auto_ptr to wrap it. Is my understanding correct? Here is a sample about what is unmanaged resource.
1
1781
by: ebony.soft | last post by:
Dear all Hi As you know constructor is a member function with several missions and one of them is "acquiring a resource" and in the same token destructor "releases the resource". Usually after such descriptions, It is said, the resource is like memory, file, lock, semaphore, ... As a matter of fact, resource isn't confined to memory and constructor/destructor do more than just memory management. I reviewed most of the major books and...
0
9845
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
11277
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10866
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
10978
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,...
0
10497
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6084
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4724
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
4305
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3323
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.