If I have a code looked like this:
-(void) func {
ObjectA* A = [[ObjectA alloc]init];
[something doSomething:blah andDelegate: A];
}
Assuming the call of doSomething will make a http request call so the delegate will be called only when it received response from the server. In this case, there would be a delay.
Note: something is an instance variable of a class.
If I call ‘func’ twice, will the first initialized of A be leaked before it received the response on the delegate. Assuming there is a release operation when calling the delegate function when received the responsed.
The reason I thinking of this is because if second initialized of ‘A’ passed in to something as an delegate before the first delegate finished it’s role. Will second initialized of ‘A’ replaced the first initialized of ‘A’?
Yes, if you’re not compiling with ARC, you have a leak. You’re creating an object with
alloc, which means that you own it, and you’re not relinquishing that ownership by sendingrelease. This is the core memory management rule for Cocoa.It may be that the object,
something, to which you’re passingA, also needs to ownA(in fact, it sounds like that is the case). If so,somethingshould sendretaintoAand then sendreleasewhen it no longer needsA.Sort of. The name
Ais only valid inside this method. When you create an object and assign it toA, and then that name goes out of scope, you can’t refer to the object anymore. That’s what a leak is. When you run this method again, essentially a new nameAis created and you assign another object to it.