简体   繁体   English

如何确定是否已取消分配C ++对象?

[英]How can I determine if a C++ object has been deallocated?

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. 该函数有时会执行并且有时不会释放该对象。

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. 为了避免必须使用try / catch块来捕获前一个问题中描述的SEH异常,我需要能够判断对象是否已被释放。

How can I determine if a C++ object has been deallocated or is still a valid pointer? 如何确定C ++对象是否已被释放或仍然是有效指针?

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. 可能会有一些黑魔法技巧,但更清洁的方法是在对象的析构函数中构建一个回调机制。

I think you're asking the wrong question. 我想你问的是错误的问题。 The question really ought to be: 问题应该是:

Why do I feel like I need to look at some address in order to find out whether the pointer pointing there refers to an object or to the garbage left after an ex-object was deleted? 为什么我觉得我需要查看一些地址才能找出指向那里的指针是指一个对象还是删除了一个ex-object后留下的垃圾?

Honestly, you shouldn't be in that situation. 老实说,你不应该处于那种境地。 If an object is deleted, why are you left with a pointer to its ex-address? 如果删除了一个对象,为什么还有一个指向其前地址的指针? Ideally, the pointer should be out of scope. 理想情况下,指针应该超出范围。 If that isn't possible in your code, it should have been set to NULL . 如果您的代码中无法实现,则应将其设置为NULL

You can't. 你不能。 If the function you're using doesn't give you ways of knowing whether or not it deallocated the object itself, there's no way you can find out. 如果你正在使用的函数没有给你如何知道它是否释放了对象本身,你就无法找到它。

Edit: Well, unless the object in question is an instance of your own class and you can modify the destructor like Adrian suggested. 编辑:好吧,除非有问题的对象是你自己的类的一个实例,你可以修改像Adrian建议的析构函数。

I don't think you can, at least not in a standard way. 我认为你不能,至少不是以标准的方式。 There is, as far as I know, no requirement that the heap manager be able to detect if an address has been previously returned. 据我所知,没有要求堆管理器能够检测先前是否已返回地址。

If there was, there would be no need to make it undefined what delete is to do with invalid pointers. 如果有的话,就没有必要将未delete与无效指针有关的内容定义为未定义。

If you control all the code that accesses your object, then the proper way is to use boost::shared_ptr to manage the lifetime of the object and then boost::weak_ptr for any references that you expect to remain after the object is deleted. 如果您控制访问对象的所有代码,那么正确的方法是使用boost :: shared_ptr来管理对象的生命周期,然后使用boost :: weak_ptr来查找在删除对象后您希望保留的任何引用。

Looking at your other question about a third party library though, you may be stuck. 看看你关于第三方图书馆的其他问题,你可能会陷入困境。 If the library is prone to deleting the object as a side effect of other options, then likely you're not intended to keep those pointers around. 如果库容易将对象删除作为其他选项的副作用,那么很可能您并不打算保留这些指针。 Is there a reason that you can't re-fetch the object any time you need it, or at least after doing anything that may have deleted it? 是否有理由不能在任何需要的时候重新获取对象,或者至少在做了可能删除它的任何事情之后?

create a destructor that notifies upon desrtruction. 创建一个析构函数,通知desrtruction。 It could even toggle a bool, if you wanted to check it during runtime. 如果你想在运行时检查它,它甚至可以切换bool。

class free
{

public:

    ~free() { cout << "Im being freed " << endl; } //destructor output
};

This will output in console that its being freed. 这将在控制台中输出以释放它。 After being freed, any reference to it will be undefined behaviour, even if it appears to exist. 被释放后,对它的任何引用都将是未定义的行为,即使它似乎存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何验证在C ++链表中已释放节点? - How can I verify a node is deallocated in a C++ Linked List? 如果方法被覆盖,C ++基类如何在运行时确定? - How can a C++ base class determine at runtime if a method has been overridden? 如何确定是否已向tolua注册了C ++ usertype - How to determine if a C++ usertype has been registered with tolua 在对象已经初始化后,如何使用赋值运算符覆盖 C++ 中的类对象? - How can I overwrite a Class Object in C++ using the Assignment Operator, after the Object has already been initialized? 如何确定运行时void *指针是指向Objective-C对象还是C ++对象 - How can I determine whether a void * pointer points to a objective-c object or a c++ object at runtime 如何检查对象是否已在C ++中初始化/创建? - How do I check if an Object has been initialized/created in C++? c ++中的内存是如何释放的 - How is memory deallocated in c++ C++ 如何知道 object 何时被破坏 - C++ How to know when an object has been destroyed 我如何才能找到用户在包含 7 的第二个数组中输入的数字并打印出来。 C++ - How can i find if a number that has been entered by a user on the second array that contains a 7 and print it. C++ 如何删除在C ++的另一个作用域中声明的动态字符串数组? - How can I delete a dynamic string array that has been declared in another scope in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM