通过异常展开来检测我的析构函数是否被调用? [英] Detect if my destructor is called by unwinding through an exception?

查看:103
本文介绍了通过异常展开来检测我的析构函数是否被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的类来验证函数的开头和

结尾之间的一些数据:


class验证

{

验证(){注意数据值}

~Kerly(){检查数据值是否与我们记录的相同}

};


void foo()

{

验证_v;

.. 。

抛出SomeException; //测试可能会因为_v被破坏而失败

''过早''

...

};

~Kerify()只是断言警告我我做错了什么

在函数体中。

如果发生异常,验证不再相关。

有没有办法检测到我是(〜Verify())被

异常而不是结束范围?



~确认()

{if(!exception)assert(...); }


我知道我可以在函数中添加另一个变量,或者设置一些

的成员在我抛出之前验证,但是这使得使用验证类

作为测试函数完整性的简单方法。

I have a simple class that verifies some data between the beginning and the
end of a function:

class Verify
{
Verify() { note the data value }
~Verify() { check if data value is the same as we noted }
};

void foo()
{
Verify _v;
...
throw SomeException; // test can fail because _v is destroyed
''prematurely''
...
};

~Verify() just makes an assertion to warn me that I''ve done something wrong
in the function body.
If the exception occurs, the verification is no longer relevant.
Is there some way to detect that I''m (~Verify()) being destroyed by an
exception rather than the end of scope?
i.e.
~Verify()
{ if (!exception) assert(...); }

I know I could add another variable into the function, or set some member of
Verify before I throw, but that defeats the point of using the Verify class
as a simple way to test the integrity of the function.

推荐答案



Douglas Peterson <碲****** @ nospam.msn.com>在留言中写道

news:qN ******************** @ comcast.com ...

"Douglas Peterson" <Te******@nospam.msn.com> wrote in message
news:qN********************@comcast.com...
I有一个简单的类来验证开头和
之间的一些数据函数的结尾:


为什么不在函数中保持数据不变?或者在你的数据类中实现verify()

方法?

类验证
{
验证(){注意数据值}
〜Verify(){检查数据值是否与我们记录的相同}
};

void foo()
{
验证_v;
...
抛出SomeException; //测试可能会因为_v被破坏而失败
''过早地''
...
};

〜Verify()只是断言警告我我在函数体中做了一些
错误的事情。
如果发生异常,验证就不再相关了。
有没有办法检测到我是谁(〜验证())是否被
异常而不是范围的结束所破坏?


验证验证是否破坏了析构函数?你应该在这里重新考虑设计

。构造函数应该执行构造,而不是验证。

同样适用于析构函数。从ctor或者dor tor中抛出异常是一个很好的例子a_big_no_no。

