有什么方法可以检查实例是否仍在内存中?

发布于 2024-08-23 03:59:33 字数 108 浏览 6 评论 0原文

示例:我有一个视图控制器并摆脱它。但仍然有一个变量保存它的内存地址。访问该结果会导致 EXEC_BAD_ACCESS。当然。但是:有什么方法可以检查该变量是否仍然有效?即它是否仍然指向内存中存在的东西?

Example: I have a view controller and get rid of it. But there's still an variable holding it's memory address. Accessing that results in EXEC_BAD_ACCESS. Of course. But: Is there any way to check if that variable is still valid? i.e. if it's still pointing to something that exists in memory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

回眸一笑 2024-08-30 03:59:33

你需要再读一遍:

Cocoa 内存管理指南< /a>

简而言之,如果你想让某些东西留在身边,你必须保留它。

如果您希望某些东西消失并且您之前保留了它,则必须释放自动释放它。

绝不能直接调用dealloc(除了在每一个dealloc末尾的[super dealloc];方法)。

绝不能释放自动释放您未保留的对象。

请注意,某些方法确实会返回必须释放的保留对象。如果您分配一个类的实例,则意味着保留。如果您复制并实例,则会保留副本。

如果您曾经想使用 retainCount 方法,请不要这样做。这没有用。仅将保留计数视为增量;如果你加,你必须减,但绝对值是一个应该被忽略的实现细节。

(换句话说,即使有办法明确检查对象的有效性——没有——这将是错误的答案。)

哦,使用构建和分析功能Xcode。它在识别内存管理问题等方面做得非常好,但还不是很完美。

You need to read this again:

Cocoa Memory Management Guidelines

In short, if you want something to stick around you must retain it.

If you want something to go away and you have previously retained it, you must release or autorelease it.

You must never call dealloc directly (except [super dealloc]; at the end of every one of your dealloc methods).

You must never release or autorelease an object that you did not retain.

Note that some methods do return retained objects that you must release. If you alloc an instance of a class, that implies a retain. If you copy and instance, the copy is retained.

If you are ever tempted to use the retainCount method, don't. It isn't useful. Only consider retain counts as a delta; if you add, you must subtract, but the absolute value is an implementation detail that should be ignored.

(In other words, even if there were ways to check for an object's validity definitively -- there aren't -- it would be the wrong answer.)

Oh, and use the Build and Analyze feature in Xcode. It does a very good -- but not quite perfect -- job of identifying memory management problems, amongst other things.

厌倦 2024-08-30 03:59:33

这就是整个内存管理模型的设置目的 - 如果您在正确的时间调用 retain,并在正确的时间调用 releaseautorelease ,那不可能发生。您可以使用 NSZombie 来帮助您调试。

That's what the entire memory management model is set up for - if you call retain at the right times, and release and autorelease at the right times, that can't happen. You can use NSZombie to help you debug.

迷你仙 2024-08-30 03:59:33

使用“NSZombieEnabled”断点。

正因如此,才强烈建议我们使用访问器。如果你的对象在任何地方被释放,它就会被赋值为 nil,并且在 Nil 对象上调用任何 API 或方法都不会造成任何损害。所以请养成使用访问器的习惯。

您只需将此 NSZombieEnabled Flag 作为参数添加到构建设置中的应用程序即可。并启用它。现在您可以在调试模式下运行应用程序。如果即将发生任何此类崩溃,此断点将显示哪个对象被释放以及它在哪里崩溃。

干杯,
曼朱纳特

Use "NSZombieEnabled" break point.

For this reason only all strongly recommend us to use accessors. If your object is released anywhere, it will get assigned to nil, and there will be no harm if you call any API or method on Nil object. So please make a habit of using Accessors.

you just add this NSZombieEnabled Flag as an argument to your application in build settings. and enable it. Now you run your application in debug mode. If any such crash is about to occur, this breakpoint will show you which object is freed and where it is crashing.

Cheers,
Manjunath

假装爱人 2024-08-30 03:59:33

如果通过变量,您的意思是指向对象的指针是否仍然引用有效内存,则:

MyClass *myVariable = [[MyClass alloc] init];

//发生了很多事情...

if (myVariable != nil)
//做更多的事情

If by variable, you mean whether the pointer to your object still references valid memory then:

MyClass *myVariable = [[MyClass alloc] init];

//Tons of stuff happens...

if (myVariable != nil)
//Do more stuff

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文