Re: Cocoa view gets sluggish over time
Re: Cocoa view gets sluggish over time
- Subject: Re: Cocoa view gets sluggish over time
- From: aglee <email@hidden>
- Date: Mon, 20 Dec 2010 16:31:28 +0000 (GMT)
On Dec 20, 2010, at 09:13 AM, Artemiy Pavlov <email@hidden> wrote:
NSTextField *PatternStepLabel = [[NSTextField alloc]
initWithFrame:NSMakeRect(x, y, 20, 20)];
NSString *PatternStepLabelString = [NSString stringWithFormat:@"%d",
i];
[PatternStepLabel setEditable:NO];
[PatternStepLabel setDrawsBackground:NO];
[PatternStepLabel setSelectable:NO];
[PatternStepLabel setBezeled:NO];
[PatternStepLabel setAlignment:2];
[PatternStepLabel setTextColor:StepColor];
[PatternStepLabel setStringValue:PatternStepLabelString];
[PatternStepLabel setFont:[NSFont fontWithName:@"Courier" size:8.0]];
[self addSubview:PatternStepLabel];
[PatternStepLabelString release];
[PatternStepLabel release];
You seem to have solved your main problem, so I'll just comment on a couple of other things.
(1) You are over-releasing PatternStepLabelString.
stringWithFormat: returns an object you do not own, which means you are not responsible for releasing it. In fact, you must not release it, because releasing an object too many times will cause your program to crash.
Note that it is correct to release PatternStepLabel, because alloc returns an object that you do own. You must release it (either right away or later via autorelease), or it will be a memory leak.
See <http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html>, in particular "Memory Management Rules".
(2) Unless you need PatternStepLabelString for something else, you can eliminate it altogether by using setIntValue: instead of setStringValue: as follows.
[PatternStepLabel setIntValue:i];
(3) The convention in Cocoa is to begin variables with lowercase letters, like this:
NSTextField *patternStepLabel;
NSString *patternStepLabelString;
If you work with other Cocoa developers, or even if you paste code into emails like this one, you can make the code easier for others to read by sticking to convention.
--Andy
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden