Rob Bonner
Rob Bonner

Reputation: 9424

Should I release NSData dataWithContentsOfURL?

Question on memory mgmt of the following:

NSData *returnData = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]                                                options:0                                                     error:&err];

We are seeing our allocations spike here, but not sure if I should be releasing this memory after I have moved it off.

I get an exception when I try to release, so not understanding something about the internals here.

Thanks in advance!

Upvotes: 1

Views: 925

Answers (2)

aroth
aroth

Reputation: 54816

No, you don't need to release it. The dataWithContentsOfURL: method returns an autorelease object. It will be release automatically if you do not explicitly retain it.

Automatically, that is, so long as your current thread has an NSAutoreleasePool properly set up for it. All autorelease objects are released when their enclosing NSAutoreleasePool is drained. If you have found a leak in this code, then perhaps the corresponding pool is not being drained frequently enough (or perhaps not at all).

Upvotes: 0

Eiko
Eiko

Reputation: 25632

No. It is returned autoreleased per naming convention.

You might check the memory management programming guide: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html

Upvotes: 1

Related Questions