我什么时候解除对象?

Wol*_*ink 0 iphone memory-management dealloc ios4 ios

在我的一个函数中,我有一个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++;
}
Run Code Online (Sandbox Code Playgroud)

代码工作正常,但我担心内存泄漏.我想知道是否以及如何释放myObject.

小智 5

你永远不要直接打电话给Dealloc.

你调用Release并且当retain count达到0时,将在object上调用dealloc.