werbary
werbary

Reputation: 1095

Troubles with ASIHTTPRequest

I'm trying to use wordpress api, but have unknown problem with my request: First request works perfect, but on second it crashes. My code:

//Response from request, which was before
        NSString *responseString = [request responseString];
        SBJsonParser *jsonParser = [SBJsonParser new];
        NSDictionary *dict = (NSDictionary*)[jsonParser objectWithString:responseString];
        [jsonParser release];
        int count = 10
        int loadingNow = 1;
        while (loadingNow <= count) {
            NSDictionary *currentPostDict = (NSDictionary *)[fullPosts objectAtIndex:(loadingNow - 1)];
            NSString *currentSlug = (NSString *)[currentPostDict objectForKey:@"slug"];
            if (loadingNow == 1) {
                NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.smartfiction.ru/api/get_post/?slug=%@", currentSlug]];
                NSLog(@"url: %@", url);
                ASIHTTPRequest *requestNew = [ASIHTTPRequest requestWithURL:url];
                [requestNew setDelegate:self];
                [requestNew setDidFinishSelector:@selector(requestDidFinishForThreadID:)];
                [requestNew setTag:1];
                [requestNew startAsynchronous];
                [url release];
            }
        }

My URL looks like: http://www.smartfiction.ru/api/get_post/?slug=best_friend

Error place: //In ASIHTTPRequest.m NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[[self url] absoluteURL]];

Upvotes: 0

Views: 357

Answers (3)

Jeff Wolski
Jeff Wolski

Reputation: 6382

Solution:

You should get rid of this line...

[url release];

What Happened?

Here's where the problem get's set up...

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.smartfiction.ru/api/get_post/?slug=%@", currentSlug]];

There's nothing wrong with what you did here. But this method returns an autoreleased NSURL. This means that once your url variable goes out of scope, it's fair game for the autoReleasePool to release it the next time the app runs through it's next cycle.

When you go and release it manually with [url release], it gets a retain count of zero and soon gets deallocated. Then the next time you try to access it, it's not there anymore, and the operating system shuts you down.

So, what's going on here?

If you create an object by sending an alloc message to a Class, you "own" that object and are responsible for releasing it at the appropriate time. Other than that, when you use one of these class constructors, the object is already autoreleased and you are not responsible for releasing it.

In general, you should pair up each alloc with either a release or an autorelease.

Upvotes: 2

Carl Veazey
Carl Veazey

Reputation: 18363

Since you did not get your NSURL instance through an alloc, new, or a copy, and didn't retain it explicitly, you do not have 'ownership' over the object. Releasing it without ownership is a memory management error. See http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994-BAJHFBGH for more details.

Upvotes: 1

BP.
BP.

Reputation: 10083

The line setting the NSURL *url is returning an autoreleased object, so you do not want to release it later in that series of statements.

Upvotes: 2

Related Questions