有什么方法可以检查实例是否仍在内存中?
示例:我有一个视图控制器并摆脱它。但仍然有一个变量保存它的内存地址。访问该结果会导致 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你需要再读一遍:
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
orautorelease
it.You must never call
dealloc
directly (except[super dealloc];
at the end of every one of yourdealloc
methods).You must never
release
orautorelease
an object that you did notretain
.Note that some methods do return retained objects that you must
release
. If youalloc
an instance of a class, that implies aretain
. If youcopy
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.
这就是整个内存管理模型的设置目的 - 如果您在正确的时间调用
retain
,并在正确的时间调用release
和autorelease
,那不可能发生。您可以使用NSZombie
来帮助您调试。That's what the entire memory management model is set up for - if you call
retain
at the right times, andrelease
andautorelease
at the right times, that can't happen. You can useNSZombie
to help you debug.使用“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
如果通过变量,您的意思是指向对象的指针是否仍然引用有效内存,则:
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