ie
〜Verify()
{if(!exception)断言(...);我知道我可以在函数中添加另一个变量,或者在我抛出之前设置一些成员
的验证,但是这使得使用Verify
类作为测试函数完整性的简单方法。
I have a simple class that verifies some data between the beginning and the end of a function:
Why not keep data constant in the function instead? Or implement a verify()
method in your data class otherwise?

class Verify
{
Verify() { note the data value }
~Verify() { check if data value is the same as we noted }
};

void foo()
{
Verify _v;
...
throw SomeException; // test can fail because _v is destroyed
''prematurely''
...
};

~Verify() just makes an assertion to warn me that I''ve done something wrong in the function body.
If the exception occurs, the verification is no longer relevant.
Is there some way to detect that I''m (~Verify()) being destroyed by an
exception rather than the end of scope?
To verify that Verify zapped your destructor? You should rethink the design
here. A constructor should be performing construction, not verification.
Ditto for the destructor. Throwing exceptions from a ctor or d~tor is a good
example of a_big_no_no.
i.e.
~Verify()
{ if (!exception) assert(...); }

I know I could add another variable into the function, or set some member of Verify before I throw, but that defeats the point of using the Verify class as a simple way to test the integrity of the function.




你没有测试函数的完整性,你正在测试数据的

完整性。



You aren''t testing the integrity of the function, you are testing the data''s
integrity.


这只是一个断言,用于验证我的堆栈(不是程序堆栈)是否为
当函数开始作为一个完整性检查时所处的状态相同

表示何时/如果代码被修改。


class验证

{

public:

Verify(){m_stackDepth = getStackDepth(); }

~Complete(){assert(m_stackDepth == getStackDepth();}

int m_stackDepth;

};


这就是它的全部。我可以在操作堆栈的每个

函数的开头放一行:


void foo()

{

验证_verify;

...

}


简单的馅饼。


显然我可以写:


void foo()

{

int _stackDepth = getStackDepth();

...

断言(_stackDepth == getStackDepth());

}


这似乎可以解决这个问题(因为断言会跳过

一次投掷),但很多大约60个使用它的函数有

备用返回路径。最好的解决方案是IMO将支票折入

类对象。


如果在执行函数期间发生抛出,则堆栈深度(再次

请记住我们是不谈论程序堆栈不同步是因为
不再相关,整个堆栈因错误而被破坏

条件。


我当然可以保留它,但是当它没有任何意义时,它很烦人地获得断言




codigo <共**** @ codigo.trap.com>在消息中写道

新闻:我们***************** @ news20.bellglobal.com ...
It''s just a assertion to verify that my stack (not the program stack) is
left in the same state it was in when the function began as a sanity check
for when/if the code gets modified.

class Verify
{
public:
Verify() { m_stackDepth = getStackDepth(); }
~Verify() { assert(m_stackDepth == getStackDepth(); }
int m_stackDepth;
};

That''s the whole of it. I can put one line at the beginning of every
function that manipulates the stack:

void foo()
{
Verify _verify;
...
}

Simple as pie.

Obviously I could write:

void foo()
{
int _stackDepth = getStackDepth();
...
assert(_stackDepth == getStackDepth());
}

That might seem to solve the problem (since the assertion would be skipped
by a throw), but many of the roughly 60 functions that use this have
alternate return paths. The best solution IMO was to toss the check into a
class object.

If a throw occurs during execution of the function, the stack depth (again
keep in mind we''re not talking about the program stack) being out of sync is
no longer relevant, the entire stack gets destroyed as a result of the error
condition.

I can just leave it as is of course, but it''s annoying getting the assertion
when it has no meaning.

"codigo" <co****@codigo.trap.com> wrote in message
news:We*****************@news20.bellglobal.com...

道格拉斯彼得森 <碲****** @ nospam.msn.com>在消息中写道
新闻:qN ******************** @ comcast.com ...

"Douglas Peterson" <Te******@nospam.msn.com> wrote in message
news:qN********************@comcast.com...
我有一个简单的课验证开头和
I have a simple class that verifies some data between the beginning and

之间的一些数据

函数的结尾:



为什么不在函数中保持数据不变代替?或者在你的数据类中实现一个
verify()
方法?



Why not keep data constant in the function instead? Or implement a
verify()
method in your data class otherwise?


类验证
{
验证( ){注意数据值}
〜Verify(){检查数据值是否与我们记录的相同}
};

void foo()
{
验证_v;
...
抛出SomeException; //测试可能会因为_v被破坏而失败
''过早地''
...
};

〜Verify()只是断言警告我我在函数体中做了一些

class Verify
{
Verify() { note the data value }
~Verify() { check if data value is the same as we noted }
};

void foo()
{
Verify _v;
...
throw SomeException; // test can fail because _v is destroyed
''prematurely''
...
};

~Verify() just makes an assertion to warn me that I''ve done something


错误的


如果发生异常,则验证不再相关。
是有一些方法来检测我(〜Verify())是否被
异常而不是范围的结束所破坏?
in the function body.
If the exception occurs, the verification is no longer relevant.
Is there some way to detect that I''m (~Verify()) being destroyed by an
exception rather than the end of scope?



要验证验证是否已经破坏了你的析构函数?你应该重新考虑
设计
。构造函数应该执行构造,而不是验证。
同样适用于析构函数。从ctor或dtor转出异常是一个很好的例子a_big_no_no。



To verify that Verify zapped your destructor? You should rethink the
design
here. A constructor should be performing construction, not verification.
Ditto for the destructor. Throwing exceptions from a ctor or d~tor is a
good
example of a_big_no_no.

ie
〜Verify()
{ if(!exception)assert(...);我知道我可以在函数中添加另一个变量,或者在我抛出之前设置
i.e.
~Verify()
{ if (!exception) assert(...); }

I know I could add another variable into the function, or set some member
验证的一些成员


使用Verify

Verify before I throw, but that defeats the point of using the Verify


作为测试函数完整性的简单方法。
as a simple way to test the integrity of the function.



你不是''测试功能的完整性,您正在测试
数据的完整性。



You aren''t testing the integrity of the function, you are testing the
data''s
integrity.



" ;道格拉斯彼得森 < Tergi ... @ nospam.msn.com>写道:
"Douglas Peterson" <Tergi...@nospam.msn.com> wrote:
如果发生异常,验证就不再相关了。
有没有办法检测到我(@ Verify())被摧毁了
异常而非范围结束?
ie
〜Verify()
{if(!exception)assert(...); }
If the exception occurs, the verification is no longer relevant.
Is there some way to detect that I''m (~Verify()) being destroyed by an exception rather than the end of scope? i.e.
~Verify()
{ if (!exception) assert(...); }




尝试uncaught_exception()(虽然这个函数被认为是

不优雅)。


AC



Try uncaught_exception() (though this function is deemed to be
unelegant).

A.C.


这篇关于通过异常展开来检测我的析构函数是否被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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