Memory Management question

Discussion in 'iOS Programming' started by AndyCodez, May 10, 2010.

  1. AndyCodez macrumors regular

    Joined:
    Aug 6, 2009
    #1
    Ok, I have been doing a few views like this where in one class i will:

    Code:
    myView *view = [[myView alloc] initwithFrame: blah];

    then within myView I get done doing what ever i'm doing and then i do [self release]. Will this properly deallocate the memory?

    It doesn't seem that dealloc gets called in said class :/
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    As far as I know, [self release] isn't recommended under any circumstances. In this case, your view would likely be disappearing before the result may be displayed. The views stability is highly questionable. The owner, the object or method, that created the instance of your view is the one responsible for releasing the view.

    If your view is being added as a subview to another view, then the minimum code might look like this;

    Code:
    MySubClassView * myview = [[MySubClassView alloc] initWithFrame: aRect];
    [myParentview addsubview: myview];
    [myview release];
    As far as knowing if dealloc is getting called, put in an NSLog() to see if it is. Simple solution.

    Go read Learn Objective-C over at Cocoadevcentral.com.
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    Also, be aware the method name is addSubview: and not addsubview:. Objective-C is case-sensitive.
     
  4. AndyCodez thread starter macrumors regular

    Joined:
    Aug 6, 2009
    #4
    Lol yes of course case sensitive. But say i don't want to create a property for said item. So I want to release said item later. So i have a delegate method like so

    Code:
    - (void) releaseItem:(id) item
    {
          [item release];
    }
    is this fair or should I not do this either?

    But then again I'm allocing
    Code:
    Item *item = [[Item alloc] initWithArray:arr];
    [item addViewto:self.view];  //adding view to parent (method within the Item class actually adds Item view to view that is passed to the method.)
    [item release];
    
    Would this be correct?
     
  5. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #5
    Your coding thought process is totally messed up. Go buy a decent Objective-C book. Hopefully it will set you straight. I'm not trying to be mean, but you are making stuff up that isn't required.

    Your delegate idea serves no purpose since you can just do
    Code:
    [item release];
    There is no need for the extra call since you already know the object, 'item', that you want to release.

    Ditto for your idea about creating a method in your custom view to add it to a parent view. That is a complete waste.
     

Share This Page