如何检测“悬挂指针”如果“Assigned()”不行吗 [英] How to detect "dangling pointers" if "Assigned()" can't do it?

查看:123
本文介绍了如何检测“悬挂指针”如果“Assigned()”不行吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在另一个问题,我发现 Assigned()函数与 Pointer<> nil 。一直以来我的理解是,分配()正在检测这些悬挂的指针,但现在我已经学会了。悬挂指针是可能在某一时刻创建的指针,但是已经被释放,并且尚未分配给 nil

In another question, I found out that the Assigned() function is identical to Pointer <> nil. It has always been my understanding that Assigned() was detecting these dangling pointers, but now I've learned it does not. Dangling Pointers are those which may have been created at one point, but have since been free'd and haven't been assigned to nil yet.

如果 Assigned()无法检测到悬挂指针,那可以怎么办?我想检查我的对象,以确保它真的是一个有效的创建对象,然后再尝试使用它。我不使用 FreeAndNil ,因为我喜欢直接。我只是使用 SomeObject.Free

If Assigned() can't detect dangling pointers, then what can? I'd like to check my object to make sure it's really a valid created object before I try to work with it. I don't use FreeAndNil as many recommend, because I like to be direct. I just use SomeObject.Free.

访问违规是我最大的敌人 - 我尽全力防止他们

Access Violations are my worst enemy - I do all I can to prevent their appearance.

推荐答案

如果你有一个对象变量的范围,它可能是也可能不是一个有效的参考,FreeAndNil是你应该使用这样或修复你的代码,以便您的对象引用被更加严格的管理,所以它永远不会是一个问题。

If you have an object variable in scope and it may or may not be a valid reference, FreeAndNil is what you should be using. That or fixing your code so that your object references are more tightly managed so it's never a question.

访问违规不应被视为敌人。他们是错误:它们意味着你犯了一个需要修复的错误。 (或者你依赖的一些代码中有一个错误,但是我经常发现我是一个搞砸的人,特别是在处理RTL,VCL或Win32 API时)。

Access Violations shouldn't be thought of as an enemy. They're bugs: they mean you made a mistake that needs fixed. (Or that there's a bug in some code you're relying on, but I find most often that I'm the one who screwed up, especially when dealing with the RTL, VCL, or Win32 API.)

这篇关于如何检测“悬挂指针”如果“Assigned()”不行吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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