Re: newbie EXC_BAD_ACCESS
Re: newbie EXC_BAD_ACCESS
- Subject: Re: newbie EXC_BAD_ACCESS
- From: Hamish Allan <email@hidden>
- Date: Mon, 21 Mar 2005 02:45:57 +0000
[scanner scanUpToCharactersFromSet: chSetTab intoString:
&scannedName];
This line is going to cause a problem. You have declared scannedName,
but it hasn't been allocated or initialized, so it's pointing nowhere.
That will cause a crash.
No it won't! From the documentation:
- (BOOL)scanCharactersFromSet:(NSCharacterSet *)scanSet
intoString:(NSString **)stringValue
Scans the string as long as characters from scanSet are encountered,
accumulating characters into a string that’s returned by reference in
stringValue. Returns YES if the receiver scans any characters;
otherwise returns NO. Invoke this method with nil as stringValue to
simply scan past a given set of characters.
In other words, this method returns two things: a BOOL and an NSString
*. It returns the latter by reference, overwriting the contents of the
NSString *scannedName with the address of the newly-created string.
But of course, Daniel, that string will have been autoreleased, so you
have to retain it otherwise it'll get deallocated after the method
returns.
Unfortunately that is the least of your worries. Using a fixed-size of
44 for your array of lines is not a good idea, nor is it necessary.
Your code should read something like:
- (void)fillStrokes
{
NSScanner *scanner = [NSScanner scannerWithString:
[NSString stringWithContentsOfFile:
@"~/Documents/PROGRAMMING/IME DEV/ScratchPad/strokeList.txt"
stringByExpandingTildeInPath]];
int type;
NSString *desc;
while (![scanner isAtEnd])
if ([scanner scanInt:&type] && [scanner scanUpToString:@"\n"
intoString:&desc])
[self addStrokeDesc:[StrokeDescription
strokeDescriptionWithType:type desc:desc]];
}
with the following method in StrokeDescription:
+ (id)strokeDescriptionWithType:(int)type desc:(NSString *)desc
{
id sd = [[StrokeDescription alloc] init];
[sd setStrokeType:[NSNumber numberWithInt:type]]; // is this wrapping
necessary?
[sd setDesc:desc];
return [sd autorelease];
}
Assuming you know the contents of the file strokeList.txt (in order to
assume it has 44 lines) why not use a more suitable container, like an
XML property list (which is basically a file-based version of an
NSDictionary)?
Best wishes,
Hamish
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden