objective c - Creating and releasing objects in the same method, while using self as delegate -


In Objective-C, you are responsible for releasing allocated items, but when you assign an object to a method , Then leave the object as itself , and then leave the object.

Callback fails from the newly created (and released) object at this point, or rather, it does not happen.

- (zero) doSomething {MyObj * myObj = [[MyObj alloc] init]; MyObj.delegate = self; [MyObj display operation]; [MyObs release]; } - (zero) callbackMethodFromMyObj: (NSString *) Message {NSLog (message); } Edit: To make it slightly more detail, it is necessary to include MyObj using NSURLConnection.

MyObj:

  @implementation MyObj - (zero) connectionDefinish loading: (NSURLConnection *) Connection {[self.delegate callbackMethodFromMyObj: @ "a string"]; }  

Until I have a callback I can not release the object, and I can not save the object from releasing it in the same method as it creates (because it exists outside the scope) .

One way to do this is to pass the object to the call-back and release it in the callback, but is this the right way to go about it?

I have included the fact to update this post that I am using NSURL connection, so threading is included here.

If I have to release myObj, then there is a chance to call NSURLConnection - (zero) connectionDefinition Loading: (NSRU connection *) connection

As I have seen it, and from the answer given below, it seems that it is necessary to pass * myObj in the form of a parameter in the callback, thus it will be called the - (zero) callback in mindfood. (NSString *) message by

by changing the method signature, following As a result of Idi:

  - (minus) callback Mendfrmmebः (Ansstiarai ng *) MyObj with the message: (MyObj *) sender {// do things [myObj release]; }   

edit

If you select myobj < / Code> class, more specifically if you are controlling the method that invokes callbackModeFromMyObj: callback, you can treat MyObj as a short-term one-time operation object Who then releases himself, invokes his callback, i.e.

  / * we are inside the mios, some internal method * / NSString * message ; // Assume that [self.delegate callbackMethodFromMyObj: message]; [Self release]; // Add this  

In this way you do not leak specific arguments in the representatives concerning the lifespan of your MyOb example. Even better, because the reps are always optional (well, they should need !), Even if you do not provide a representative, MyOb will collect garbage properly.

Hope this is helpful.

The original answer

Your example is a little confusing, but if I understand it correctly, then you can use some type of command object (like MyObj < Code> NSIvvocation ) and execute any representation information (e.g. self ) with example.

If you are in the performance then there is a single thread in the form of doSomething , which is in operation, so you do not have to fear because callback calls before sending the release message Will be done.

It is confusing that you mention it ...

... What happens when you assign an object to a method, then the object representative Assign yourself as, and then release the object.

Object and then leave it without assigning it elsewhere (eg, a retaining property) so it will get deallocated immediately, it is no longer in existence and it is Display Operation does not give any context to the message, thus the representative is useless.

If you want to hold the object, keep it the property.

In order to make a representative more portable, I should be advised, as you mentioned earlier, you callback in representative (in your case MyObj ).


Comments