Objective-C object release and allocation timing

Posted by ryanjm.mp on Stack Overflow See other posts from Stack Overflow or by ryanjm.mp
Published on 2010-06-11T16:10:17Z Indexed on 2010/06/11 16:12 UTC
Read the original article Hit count: 439

The code for this question is too long to be of any use. But I'm pretty sure my problem has to do with releasing a class.

I have a helper class, ConnectionHelper.h/.m, that handles a NSURLConnection for me. Basically, I give it the URL I want and it returns the data (it happens to do a quick json parse on it too). It has a delegate which I set to the calling class (in this case: DownloadViewController). When it finishes the download, it calls [delegate didFinishParseOf:objectName withDictionary:dictionary];. Then in DownloadViewController I release ConnectionHelper and alloc a new one in order to download the next object.

My problem is, I do this once, and then it creates the connection for the second one, and then my program just crashes. After this call:

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyNever];  
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

Then I don't think any of the following methods are called:

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection 
              willCacheResponse:(NSCachedURLResponse *)cachedResponse

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

So am I right in that I'm not releasing something? When I release it the first time, the dealloc function isn't being called. Is there a way I can "force" it to deallocate? Do I need to force it to? I didn't think it would matter since I allocating a new ConnectionHelper for the new call. How else would they overlap / conflict with each other?

Thank you.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about memory-allocation