473,671 Members | 2,426 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 1545
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
1514
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
2131
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
2242
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
5705
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
1191
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
2348
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
1219
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
1843
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
1766
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
8481
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...
1
8602
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
8672
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
7441
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6234
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
5702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
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
4412
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2817
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

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.