简体   繁体   English

iPhone调试帮助-何时释放对象?

[英]iphone debugging help- see when objects are released?

I'm a little ways into developing my first iphone app, and I've been running into exc_bad_access a lot. 我在开发我的第一个iPhone应用程序时有些办法,并且已经遇到了exc_bad_access很多。 (I'm sure it's because I'm not designing stuff in a very MVC-proper way yet.) (我确定这是因为我还没有以非常适合MVC的方式来设计东西。)

It's extremely frustrating trying to hunt down why I'm running into these errors all the time... is there a way in the xcode debugger to keep a watch list of pointers, so you can actually see when various objects are being released? 试图找出为什么我一直都遇到这些错误是非常令人沮丧的... xcode调试器中是否有一种方法可以保留指针的监视列表,因此您实际上可以看到何时释放了各种对象? Something, anything that can give me more of a visual understanding of why my program design is flawed? 有什么可以让我更加直观地了解为什么我的程序设计有缺陷的东西?

In particular, I keep having trouble with my "webobject" class I've wrapped all of my NSURLConnection methods in. When I try to call performselector to the view controller that initiated a "webobject" request, I keep getting exc_bad_access when I try to access the webobject's synthesized properties. 特别是,我将所有NSURLConnection方法都包装在我的“ webobject”类中时遇到了麻烦。当我尝试向执行“ webobject”请求的视图控制器调用performselector时,当我尝试执行以下操作时,我总是收到exc_bad_access访问Web对象的综合属性。 Is there something fundamentally wrong with my notion that I should wrap all of my webservice-related methods into a class that I can import anywhere? 我应该将所有与Webservice相关的方法包装到可以导入到任何地方的类中,这在根本上有什么错吗?

Dave, 戴夫

I don't know of a way to trace the deallocation of objects (especially native objects like NSStrings). 我不知道一种跟踪对象(尤其是本机对象,例如NSStrings)的释放的方法。 But, I experienced a similar abundance of exc_bad_access messages when starting Objective-C programming. 但是,在启动Objective-C编程时,我遇到了类似的exc_bad_access消息。 But one day I read something that helped me alot to get rid of these messages: 但是有一天,我读到了一些可以帮助我摆脱这些信息的东西:

If you create an object using alloc, copy, or a method that begins with new, then it is your object to manage and dealloc. 如果使用alloc,copy或以new开头的方法创建对象,则要管理和取消分配它的对象。 You can call retain and release and that cycle will work as expected on those objects. 您可以调用保留和释放,并且该周期将在这些对象上按预期工作。 If you never call release or dealloc on it, it will hang around foreever and be a leak. 如果您从未在其上调用release或dealloc,它将永远徘徊并成为泄漏。 If you call a method that is something like 'stringWithFormat', numberWithYadaYada then that object is set for autorelease. 如果您调用的方法类似于“ stringWithFormat”,numberWithYadaYada,则该对象将设置为自动释放。 Meaning, as soon as you exit the method that is using it, it may get dealloced. 意思是,一旦退出使用它的方法,它就可能被取消分配。 Therefore, if you've called one of these autorelease methods, you must retain it if you want to see it later on. 因此,如果调用了这些自动释放方法之一,则以后要查看时必须保留它。

I suspect that either your delegate or NSURLConnection is getting autoreleased, that is the cause of your bad access messages. 我怀疑您的委托或NSURLConnection会自动释放,这是导致访问消息错误的原因。

Jack 插口

You could try creating a breakpoint on -[NSObject dealloc] (or the same method on the subclass you are targetting). 您可以尝试在-[NSObject dealloc] (或您要定位的子类上的相同方法)上创建断点。

I would also refer you to one of the many Cocoa memory handling tutorials. 我还将推荐您参阅许多Cocoa内存处理教程之一。

I ended up reading up about NSZombieEnabled, which has helped me track down those exc_bad_access messages. 我最终阅读了有关NSZombieEnabled的文章,该文章帮助我跟踪了这​​些exc_bad_access消息。 More info here: http://www.cocoadev.com/index.pl?NSZombieEnabled 此处提供更多信息: http : //www.cocoadev.com/index.pl?NSZombieEnabled

Just as a general thing, you should run as a static analyzer and it will probably tell you when you are over or under retaining objects. 通常,您应该以静态分析器的形式运行,它可能会告诉您何时超过或低于保留对象。

pertinent stackoverflow question 相关的stackoverflow问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM