Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-11-2010, 01:00 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 7
Default Some things of memory management

Hi. Im just beginner with objective c. I have a some problems with memory management. In here is some example code:
In the same method:
-(void)doSomething{

NSString *strTxt = @"This is txt";
NSString *strTxt2 = @"This is txt2";

NSMutableArray *maAllTxt = [NSMutableArray alloc] initWithObjects:strTxt, strTxt2

[maAllTxt removeObjectAtIndex:0 ];

NSLog(@"%@", strTxt);

}

The question is, when im printing strTxt to NSLog, has its memory alloc released already or did removeObjectAtIndex release only maAllTxt object at index 0? Is it the same with replaceObjectAtIndex- method?


Second question is, what is the difference with "self.variable" and "variable" if variable have add to class header file?

Last edited by Alec; 03-11-2010 at 01:03 PM. Reason: error fix
Alec is offline   Reply With Quote
Old 03-12-2010, 05:56 AM   #2 (permalink)
Mobile and Web solutions
 
hello24's Avatar
 
Join Date: May 2009
Location: London, UK
Posts: 72
Send a message via Skype™ to hello24
Default

strTxt is an autoreleased NSString.

BTW, there is a mistake in your code - make sure you have nil as the final element in initWithObjects.
NSMutableArray *maAllTxt = [NSMutableArray alloc] initWithObjects:strTxt, strTxt2, nil];

Also, there is a memory leak in there as maAllTxt is never released. You seem pretty confused about memory management - maybe you should read a relevant chapter in a good iPhone dev book.

self.variable uses the setter/getter whilst variable accesses your variable directly.
I recommend to always use self.variable as this makes sure that, when the value changes, the old content is released and the new one retained.
hello24 is offline   Reply With Quote
Old 03-12-2010, 06:40 AM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 267
Default

Quote:
Originally Posted by hello24 View Post

self.variable uses the setter/getter whilst variable accesses your variable directly.
Could you point me to a reference page that talks about that? Thanks
georgeburns is offline   Reply With Quote
Old 03-12-2010, 06:44 AM   #4 (permalink)
Mobile and Web solutions
 
hello24's Avatar
 
Join Date: May 2009
Location: London, UK
Posts: 72
Send a message via Skype™ to hello24
Default

Here is a pretty good explanation:
http://www.iphonedevsdk.com/forum/ip...es-newbie.html

Also, can you please consider changing your signature - I believe you're trying too hard to sell and forgot us, the poor users that are reading this forum.
hello24 is offline   Reply With Quote
Old 03-12-2010, 07:25 AM   #5 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 267
Default

Quote:
Originally Posted by hello24 View Post
Here is a pretty good explanation:
http://www.iphonedevsdk.com/forum/ip...es-newbie.html

Also, can you please consider changing your signature - I believe you're trying too hard to sell and forgot us, the poor users that are reading this forum.

Sorry... quoted the wrong part in my last reply.. I meant to quote this...
"I recommend to always use self.variable as this makes sure that, when the value changes, the old content is released and the new one retained. "

I wasn't aware that one would run the risk of not releasing something using direct access.
georgeburns is offline   Reply With Quote
Old 03-15-2010, 03:21 AM   #6 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 7
Default Thanks for reply

Thank you a lot for help and sorry my bad english. Now I understand my examples much better.
I found pretty good document from the websites of apple:
Mac Dev Center: Memory Management Programming Guide for Cocoa: Introduction
Alec is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 272
21 members and 251 guests
ADY, AragornSG, Bertrand21, Dani77, Dattee, fkmtc, HDshot, iDifferent, JasonR, jimbo, macquitzon216, mer10, prchn4christ, Rudy, sacha1996, silverwiz, sneaky, spiderguy84, Sunny46, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:08 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0