Is this right way to handle memory?
Method 1: with no crash
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(220.0f, 7.0f, 57.0f, 35.0f)] ;
button = nil;
[button release];
Method 2: with crash
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(220.0f, 7.0f, 57.0f, 35.0f)] ;
[button release];
button = nil;
Both are incorrect. You should use the class method
buttonWithType: to construct buttons. It will set the type as you specify. Buttons shouldn’t be alloced/itited directly. (Maybe that’s where the crash co es from, though it shouldn’t)The class method will return an autoreleased object, so you must not release it.
Despite of that (that is with other objects and ordinary alloc/init), the second way is correct; the first leaks.