资源获取是初始化? [英] resource acquisition is initialization?

查看:91
本文介绍了资源获取是初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的程序,下面列出了,它基本上试图使用资源但是如果发生异常则防止内存泄漏。

class T是资源和类用户是使用资源,我的

期望是当t2失败时,t1的析构函数应该是

调用,这没有发生。


我在solaris 2.8上使用g ++ 3.2.3,谢谢。


yang


#include< string> ;

#include< iostream>


使用命名空间std;


class T {

私人:

字符串名称;


public:

T(const string& name){

this-> name = name;

cout<<"分配资源 <<名称<< endl;

if(name.compare(" banana")== 0){

抛出异常();

}

}


运算符字符串(){

返回名称;

}


~T(){

cout<<"" deallocate resource from <<名称<<结束;

}

};

类用户{

私人:

T t1,t2;


public:

User(const string& s,const string& p):t1(s),t2(p){

cout<<" user inited" << endl;

尝试{

抛出异常();

} catch(例外& e){

}

}


void use(){

cout<< 使用<< (字符串)t1<< "和 << (字符串)t2<<

endl;

抛出异常();

}


~User(){

cout<<<"" user destroyed" <<结束;

}

};

int main(int argc,char ** argv){

/ *

用户* u = NULL;

if(argc> = 3){

if(argc> 3){

用户x(argv [1],argv [2]);

x.use();

返回0;

}

}

u =新用户(argv [1],argv [2]);

}其他{

u =新用户(字符串("苹果)),字符串(" banana"));

}


u-> ; use();

删除你;

* /

用户u(苹果,香蕉);

u.use();

}

解决方案

yang su写道:

我有一个非常简单的程序,如下所示,它基本上试图使用资源但是如果发生异常则防止内存泄漏。
类T是资源和类用户是使用资源,我的期望是当t2失败时,t1的析构函数应该是
编辑,这没有发生。

我在solaris 2.8上使用g ++ 3.2.3,谢谢。




可能是个bug编译器。我用try {} catch(...){}包围了''main''

的内容,以防止未处理的异常,并且按照VC ++的预期工作了

7.1


Victor


yang su写道:

我有一个非常简单下面列出的程序,它基本上试图使用资源但是防止内存泄漏应该发生异常。
类T是资源和类用户是使用资源,我的期望是当t2失败了,应该调用t1的析构函数,这不会发生。




我认为问题是你没有抓到例外。如果找到

未捕获的异常,则在销毁

变量之前终止该过程。如果我在主要的创建和使用中添加一个try / catch块,那么该对象会按预期被破坏。


< blockquote> yang su写道:

我有一个非常简单的程序如下所示,它基本上试图使用资源但是如果发生异常则防止内存泄漏。
class T是资源和类用户要使用资源,我的期望是当t2失败时,应该调用t1的析构函数,这不会发生。




不太好。 t2抛出一个异常,其中不存在异常处理程序。

如果发生类似的情况,运行时系统必须调用

terminate()函数,该函数将中止该程序。好的一点是:

c ++标准说,它是实现定义的,无论是否在这种情况下堆栈

是否被解开!

所以如果你想要保证清理,即使没有一个

合适的异常处理程序,你也必须将任何东西包装成

catchall block:


int main()

尝试{

用户u(" apple"," banana" );

u.use();

}

catch(...)

{

//根据需要调用abort()或terminate()..

}


Marco

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("banana") == 0 ) {
throw exception();
}
}

operator string() {
return name;
}

~T() {
cout<<"deallocate 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(exception& 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("apple"), string("banana"));
}

u->use();
delete u;
*/
User u("apple", "banana");
u.use();
}

解决方案

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


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.


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


这篇关于资源获取是初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