C ++找到悬空指针删除 [英] C++ find dangling pointer deletion

查看:88
本文介绍了C ++找到悬空指针删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!


我正在研究Blender的源代码并尝试做出贡献,主要是通过错误修复。现在我遇到了一个问题:由于悬空指针(值0xDDDDDDDD),我正在使用Visual Studio和Blender崩溃。现在我想找到它被删除或释放的地方。那有工具或解决方案吗?大多数工具似乎只搜索内存泄漏等等,那些找到悬空指针,通常不会搜索它被删除或释放的位置。

谢谢!
<编辑:对不起双重帖子,当我问第一个时,它不知何故消失了一段时间,我以为它不起作用,再次问这个问题。然后第一个问题再次可见,但似乎它有问题,子网站错了。

解决方案

我相信0xDDDDDDDD是未初始化的指针。没有DDDDDDDD地址。


我相信如果指针永远不会被初始化,Visual Studio会将此虚假地址作为提示插入。


读取这就是说,0xDDDDDDDD表示释放内存。 https://www.softwareverify.com/memory-bit-patterns。 PHP

是。它正在慢慢回归。 0xDDDDDDDD是释放的内存,0xCCCCCCCC是未初始化的指针。


好​​的。所以这里。你需要停止使用指针。而是使用智能指针。智能指针是一个包含指针的类,每次复制指针对象时,构造函数都会增加引用计数,每次删除指针对象时,析构函数都会减少引用计数。如果引用计数变为零,则析构函数将删除包络指针指向的内存。当前对象(this)是唯一剩下的对象时,引用计数只能为零。


C ++标准库中有一个模板。寻找std :: shared_ptr。


Hello!

I am working on the source code of Blender and trying to contribute, mainly through bug fixes. Now I have a problem: I am using Visual Studio and Blender crashes because of a dangling pointer (value 0xDDDDDDDD). Now I want to find the place where it is deleted or freed. Are there tools or solutions for that? Most tools seem to only search for memory leaks and so on, those, which find dangling pointers, don''t usually search the location where it is deleted or freed.
Thanks!

EDIT: Sorry for the double post, when I asked the first one, it somehow disappeared for a while, I thought it didn''t work, asked this question again. Then the first question was visible again, but it seems like there is a problem with it, the sub site is wrong somehow.

解决方案

I believe a 0xDDDDDDDD is an uninitialized pointer. There is no DDDDDDDD address.

I believe Visual Studio inserts this phony address as a tip-off should the pointer never be initialized.


Read this, there it says that 0xDDDDDDDD means deallocated memory. https://www.softwareverify.com/memory-bit-patterns.php


Yes. it''s slowly coming back. 0xDDDDDDDD is deallocated memory and the 0xCCCCCCCC is the uninitialized pointer.

OK. So here goes. You need to stop using pointers. Instead you use a smart pointer. A smart pointer is a class enveloping a pointer such that each time a pointer object is copied the constructor increments a reference count and each time a pointer object is deleted, the destructor decrements the reference count. Should the reference count become zero, the destructor will delete the memory pointed at by the enveloped pointer. The reference count can only be zero when the current object(this) is the only object left.

There is a template for this in the C++ Standard Library. Look for std::shared_ptr.


这篇关于C ++找到悬空指针删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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