Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Marimuthu

macrumors member
Original poster
Oct 18, 2010
43
0
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?
 
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?

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.
 
Last edited:
Also, in this case you want to use the dictionary, you can use
[NSButton initWithFrame:currentRect];
instead of
[[NSButton alloc]initWithFrame:currentRect];

The method will then auto release it.

NSButton doesn't have any class methods, and no class method initWithFrame. No class would ever have a class method starting with "init".
 
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 :).
 
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 :).

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.
 
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.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.