如何监视指向记忆泄漏的悬空指针?
让我们认为我们有这样的代码:
int * ptr = new int;
int *nptr = ptr
int * ptr2 = new int;
int *nptr2 = 2*nptr+ptr2;
delete ptr;
delete ptr2;
ptr = NULL;
ptr2 = NULL;
因此,现在NPTR和NPTR2正在悬挂指针。我知道我们可以超载新的分配函数以跟踪泄漏的内存(如果我们不使用代码中的delete命令),但不知道如何跟踪或监视悬挂的指针。
lets think we have a code like this :
int * ptr = new int;
int *nptr = ptr
int * ptr2 = new int;
int *nptr2 = 2*nptr+ptr2;
delete ptr;
delete ptr2;
ptr = NULL;
ptr2 = NULL;
So now the nptr and nptr2 is dangling pointer .I know that we can overload new allocating function to trace leaked memory (if we dont use delete command in code ) but dont know how to trace or monitor dangling pointers .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
泄漏的指针并未被任何事物指向。根据定义,悬空的指针指向尚未泄漏的内存。这是一个矛盾。
这是不明式的。指针不能乘以整数,并且指针不能添加到指针中(我假设假设是乘法不良的结果也是指针)。此外,如果此指针算术产生了
nptr
,nptr+1
,nptr2
,nptr2+1
,然后,从技术上讲,结果将是不确定的。悬挂的指针是完全很好的,很正常。不罚款的是通过悬挂的指针或依靠其价值来间接。至少在间接方面,您可以使用地址疗程器尝试捕获此类错误。它可以找到一些悬空指针的用途,尤其是那些是动态记忆的指示器,例如问题中的指针。它不太擅长检测到自动变量的悬空指针。另一方面,编译器可以捕获简单的案例,例如将指针返回到本地变量。
另外,您可以修改程序以使用
std :: shared_ptr
,以便只要您仍然持有共享指针,它就不会悬挂。这是一笔小成本的开销,但它使推理终身正确性变得更加容易。A pointer that has leaked, by definition is not being pointed by anything. A dangling pointer by definition points to memory that hasn't been leaked. This is a contradiction.
This is ill-formed. Pointers cannot be multiplied by integers, and pointers cannot be added to pointers (I'm assuming the assumption is that result of the ill-formed multiplication is also a pointer). Furthermore, if this pointer arithmetic produced anything other than
nptr
,nptr+1
,nptr2
,nptr2+1
, then the result would technically be undefined.It's perfectly fine and normal to have dangling pointers. What isn't fine is indirecting through dangling pointers or relying on their value. At least for indirection, you can use an address sanitiser to try catch such bugs. It can find some uses of dangling pointers particularly those to dynamic memory such as the pointers in the question. It's not as good at detecting dangling pointers to automatic variables. On the other hand, compiler can catch simple cases such as returning pointer to local variable.
Alternatively, you can modify your program to use
std::shared_ptr
so that as long as you still hold a shared pointer, it cannot become dangling. This comes at a small cost of overhead, but it makes it much easier to reason about lifetime correctness.首先,“指向记忆泄漏的悬挂指针”没有道理。我想您错误地将“泄漏的内存”用于已经被破坏的对象。在您的代码中,没有内存泄漏。 的指针时,您会有内存泄漏
当您进行不是 *nptr+ptr2; 。它不是有效的C ++,因此我将在其余答案中忽略它。同样,最后两行也不是那么相关(也许是关于Shwon代码之后的代码,但在这里也不是)。
因此,我们剩下:
您通过
new
创建两个int
。您删除
两个int
。没有内存泄漏。删除Ptr
指针NPTR
指向不再现有的对象。NPTR
被认为是一个悬空的指针。这样一来,我们就可以转向您的实际问题:如何监视悬空的原始指针?
你不能。原始指针是相当原始的。它存储一个地址。那就是全部。指针不知道该地址是否有一个对象。这是您不应使用
new
和删除
和原始指针来管理内存的原因之一。使用智能指针。例如,如果您在两个std :: shared_ptr
之间共享所有权,那么只要共享指针之一还活着,Pointee就会保持生命。共享的指针不能悬挂(除非您做错了什么)。First of all "dangling pointers that points to leakded memory" makes no sense. I suppose you mistakenly use "leaked memory" for objects that are already destroyed. In your code there is no memory leak. You have a memory leak when you do not have a pointer to some dynamically allocated obejct anymore, as in:
Further, it is not clear what this line is supposed to do
int *nptr2 = 2*nptr+ptr2;
. It is not valid C++, hence I am going to ignore it for the rest of the answer. Also the last two lines are not that relevant (maybe for code that comes after the shwon code but not here).So we are left with:
You create two
int
vianew
. Youdelete
twoint
. No memory is leaked. Afterdelete ptr
the pointernptr
points to a no longer existing object.nptr
is said to be a dangling pointer.With that out of the way, we can turn to your actual question: How to monitor dangling raw pointers?
You cannot. A raw pointer is rather primitive. It stores an address. And thats all. The pointer is not aware whether there is an object at that adress or not. This is one reason you should not use
new
anddelete
and raw pointers to manage memory. Use smart pointers. For example if you share ownership between twostd::shared_ptr
then the pointee will be kept alive as long as one of the shared pointers is alive. A shared pointer cannot dangle (unless you do something wrong).我建议您不要使用原始指针。如果您使用智能指针,那么您将永远不会(*)有一个悬挂的指针。智能指针的全部目的是确保指针的寿命和指向的东西都可以一起管理。
使用std :: unique_ptr,std :: shared_ptr(以及std :: feek_ptr(如果需要))。
您可以使用Valgrind和Addressanitizer之类的工具来确保您不使用悬空指针。
(*)也许从来没有太强,至少使它变得更加困难。
I would recommend that you not use raw pointers. If you use smart pointers that then you will never(*) have a dangling pointer. The whole purpose of smart pointers is to ensure that the lifetime of both the pointer and what is pointed to is managed together.
Use std::unique_ptr, std::shared_ptr (and std::weak_ptr if needed).
You can use tools like Valgrind and AddressSanitizer to ensure that you do not use dangling pointers.
(*) never is perhaps too strong, at least make it more difficult.