PDA

View Full Version : Application is hangs. Why?




Sergio10
Apr 1, 2009, 07:36 PM
Hi,
This slider's control action function:

- (IBAction)dragSlider:(id)sender
{
NSNumber *tmpValue = nil;

if(_isChecked == YES)
{
// double value
tmpValue = [NSNumber numberWithInt: [ sender intValue ] *2 ];
[ _position setStringValue: [ tmpValue stringValue ] ];
}
else
{
tmpValue = [NSNumber numberWithInt: [ sender intValue ] ];
[ _position setStringValue: [ tmpValue stringValue ] ];
}

// free resources
[tmpValue release];
}


Why does application is hang?

Thanks.



kainjow
Apr 1, 2009, 07:48 PM
Either retain the NSNumber, or remove the [tmpValue release] - it's not necessary since the object is autoreleased.

Sergio10
Apr 2, 2009, 11:48 AM
You mean garbage collector is responsible for memory management?

kainjow
Apr 2, 2009, 12:02 PM
No, your tmpValue is an autoreleased object, which means it gets released automatically later on by the autorelease pool. If you're not familiar with this (very important) concept, you should read through this (http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html) and the other articles on memory management.

Darkroom
Apr 7, 2009, 01:55 AM
how can you tell from this code if the NSNumber instance is being autoreleased? is it because NSNumber is part of the foundation framework?

Catfish_Man
Apr 7, 2009, 02:18 AM
Do you see 'alloc', 'copy', or 'new' anywhere in the name of the method used to create it? If not, treat it as autoreleased.

mdeh
Apr 7, 2009, 07:31 AM
how can you tell from this code if the NSNumber instance is being autoreleased? is it because NSNumber is part of the foundation framework?


Here is a really nice article on memory managment

http://www.stepwise.com/Articles/Technical/2001-03-11.01.html

Darkroom
Apr 7, 2009, 08:07 AM
Do you see 'alloc', 'copy', or 'new' anywhere in the name of the method used to create it? If not, treat it as autoreleased.

oh right... i knew that, i swear...
:o