views:

144

answers:

1

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.

A: 

First of all you never force deallocate. If you follow the memory management rules closely then you shouldn't even have to worry about it. For the rules Look at:

http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-SW1

If your app is crashing it sounds to me more like something is being released early. Probably the object which you are setting as the delegate for your connection. Last but not least have you looked at ASIHTTPRequest for handling connections? It's a CFNetwork wrapper, very simple to use and very powerful.

Hopes this helps.

Alej
I've checked again and again at what I'm allocating and releasing and I don't see anything wrong with it. I've also looked at the retainCount but I don't understand what is making it go up and down. When it becomes a delegate of another class, does that go up? (And if so, how do I get that delegating class to release it?)
RyanJM
It took my awhile to track down my error. In the end, it was a memory allocation error, but not in the files I was directly working with. In another class I had a for loop in which I was using quick iteration and did for(MyObject *tempObj in myArray) and I was attempting to release that object at the end of the loop. And as the rules go, I didn't allocate it, so I shouldn't release it. Thanks for your help.
RyanJM