How can i force a autoreleasepool to release my autorelease object which was created outside the autoreleasepool {}

the code im using

- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection {

    NSError *error = nil;
    id response = [NSJSONSerialization JSONObjectWithData:responseData options:nil error:&error];
    [responseData release];
    if (error) {
            NSLog(@"ERROR JSON PARSING : %@", error.localizedDescription);
    }

    [delegate databaseUpdates:response connection:self];
}

- (void)databaseUpdates:(id)_updates connection:(URLConnection *)_urlConnection {
    if (_updates) {

        NSDictionary *updates = nil;
        @autoreleasepool {

            updates = [[_updates valueForKey:@"objects"] retain];

            //Release _updates here!?!
        }
    }
}

Thanks a lot

有帮助吗?

解决方案

Simply call autorelease while in the scope of the autorelease pool, that will automatically add the object to the pool. Though, it looks like you are trying to solve the wrong problem here. If you really mean _updates, then that shouldn't be memory management by the method but by the caller (and it already is! JSONObjectWithData:options:error: already returns an autoreleased instance), and if you mean updates, well, simply don't retain it.

其他提示

It is not possible. I think you need this

- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection {
    @autoreleasepool {
        NSError *error = nil;
        id response = [NSJSONSerialization JSONObjectWithData:responseData options:nil error:&error];
        [responseData release];
        if (error) {
                NSLog(@"ERROR JSON PARSING : %@", error.localizedDescription);
        }

        [delegate databaseUpdates:response connection:self];
    } // response will be released here
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top