在我的一个函数中,我有一个while循环,在某些情况下,它可能需要临时创建一个对象。我的代码如下所示:
while(c < end){
if(specialCase){
Object *myObject = [Object alloc];
//do stuff with myObject
//I tried [myObject dealloc] here, but it crashed when this method was called.
}
c++;
}代码按原样运行良好,但我担心内存泄漏。我想知道我是否应该以及如何取消分配myObject。
发布于 2011-09-19 15:25:08
你永远不会直接调用Dealloc。
您调用Release,当保留计数达到0时,将在object上调用dealloc。
发布于 2011-09-19 15:25:50
不要直接调用dealloc方法,如果为E19对象满足iOS系统设置的条件(如果E115E216对象E216为零,则E113通常为),在对象为alloced或retain的对象上调用release会调用dealloc implicity。
阅读NSObject类中的dealloc方法的apple文档,并浏览objective-C的Memory Management Programming Guide
发布于 2011-09-19 15:33:26
尝尝这个
while(c < end){
if(specialCase){
Object *myObject = [[Object alloc] autorelease];
//do stuff with myObject
//I tried [myObject dealloc] here, but it crashed when this method was called.
}
c++;}
https://stackoverflow.com/questions/7467610
复制相似问题