Reputation: 2489
When I use autorelease with NSURLRequest the application crashes.
NSURLRequest *getRequest = [[NSURLRequest requestWithURL:[NSURL URLWithString:query]] autorelease];
I am new to obj-c and I am not sure how the memory management works. DO I need to manually do a [getRequest release]
?
And if so, when should I do it... right after I create the NSURLConnection?
Or does NSURLConnection release the request?
A related question I have is with NSMutableArray. When I add an object into the array, do I need to do a release of that object after adding it to the array? What is the memory life cycle of objects added to the array?
Upvotes: 1
Views: 1248
Reputation: 1916
[NSURLRequest requestWithURL:[NSURL URLWithString:query]]
returns an autoreleased object, so you don't have to add an autorelease message
otherwise you can retain it in a class attribute and release it later
Upvotes: 0
Reputation: 2547
study this thoroughly before you code in ObjC http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html.
Answers to your questions :
requestWithURL gives an autoreleased object , hence no need to release/autorelease (as a thumb rule, most of the class (factory) methods provide autoreleased objects - check the documentation before releasing them).
No need to explicitly release objects added to Array/Dictionary. They are released when deleted from Array/List or deleting the collection. (in essence add to array + deleting from array will balance out)
Upvotes: 2
Reputation: 48398
You release an object once you (or the current routine) are finished with it. You do not need to release if you use autorelease (as the name may well imply).
Upvotes: 1