XCode's Build and Analyze - mistake or correct in finding Potential Leaks? - xcode

How reliable is Xcode's "Build and Analyze" ?
I keep looking for the "potential leak" that it finds - but don't understand it.
Here the code:
-(void)viewDidLoad{
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *aBI = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:#selector(add:)];
self.navigationItem.rightBarButtonItem = aBI;
aBI.release;
UIBarButtonItem *eB = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
target:self action:#selector(EinstellungenTapped:)];
UIBarButtonItem * space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *emailB = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCompose
target:self action:#selector(emailTapped:)];
NSArray* toolbarItems = [NSArray arrayWithObjects: eB,space,emailB,
nil];
[self setToolbarItems: toolbarItems];
eB.release; // <------- Potential leak ??????
emailB.release; // <------- Potential leak ??????
space.release; // <------- Potential leak ??????
}
Why should these three be potential leaks?
Even without the assignemt to the NSARRAY it's telling me about leaks.
What is wrong here?
many thanks....

I imagine the problem is because you're horribly mis-using dot syntax and confusing the static analyzer. You should never ever type anObject.release, because that has the completely wrong meaning. Dot syntax is designed for accessing properties, and has that specific semantic meaning, e.g. accessing a property. Anything which modifies the object itself should be a method, not a property, and should be called as such.
In other words, change those to
[eB release];
[emailB release];
[space release];
and things should work fine. Now the static analyzer should recognize that you really are releasing the objects.
As one final note, the fact that eB.release technically works can be considered an artifact of how the compiler works. While I don't believe that will ever break, I also don't know if it's guaranteed not to, and it's very bad form to rely upon.

Related

Leak when autoreleased in return

I found this strange thing. XCode instruments tell me that this line
return (SDZPerson*)[[[SDZPerson alloc] initWithNode: node] autorelease];
leaks. But if i change it to:
SDZPerson* person = [[[SDZPerson alloc] initWithNode: node] autorelease];
return person;
Instruments no longer report leak in this place. Is it really a leak or is it nothing?
Thank you all in advance.
Even if the two code snippets are slightly different (the 1st one contain a typecasting), I doubt the instruments indicate this as a leak due to the autorelease in the return statement.
Adding autorelease in the return statement can be used.
You should give it a try to
SDZPerson* person = [[SDZPerson alloc] init];
/** do your stuff here if needed */
return [person autorelease];
or even
return [[[SDZPerson alloc] init] autorelease;

componentsJoinedByString gives me EXC_BAD_ACCESS

I have an NSMutableArray i am trying to convert into a string.
Declaring my NSMutableArray...
NSMutableArray *listData;
And later inside a method...
NSString *foo = [listData componentsJoinedByString:#"|"];
NSLog(#"%#",foo);
It seems no matter what i try i keep getting EXC_BAD_ACCESS.
To make sure each element in my array was an NSString i also tried this...
NSMutableArray *mArray = [[NSMutableArray alloc] init];
for (id ln in listData) {
NSString *boo = [NSString stringWithFormat: #"%#",ln];
[mArray addObject:boo];
}
NSString *foo = [mArray componentsJoinedByString:#"|"];
NSLog(#"%#",foo);
I can manipulate my NSMutableArray by adding/deleting objects in the same method or other methods inside my class. But when i try "componentsJoinedByString" the error pops up. Does anyone have any advice or another way i can combine this array into a single NSString?
In the code you've given, there will never be an NSMutableArray for listData. At some point in your code, you'll need to create one, and presumably populate it.
Edit
Okay, so you may get into memory management problems here, so let's be a bit clearer:
You're synthesizing getters and setters for the instance variable, so it's good practice to use those to access it, they'll take care of retain and releasing appropriately.
To set listData you can simply use
self.listData = [listManage getList:[[NSUserDefaults standardUserDefaults] stringForKey:#"list_name"] list:#"LIST"];
or
[self setListData:[listManage getList:[[NSUserDefaults standardUserDefaults] stringForKey:#"list_name"] list:#"LIST"]];
if you prefer.

Cocoa (Snow Leopard) NSTextView's textStorage -setAttributes:range: removes characters!

I'm not sure what I'm doing wrong. I have a NSTextView and am registered as the delegate for its textStorage attribute. When I receive -textStorageDidProcessEditing:notification: I'm trying to apply attributes to ranges of characters within the text. It certainly does "something" to the characters, but not what I expect... they just disappear!
A heavily distilled code example. This should make sure the second character in the text field is always red:
-(void)textStorageDidProcessEditing:(NSNotification *)notification {
NSTextStorage *textStorage = [textView textStorage];
if ([[textStorage string] length] > 1) {
NSColor *color = [NSColor redColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:color, NSForegroundColorAttributeName, nil];
[textStorage setAttributes:attributes range:NSMakeRange(1, 1)];
}
}
Instead, as I type the sequence "abcdefg" I get "a", then when I hit "b" seemingly nothing happens, then when I hit "cdefg" typing occurs as normal, making the end result "acdefg"... the "b" is missing!
If I start hitting backspace I have to hit backspace 7 times, as if the "b" is actually there, but just not being drawn (cursor stalls as it deletes the "b", then on the next backspace deletes the "a" as expected).
If I apply attributes to some default text in the view using the same -setAttributes:range: method before the view is drawn then it does exactly as I expect.
Any clues? It seems like a fairly normal use of a NSTextStorageDelegate :)
I've tried calling -setNeedsDisplay on the text field to no avail.
Figured it out. Using NSTextStorage's -addAttribute:value:range works. I still don't fully understand why but at least I can get over it and move on.
-(void)textStorageDidProcessEditing:(NSNotification *)notification {
// ... SNIP ...
[textStorage addAttribute:NSForegroundColorAttributeName
value:[NSColor redColor]
range:NSMakeRange(1, 1)];
}
Makes the code a bit less cluttered too.
I'm not sure how relevant this is for you after so many years but I think the reason for it was that you were setting attributes with a dictionary which does not contain NSFontAttributeName, effectively removing it from the textview.
So I think this should work:
-(void)textStorageDidProcessEditing:(NSNotification *)notification {
NSTextStorage *textStorage = [textView textStorage];
if ([[textStorage string] length] > 1) {
NSColor *color = [NSColor redColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:color, NSForegroundColorAttributeName, [NSFont ...whatever...], NSFontAttributeName, nil];
[textStorage setAttributes:attributes range:NSMakeRange(1, 1)];
}
}

Placing an NSTimer in a separate thread

Note: It's probably worth scrolling down to read my edit.
I'm trying to setup an NSTimer in a separate thread so that it continues to fire when users interact with the UI of my application. This seems to work, but Leaks reports a number of issues - and I believe I've narrowed it down to my timer code.
Currently what's happening is that updateTimer tries to access an NSArrayController (timersController) which is bound to an NSTableView in my applications interface. From there, I grab the first selected row and alter its timeSpent column. Note: the contents of timersController is a collection of managed objects generated via Core Data.
From reading around, I believe what I should be trying to do is execute the updateTimer function on the main thread, rather than in my timers secondary thread.
I'm posting here in the hopes that someone with more experience can tell me if that's the only thing I'm doing wrong. Having read Apple's documentation on Threading, I've found it an overwhelmingly large subject area.
NSThread *timerThread = [[[NSThread alloc] initWithTarget:self selector:#selector(startTimerThread) object:nil] autorelease];
[timerThread start];
-(void)startTimerThread
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
activeTimer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateTimer:) userInfo:nil repeats:YES] retain];
[runLoop run];
[pool release];
}
-(void)updateTimer:(NSTimer *)timer
{
NSArray *selectedTimers = [timersController selectedObjects];
id selectedTimer = [selectedTimers objectAtIndex:0];
NSNumber *currentTimeSpent = [selectedTimer timeSpent];
[selectedTimer setValue:[NSNumber numberWithInt:[currentTimeSpent intValue]+1] forKey:#"timeSpent"];
}
-(void)stopTimer
{
[activeTimer invalidate];
[activeTimer release];
}
UPDATE
I'm still totally lost with regards to this leak. I know I'm obviously doing something wrong, but I've stripped my application down to its bare bones and still can't seem to find it. For simplicities sake, I've uploaded my applications controller code to: a small pastebin. Note that I've now removed the timer thread code and instead opted to run the timer in a separate runloop (as suggested here).
If I set the Leaks Call Tree to hide both Missing Symbols and System Libraries, I'm shown the following output:
EDIT: Links to screenshots broken and therefor removed.
If the only reason you are spawning a new thread is to allow your timer to run while the user is interacting with the UI you can just add it in different runloop modes:
NSTimer *uiTimer = [NSTimer timerWithTimeInterval:(1.0 / 5.0) target:self selector:#selector(uiTimerFired:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:uiTimer forMode:NSRunLoopCommonModes];
As an addendum to this answer it is now possible to schedule timers using Grand Central Dispatch and blocks:
// Update the UI 5 times per second on the main queue
// Keep a strong reference to _timer in ARC
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
dispatch_source_set_timer(_timer, DISPATCH_TIME_NOW, (1.0 / 5.0) * NSEC_PER_SEC, 0.25 * NSEC_PER_SEC);
dispatch_source_set_event_handler(_timer, ^{
// Perform a periodic action
});
// Start the timer
dispatch_resume(_timer);
Later when the timer is no longer needed:
dispatch_source_cancel(_timer);

Easiest way to format a number with thousand separators to an NSString according to the Locale

I can't seem to find an easy way to do it. The exact thing I need is:
[NSString stringWithFormat:#"%d doodads", n];
Where n is an int. So for 1234 I'd want this string (under my locale):
#"1,234 doodads"
Thanks.
For 10.6 this works:
NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior: NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
NSString *numberString = [numberFormatter stringFromNumber: [NSNumber numberWithInteger: i]];
And it properly handles localization.
I have recently discovered this one-liner:
[#1234567 descriptionWithLocale:[NSLocale currentLocale]]; // 1,234,567
Or in Swift 2:
1234567.descriptionWithLocale(NSLocale.currentLocale()) // 1,234,567
Swift 3/4:
(1234567 as NSNumber).description(withLocale: Locale.current)
Formatted per the question:
[#(n) descriptionWithLocale:[NSLocale currentLocale]];
Formatted without Objective-C literals:
[[NSNumber numberWithInt:n] descriptionWithLocale:[NSLocale currentLocale]];
This is the solution I was looking for when I asked the question. Available since iOS 2.0 and OS X 10.0, documented to return a string version of the number formatted as per the locale provided. stringValue is even documented to use this method but passing nil.
Seeing as it is my question and this fits my answer best, I am tempted to change the tick, but it seems cruel. Update I changed the tick, this answer is the answer.
The below doesn't address the locale, but it is a better way (in my opinion) of setting the thousand separator on the number formatter.
NSNumberFormatter *numberFormat = [[[NSNumberFormatter alloc] init] autorelease];
numberFormat.usesGroupingSeparator = YES;
numberFormat.groupingSeparator = #",";
numberFormat.groupingSize = 3;
Todd Ransom answered this perfectly.
I would just like to add (in a separate comment, so I can show some nicely formatted code), that if you plan to do this regularly, it's worth creating an NSString helper class.
So, create yourself an NSStringHelper.h containing this:
#import <Foundation/Foundation.h>
#interface NSString (NSStringHelper)
+(NSString*)formatWithThousandSeparator:(NSInteger)number;
#end
..and an NSStringHelper.m file containing this:
#import "NSStringHelper.h"
#implementation NSString (NSStringHelper)
+(NSString*)formatWithThousandSeparator:(NSInteger)number
{
// Format a number with thousand seperators, eg: "12,345"
NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior: NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
NSString *result = [numberFormatter stringFromNumber:[NSNumber numberWithInteger:number]];
return result;
}
#end
This gives you the perfect way to reuse this code in future projects.
#import "NSStringHelper.h"
NSInteger numOfUsers = 12345;
NSString* strNumberOfUsers = [NSString formatWithThousandSeparator:numOfUsers];
Cool, hey ?
Again, apologies for reposting Todd's answer (which was exactly what I was looking for !), but this is a great way to solve the problem, and have it ready to be used in your future XCode projects.
Use an NSNumberFormatter.

Resources