Dear Cocoa List,
I'm occasionally seeing a crash when a XIB is loading some of the bindings I've
set up. Using a NSViewController subclass, a view is loaded in response to
clicking on a NSToolbar item. One of the views displays some disk space usage
information. This works fine almost all of the time. In the NSViewController
subclass, the following code exists:
@dynamic freeSpace;
- (NSString *)freeSpace {
NSDictionary *systemInfo = [[NSFileManager defaultManager]
attributesOfFileSystemForPath: [[NSBundle mainBundle] bundlePath] error: nil];
return [self humanReadableNumber: [NSDecimalNumber
decimalNumberWithString: [[systemInfo objectForKey: NSFileSystemFreeSize]
stringValue]]];
}
An NSTextField is bound to the "freeSpace" attribute of this class.
Occasionally after recompiling, this will always cause a crash when the view is
loaded. Commenting out the code, recompiling and then restoring the code
clears the crash problem up every time. This suggests to me a race condition
somewhere that is causing the crash.
For the sake of completeness, although I'm pretty sure this isn't the problem,
the function called does this:
- (NSString *)humanReadableNumber: (NSNumber *)size {
NSArray *suffixes = [NSArray arrayWithObjects: NSLocalizedString(@"B",
@"BYTES"), NSLocalizedString(@"kB", @"KILOBYTES"), NSLocalizedString(@"MB",
@"MEGABYTES"), NSLocalizedString(@"GB", @"GIGABYTES"), NSLocalizedString(@"TB",
@"TERABYTES"), nil];
NSDecimalNumberHandler *handler = [NSDecimalNumberHandler
decimalNumberHandlerWithRoundingMode: NSRoundPlain scale: 1 raiseOnExactness:
NO raiseOnOverflow: NO raiseOnUnderflow: NO raiseOnDivideByZero: NO];
NSDecimalNumber *byteRate = [NSDecimalNumber decimalNumberWithString:
@"1000"];
NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:
[size stringValue]];
NSDecimalNumber *temp;
NSInteger i;
for (i = 0; i < ([suffixes count] - 1); i++) {
temp = [number decimalNumberByDividingBy: byteRate
withBehavior: handler];
if ([temp doubleValue] < 1)
break;
number = temp;
}
return [NSString stringWithFormat: @"%@ %@", [number stringValue],
[suffixes objectAtIndex: i]];
}
How can I protect against this problem?
Is there some part of what I'm doing here that is bad practice that might be
causing the crash?
Thanks for any insights in solving this.
Arved
_______________________________________________
Cocoa-dev mailing list ([email protected])
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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]