-1

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

Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
user1839842
  • 83
  • 1
  • 5

2 Answers2

3

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.

JustSid
  • 25,168
  • 7
  • 79
  • 97
  • Thanks, i will give it a try to call autorelease on _updates inside my autoreleasepool {}. – user1839842 May 08 '13 at 09:29
  • 1
    @user1839842: No, what JustSid is that you should not `release` or `autorelease` `_updates`. You do not release things you did not retain in that scope. – newacct May 08 '13 at 10:17
  • i found another way to reduce my memory warning problem. i removed the autoreleasepool in the databaseUpdates: method. The _updates object will be removed at the end of the databaseUpdates: method. Thanks for your help! – user1839842 May 09 '13 at 22:45
0

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
}
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143