苹果框架内存泄漏
当我发布我的类时,我收到了 NSStrings 的泄漏。但我的属性中只有 2 个 NSString 。我在此类的方法中使用了其他 NSString,并在退出该方法之前释放它们。
[somestring release],somestring = nil;
这是屏幕截图
这是我的问题吗?或者这个泄漏属于苹果框架?我还在课堂上使用 ASIHTTPRequest。
编辑:我发现了问题。我使用了几次 NSMutableArray 和 mutableCopy 方法,没有自动释放。
之前和有泄漏: someMutableArray = [anotherClass.anotherMutableArray mutableCopy];
之后,并且没有泄漏: someMutableArray = [[anotherClass.anotherMutableArray mutableCopy]autoreleasing];
When i release my class, i receive leaks with NSStrings. But i have only 2 NSString in properties. Other NSString I use in my methods in this class, and i release them before exit from the method.
[somestring release],somestring = nil;
Here is screenshot
Is it my problem? Or this leaks belongs to apple frameworks? I also use ASIHTTPRequest in my class.
EDIT: I found the problem. I used few times NSMutableArray and mutableCopy method, without autoreleasing.
Before, and with leaks: someMutableArray = [anotherClass.anotherMutableArray mutableCopy];
After, and without leaks: someMutableArray = [[anotherClass.anotherMutableArray mutableCopy]autoreleasing];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
苹果的框架可能不会泄露。
如果您将该变量传递给另一个对象,并且另一个对象保留该变量而不释放,您将看到泄漏。它似乎是您分配字符串的位置,而不是在第二个对象中。
内存泄漏是我放弃 ASIHTTPRequest 的原因。我知道它来自 ASIHTTPRequest,但无法找出位置。
在这种情况下,您能做的最好的事情可能就是查看泄漏块之一的块历史记录。您将看到各种保留和释放。他们应该保持平衡;此外,每个保留都应该有一个与其对应的释放。例如,如果您在对象 setter 中看到对象的保留,则应验证该 setter 中是否已释放前一个对象,并且 dealloc 中是否有释放。尝试识别泄漏的最高级别对象,因为其他泄漏可能是它的成员变量。
Apple's frameworks are probably not leaking.
If you pass that variable to another object and that other object retains it without releasing, you'll see a leak. And it'll appear to be where you allocated the string, rather than in the second object.
Memory leaks are why I gave up on ASIHTTPRequest. I knew enough to know it was coming from ASIHTTPRequest, but wasn't able to find out where.
Probably the best thing you can do in this case is look at the block history for one of the leaked blocks. You'll see various retains and releases. They should balance; more, each retain should have a release it its counterpart. For instance, if you see a retain of an object in an object setter, you should verify that the previous object was released in that setter and there's a release in the dealloc. Try to identify the highest level object that's leaking, as the other leaks are probably member variables of it.
在乐器中,左侧有一个名为“调用树”的部分。当您选中“隐藏系统库”时,只会显示您的方法调用。您应该尝试一下,看看您自己的代码是否与泄漏有关。
In instruments, there's a section named Call Tree at the left hand side. When you check Hide System Libraries, then only your method calls show up. You should try this to see if your own code has anything to do with the leaks.
您需要在dealloc方法中释放变量。
You need to release the variables in the
dealloc
method.