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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-28-2011, 09:13 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 32
y2kreddy is on a distinguished road
Default Memory management - Objective C.

Hello, i have some memory issues in my program and i have the following questions.

question 1) i have a member variable of type nsstring and i dont have any property for it , i used it without self.xxx in various methods to access it. i have not allocated any memory for this variable and i need to know do i still have to release it in dealloc and assign nil in viewdidunload.

question 2)

i have a member variable with (nonatomic , retain) property. i have not used this vairable outside the class anywhere whatsoever. in order to use it within various methods of the class is it necessary for me to allocate memory for it or should it self.membervariable.

question 3) Do all iboutlet's with property(nonatomic,retain) require self.outlet=nil in viewdidunload and [outlet release]; in dealloc;

question 4) what about iboutlet's without property , do i have to do outlet=nil in viewdidunload and [outlet release]; in dealloc.

i gone through lot of memory management tutorials. the more i read the more i get confused. at last i have settled with 4 above questions i dont understand.if anyone could explain in 2 lines for each question that would be great. Thanks.
y2kreddy is offline   Reply With Quote
Old 04-28-2011, 10:35 AM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

1. Depends on how you assign objects to the variable, but it's best to use properties so you don't have to manually manage it.
2. Allocation has to do with creation of an object, not use of a pointer variable, so I can't answer this really.
3. I generally just release IBOutlets in dealloc without setting them to nil
4. If it's without property then you must at least release it, setting it to nil will leak. It's best to use properties.

The best guide on Mem Management is the one in the documentation, by far. Here it is Loading…
baja_yu is offline   Reply With Quote
Old 04-28-2011, 10:49 AM   #3 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

1. If there is no property, there is no rule. You will have to make decisions based on what you did elsewhere in code. Having consistent rules is one of the reasons to use properties. Also:

Release in dealloc - only for retain and copy properties.
Set to nil in viewDidUnload - only for IBOutlets and/or items that are created in viewDidLoad.

2. Declaring a property does not create an object. You will need to create one separately.

3. "Required" isn't the right word. If you don't release things in dealloc, then you will leak memory. If you don't nil out IBOutlets, then you are not reducing your memory usage as far as you could be. Those are consequences for failing to perform those actions. Is it "required" to put the toilet seat down? No. Will your wife chew on you if you fail to do so? Yes. Will life be easier for you if you just put it down? Yes.

4. Use properties for them, and then you don't have to worry about it.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 04-28-2011, 11:13 AM   #4 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 1,106
Meredi86 is on a distinguished road
Default

Quote:
Is it "required" to put the toilet seat down? No. Will your wife chew on you if you fail to do so? Yes. Will life be easier for you if you just put it down? Yes.
LOL Ah a smile on my face at the end of the week - thats rare!!

I would like to interject a little question here if i may? I have this method set up (works no issues just a mem management question):
Code:
- (void) ipCuIdCount {
    int n;
    int ipCuIdInt = 0;
    for (n = 0; n <= count; n = n + 1) {
        ipCuIdInt = n;
        //NSLog(@"the int: %i", ipCuIdInt);
        ipCuIdString = (NSString*)ipCuIdInt;
        //NSLog(@"HERE: %@", ipCuIdString);
    }
    
}
And in that method this is set up as a property like so:
Code:
@property (nonatomic, retain) NSString *ipCuIdString;
I release this in dealloc as i believe i should. the question comes with the use of "self" Brian has hinted to me in the past that if i do not "self." or [self var] then i will not be using the property correctly. So when i was messing with my code this morning i added self so the method looked like this:
Code:
- (void) ipCuIdCount {
    int n;
    int ipCuIdInt = 0;
    for (n = 0; n <= count; n = n + 1) {
        ipCuIdInt = n;
        //NSLog(@"the int: %i", ipCuIdInt);
        self.ipCuIdString = (NSString*)ipCuIdInt;
        //NSLog(@"HERE: %@", ipCuIdString);
    }
    
}
And this resulted in a crash every time i ran the code without fail (EXC_BAD_ACCESS) and Zombies wouldnt tell me what from. Would anyone be able to explain that to me as i dont really understand what was going on there.

thanks Joe
Meredi86 is offline   Reply With Quote
Old 04-28-2011, 11:18 AM   #5 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Putting (NSString *) in front of an integer does not convert the integer into a string.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 04-28-2011, 11:20 AM   #6 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 1,106
Meredi86 is on a distinguished road
Default

ok so because i am just casting it to the string rather than converting it if i force it into the property - a string then it doesnt like it? I think i get that, and may have to alter that method if i get a chance to make it a bit more robust.

thanks Brian
Meredi86 is offline   Reply With Quote
Old 04-28-2011, 11:21 AM   #7 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Code:
self.ipCuIdString = [NSString stringWithFormat:@"%d", ipCuIdInt];
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 04-28-2011, 11:26 AM   #8 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 1,106
Meredi86 is on a distinguished road
Default

Awesome, thanks saves me digging through my code to find the line :P
Meredi86 is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone, memory management, objective-c

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: 338
12 members and 326 guests
dansparrow, iOS.Lover, lorrettaui53, MikaelBartlett, Nobbsy, oztemel, pbart, PlutoPrime, samdanielblr, sledzeppelin, thephotographer, Trickphotostudios
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

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