如何确定是否已释放C ++对象? [英] How can I determine if a C++ object has been deallocated?

查看:139
本文介绍了如何确定是否已释放C ++对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个上一个问题,在该问题中我回答了描述当我尝试访问由第三方函数释放的对象时捕获异常的困难。该函数有时会分配对象,有时不会取消分配对象。

I have a previous question that was answered in which I describe difficulties catching an exception when I try to access an object that has been deallocated by a third-party function. The function sometimes does and sometimes doesn't deallocate the object.

为了避免不得不使用try / catch块来捕获SEH异常,如上一个问题所述,我需要能够判断对象是否具有是否被释放。

In order to avoid having to use a try/catch block to catch the SEH Exception as described in the previous question, I need to be able to tell whether the object has been deallocated or not.

如何确定C ++对象是否已被释放或仍然是有效的指针?

How can I determine if a C++ object has been deallocated or is still a valid pointer?

推荐答案

仅通过查看内存位置就无法轻松分辨对象是否仍被分配。可能有一些黑魔术可以做到这一点,但是更干净的方法是在对象的析构函数中建立回调机制。

You can't easily tell just by looking at the memory location if the object is still allocated or not. There might be some black magic tricks to do that, but a much cleaner way would be to build a call-back mechanism into the object's destructor.

这篇关于如何确定是否已释放C ++对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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