Adding object to dictionary and releasing memory.

Discussion in 'Mac Programming' started by Marimuthu, Jan 30, 2011.

  1. Marimuthu macrumors member

    Joined:
    Oct 18, 2010
    #1
    Hi All,

    My environment is Snow leopard 10.6.6 I am using Xcode for developing an application. I am relatively new to Cocoa programming.

    Wanted to know if it is correct to release an memory of an object immediately after the object has been set within an dictionary.

    I have provided the code snipped to illustrate the same.

    NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:dictionary1];//dictionary1 is some source dictionary.

    NSButton *button = [[NSButton alloc]initWithFrame:currentRect];//rect of some size .
    [tempDic setObject:button forKey:BUTTON_OBJ];
    [button release];//Can I call release here after assigning to Dictionary.


    Here after setting the button object to tempDic, I am releasing the object's memory. I plan to use the button object somewhere else. This code works as expected (i mean else where I am able to access the button object form the dictionary and it is valid).

    Is what i am doing right or should I not release the button object as the same reference is being set to the dictionary also?
     
  2. Toreddo, Jan 31, 2011
    Last edited: Jan 31, 2011

    Toreddo macrumors member

    Joined:
    Jul 18, 2009
    #2
    Yes this is right, but if this is the only button you want to access from a different method then this is not the right way to do it. You should then just declare the button outside of the method.

    EDIT: Sorry for the disinformation.
     
  3. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    NSButton doesn't have any class methods, and no class method initWithFrame. No class would ever have a class method starting with "init".
     
  4. Marimuthu thread starter macrumors member

    Joined:
    Oct 18, 2010
    #4
    Hi gnasher729,

    If i understand you correctly, I should not be calling [[NSButton alloc]initWithFrame:currentRect]; at all as it would be incorrect. Is it so.

    I think I have not understood Toreddo's comments completely.

    I have illustrated using an NSButton object, it could be an NSImage or any class which has been allocated an memory.

    NSButton *button = [[NSButton alloc]initWithFrame:currentRect];//rect of some size .
    [tempDic setObject:button forKey:BUTTON_OBJ];
    [button release];//Can I call release here after assigning to Dictionary.

    In the code above, is it legal/correct to (after setting the object onto a dictionary) release the object. Will it not make the dictionary's reference of the object illegal?.

    Sorry if the answer is obvious though not to me :).
     
  5. rossipoo macrumors regular

    Joined:
    Jun 7, 2009
    #5
    All containers retain any objects inserted in them, as do any views to which you add subviews like buttons; as long as your method or object no longer needs a reference to the button you can release your ownership of the button. The button will stay around as long as there is a view displaying it.
     
  6. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    Code:
    [[NSButton alloc]initWithFrame:currentRect];
    is absolutely correct. Toreddo posted that you should use

    Code:
    [NSButton initWithFrame:currentRect];
    which was incorrect and which he since deleted, causing this confusion.
     

Share This Page