2

Hi I have the following code

NSString *analyticsStr = [[NSString alloc] initWithString:[self constructXMLMessage:TagObj]];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
                                                                        selector:@selector(sendAnalyticsString:)
                                                                          object:analyticsStr];
[operationQueue addOperation:operation];
[analyticsStr release];
//[operation release];

when I uncomment [operation release] my app crashes. And I get this error :

malloc: * error for object 0x726ed50: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug

I was of view that NSOperationQueue takes care of retaining objects. is there something I am doing wrong or not aware of.

Asad Khan
  • 11,469
  • 13
  • 44
  • 59

1 Answers1

3

Use Instruments's Zombies template to debug this. A flag will appear in the timeline when you send an object a message after it should have deallocated; you can click the button in that flag to begin investigating what unduly released the object.

By the way, you don't need to create that string object. The string that constructXMLMessage: returns will last as long as the current autorelease pool, which should be all the time you need to work with it. It won't suddenly die on you.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • re: "The string that `constructXMLMessage:` returns will last as long as the current autorelease pool, which should be all the time you need to work with it" -- you mean that it'll last long enough to pass to the NSInvocationOperation, which would then retain it, right? Even though the operation will use a retained version, the (current) autorelease pool might have gone away by the time the operation runs. – Richard Jan 20 '11 at 15:40
  • Good point; I'd missed that. Yes, the docs for NSInvocationOperation say that the designated initializer tells the invocation to retain its arguments, so the string should be safe even after the pool drains, until the operation has finished. – Peter Hosey Jan 20 '11 at 16:36