iOS5 sdk and memory leak doubts

Discussion in 'iOS Programming' started by jagatnibas, Oct 20, 2011.

  1. jagatnibas macrumors regular

    Joined:
    Jul 28, 2008
    #1
    I am trying to download iOS5 sdk. The account I login with is an administrator account created for me by my team agent. I see 2 links, one is xcode 4 for lion and then xcode 4 for snow leopard, but when i click, i get access denied message. code P1 contact support ! when i try download xcode 4 for lion, it does not download rather says need 10.7, I am on 10.6.8 Whats d solution and is it that only team agents can download iOS5 sdk ?

    I have 2 situations.

    i have an array alloced and retained as member variable. i release it in dealloc. i create a button by alloc and add this to member array, if i do not release button, do I leak ? static analizer says so ! what is the best way to fix leak in this case ?

    thanks and regards
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Yes. You alloc/init. The retain count is 1. You own this object. You add it to the NSArray. This retains it. Retain count is 2. You both own the object. You release the NSArray. It releases the button. Retain count is now 1 and you still own it. You have to release it.

    The simplest thing to do is to autorelease it before adding it to the NSArray.
     
  3. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #3
    I feel it is good practice for new developers to learn to avoid using the autorelease pool purely for convienience. Developers should learn to either call retain and release or let ARC handle things instead of putting extra work on the application by using the pool.
     
  4. jagatnibas thread starter macrumors regular

    Joined:
    Jul 28, 2008
    #4
    Ok, thanks gentlemen,

    Now a few case studies where I should know whther i am leaking or best practice shot off. Please advise, thanks in advance

    I am doing a custom cell and using following code

    Static analyser gives a warning "receiver of frame is nil and hence returns garbage CGRect" ? why so ? I lblText is an outlet and I am setting outlet through nib file. When I run the code, there is no problem in functionality, it is only that static analyser in sdk 5.0 shouts here.

    Code:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    	
        static NSString *CellIdentifier = @"myCell";
        
        MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            
            NSArray * topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
            
            for(id currentObject in topLevelObjects)
            {
                if([currentObject isKindOfClass:[UITableViewCell class]])
                {
                    cell = (MyCell *)currentObject;
                    break;
                }
            }        
        }    
    	
        if(selectedIndex == indexPath.row)
        {
    		CGFloat labelHeight = 100;
    		[B]CGRect rct = [[cell lblText] frame];[/B]
    		[[cell lblText] setFrame: CGRectMake(rct.origin.x, 
    												 rct.origin.y, 
    												 rct.size.width, 
    												 labelHeight)];
    [[cell lblText] setFrame:@"My Text"];
    }
    }
    


    Secondly,

    If I have a few IBOutlets in a class, where should I call [outletobject release] ?
    viewDidUnload or dealloc ? And where should I assign them to nil ?

    if I have done alloc to a nonatomic retained variable in viewDidLoad where should I release them ? viewDidUnload or dealloc ?

    in these cases static analyser is not shouting, though, I wanna do the best practice.

    regards
     
  5. jagatnibas, Oct 24, 2011
    Last edited by a moderator: Oct 24, 2011

    jagatnibas thread starter macrumors regular

    Joined:
    Jul 28, 2008
    #5
    Also, if I am using

    Code:
    UIImage *img = [UIImage imageNamed:@"myImage.png"];
    after the usage is over, should i release img or not ?

    Do I own img this way ?
     
  6. robbieduncan, Oct 24, 2011
    Last edited by a moderator: Oct 24, 2011

    robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    Read the Basic Memory Management Rules. Then answer your own question.
     
  7. jnoxx, Oct 24, 2011
    Last edited by a moderator: Oct 24, 2011

    jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #7
    No you don't, you used an Convenience method, which means you give the ownership to the compiler, it will release it itself.
     
  8. jagatnibas thread starter macrumors regular

    Joined:
    Jul 28, 2008
    #8
    Thanks a lot, I would be happy if you all know the static analyser warning or errors i posted,

    regards
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    We know them. And robbieduncan as already posted a response, way back in post #2.
     
  10. jagatnibas thread starter macrumors regular

    Joined:
    Jul 28, 2008
    #10
    I am sorry if i could not make the point, I am talking about custom cell code in my post #4 and outlet questions !
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    Well, this looks suspicious:
    Code:
    [[cell lblText] setFrame:[COLOR="Red"]@"My Text"[/COLOR]];
    setFrame: is expecting a CGRect not an NSString.
     

Share This Page