Jen
Jen

Reputation: 1

NSURLConnection

So why is it that you instantiate a NSURLConnection and then release it on the next line? I know that it involves the use of delegate, but I'm a little confused.

Upvotes: 0

Views: 1276

Answers (1)

Felipe Sabino
Felipe Sabino

Reputation: 18225

You should not release a NSURLConnection after instatiate it. Unless, off course, if you are using it as a retain property (as correctly pointed by @ArtGillespie)

When you instantiate it, the retain count will be only 1, and if you release it, the retain count will be set to 0, then the object will be cleaned from memory and you might be pointing to an invalid position in memory from that moment on.

If you have it as a retain property, you will be adding 1 to the retain count when assigning the property value. So your retain count will be 2 after instantiating and assigning. Then the right thing to do is release it so that the retain count goes back to 1, and you can properly release the object after the connection has finished (or set the property to nil, which will cause the object to be released as well)

Upvotes: 1

Related Questions