Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone memory management, a newbie question

I've seen in (Apple) sample code two types of ways of allocation memory, and am not sure I understand the difference and resulting behavior.

// FAILS
NSMutableArray *anArray = [NSMutableArray array];
[anArray release];

// WORKS
NSMutableArray *anArray1 = [[NSMutableArray alloc] init];
[anArray release];

By "FAILS" I mean I get crashes/runtime warnings etc., and not always as soon as I call the release...

Any explanation appreciated.

Thanks

like image 830
Reuven Avatar asked Feb 15 '26 14:02

Reuven


1 Answers

Please keep in mind that

NSMutableArray *anArray = [NSMutableArray array];

acts like:

NSMutableArray *anArray1 = [[[NSMutableArray alloc] init] autorelease];

So doing a release again will cause the crash as you are trying to release an autoreleased object.

Hope this helps you.

Thanks,

Madhup

like image 138
Madhup Singh Yadav Avatar answered Feb 18 '26 05:02

Madhup Singh Yadav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!