Nic Hubbard
Nic Hubbard

Reputation: 42139

iPhone: message sent to deallocated instance error

I have narrowed down this error (which crashes my app):

-[NSConcreteMutableData release]: message sent to deallocated instance 0x6eaed40

to the following code:

emailData = [kmlDoc dataUsingEncoding:NSUTF8StringEncoding];

But, I cannot figure out why this error is being caused? That line is just setting a very large string to an NSData object. I am releasing emailData in the dealloc method.

What is going wrong here?

Upvotes: 0

Views: 2197

Answers (1)

Georg Fritzsche
Georg Fritzsche

Reputation: 98984

You need to take ownership of the object:

emailData = [[kmlDoc dataUsingEncoding:NSUTF8StringEncoding] retain];

Or using retain/copy properties:

self.emailData = [kmlDoc dataUsingEncoding:NSUTF8StringEncoding];

Remember that you explicitly have to take ownership for returned objects from methods that contain neither new, alloc, retain or copy in their name as they return autoreleased instances.

See the Memory Management Guide for more.

Upvotes: 9

Related Questions