Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > What is wrong with my code?

What is wrong with my code?
Thread Tools
Junior Member
Join Date: Oct 2000
Location: Germany
Status: Offline
Reply With Quote
Aug 9, 2004, 11:21 AM
 
Hello,

I've just started to write a little cocoa app that should parse an ICAO string, once I've finished it.
But when I call the method a second time, my app crashes with a SIGTRAP 10 (whatever that is).
Here is how I call the method, that is located in a class I called Wrapper:

Wrapper *wrapOne = [[Wrapper alloc] init];
[wrapOne parseStringWithICAO:@"EDDK"];
[wrapOne parseStringWithICAO:@"CYTZ"]; // HERE IS THE CRASH

Below is the code for the method. Can anyone point me out to what it is and why it is causing the crash? I think that it has to do something with memory management, because I really don't know when to retain and release. Thanks in advance!
Sincerely
rudy



- (void)parseStringWithICAO:(NSString *)icaoCode {
//[icaoCode retain];

clouds = 0; // we need to do this in order to find out the strongest clouds later
icaoURL =[[NSMutableString alloc] init];
[icaoURL setString:@"http://205.156.51.200/pub/data/observations/metar/stations/"];
[icaoURL appendString:icaoCode]; [icaoURL appendString:@".TXT"];
//[icaoCode release]; icaoCode = nil;

NSURL *weatherURL = [NSURL URLWithString:icaoURL];
NSString *icaoString = [NSString stringWithContentsOfURL:weatherURL];

NSCharacterSet *spaceSet = [NSCharacterSet characterSetWithCharactersInString:@" "];
NSScanner *spaceScanner = [NSScanner scannerWithString:icaoString];
NSString *aString;

while (![spaceScanner isAtEnd]) {
if([spaceScanner scanUpToCharactersFromSet:spaceSet intoString:&aString]) {
[self categorizeTag:aString]; // check what tag it is
}
[spaceScanner scanCharactersFromSet:spaceSet intoString:nil];
}

[icaoURL release];
}
     
Fresh-Faced Recruit
Join Date: Jul 2004
Status: Offline
Reply With Quote
Aug 11, 2004, 05:30 AM
 
Hello,

Please note that I have only been learning Cocoa for a month or so myself, so you will probably want to wait for the experts to answer, but as I have been playing with parsing strings (for a word count) over the past couple of days, I thought I would have a bash at answering - hope you don't mind. Unfortunately I'm on a PC at the mo, too, so can't test this.

I usually get a SIGTRAP 10 when I have tried to access an object that I have already released (I think), as you guessed.

You could try letting the autorelease pool deal with the mutable string you set up, just as a test, like so:

Code:
- (void)parseStringWithICAO:(NSString *)icaoCode { clouds = 0; icaoURL = [NSMutableString stringWithString: @"http://205.156.51.200/pub/data/observations/metar/stations/"]; [icaoURL appendString: icaoCode]; [icaoURL appendString: @".txt"]; // (NSString has stringByAppendingExtension, but no mutable equivalent...) NSURL *weatherURL = [NSURL URLWithString:icaoURL]; NSString *icaoString = [NSString stringWithContentsOfURL:weatherURL]; NSScanner *spaceScanner = [NSScanner scannerWithString: icaoString]; NSString *aString = nil; while([spaceScanner scanCharactersUpToCharactersFromSet: [NSCharacterSet whitespaceCharacterSet] intoString: &aString]) { [self categorizeTag: aString]; } }
I wouldn't have thought this would make much difference, though, because as far as I can see you are only releasing icaoURL after you have finished using it.

That said, why are you allocating, initialising and releasing icaoURL so often (ie. each time you call the method)? I take it icaoURL is an instance variable. Does it need to be an instance variable, or could you just make it local to this method? If for some reason it needs to be an instance variable (though I don't see why it does if you are releasing it straight after you've finished using it), you should probably move its allocation and initialisation to your designated initialiser method, and its deallocation into an override of dealloc. setString: replaces the current string each time you use it anyway, so you don't need to recreate this string every time you call the method. If you are trying to use icaoURL outside of this method, this could be the problem. ie:

Code:
-(id) init { if (self = [super init]) { icaoURL = [[NSMutableString alloc] init]; } return self; } - (void) parseStringWithICAO: (NSString *) icaoCode { clouds = 0; [icaoURL setString: @"http://205.156.51.200/pub/data/observations/metar/stations/"]; [icaoURL appendString: icaoCode]; [icaoURL appendString: @".txt"]; // (NSString has stringByAppendingExtension, but no mutable equivalent...) NSURL *weatherURL = [NSURL URLWithString:icaoURL]; NSString *icaoString = [NSString stringWithContentsOfURL:weatherURL]; NSScanner *spaceScanner = [NSScanner scannerWithString: icaoString]; NSString *aString = nil; while([spaceScanner scanCharactersUpToCharactersFromSet: [NSCharacterSet whitespacecharacterSet] intoString: &aString]) { [self categorizeTag: aString]; } } -(void) dealloc { [icaoURL release]; [super dealloc]; }
Otherwise, I am wondering if the problem lies in your categorizeTag: method. Are you setting or changing any instance variables in that method? If you are allocating or releasing an instance variable in that method, which gets called more than once, that could be the problem.

By the way, I don't know if there is a reason you are creating your own character set with a space in it, but I have been using whitespaceCharacterSet for this (actually I have been using whitespaceAndNewlineCharacterSet for my word counter).

I also noticed that you tried to retain and release icaoCode a couple of times before commenting out these attempts - icaoCode doesn't need any memory management because it is a constant string.

I am very sorry if this is absolutely no use to you at all - I hope you don't mind a newbie trying to help (it helps me to try to get my head around as much code as possible :) ). Hope the experts can help you if my wild stabs are completely off-mark.

Cheers,
KB
     
Junior Member
Join Date: Oct 2000
Location: Germany
Status: Offline
Reply With Quote
Aug 11, 2004, 10:21 AM
 
Hi KB,

thanks for your reply. You were right with the mutable string. I moved to the init-method now. Now my code almost works nicely. I can now call the method parseStringWithICAO: without a SIGTRAP 10.

So this works now:
wrapOne=[[Wrapper alloc] init];
[wrapOne parseStringWithICAO:@"EDDK"];
[wrapOne parseStringWithICAO:@"CYTZ"];

I then tried to implement an initWithICAO so that I would call it like this:
wrapOne = [[Wrapper alloc] initWithICAO:@"CYTZ"];
[wrapOne parseStringWithICAO:@"EDDK"];

In my Wrapper class I implemented the init stuff like this and now I'm getting crashes again (SIGTRAP 10):
- (void)initWithICAO:(NSString *)icaoCode {
//do i have to retain icaoCode here?
[self init];
[self parseStringWithICAO:icaoCode];
}

- (id)init {
if (self= [super init]) {
cloudsArray = [[NSArray alloc] initWithObjects:@"OVC",@"BKN",@"SCT",@"FEW"];
icaoURL = [[NSMutableString alloc] init];
}
return self;
}

So, thanks again for your help!
Kind regards
rudy
     
Fresh-Faced Recruit
Join Date: Jul 2004
Status: Offline
Reply With Quote
Aug 11, 2004, 01:13 PM
 
Glad I could be of assistance - it's normally me who needs the help. :) Anyway, for your new problem, I think the problem is that you are not returning anything from your new initialiser (and by the way, you don't need to retain icaoCode because you are using it as a constant, so you don't need to copy it). Try this for your initWith:

Code:
- (id) initWithICAO:(NSString *)icaoCode { //do i have to retain icaoCode here? - nope! [self init]; [self parseStringWithICAO:icaoCode]; return self; }
Hope that helps.
Cheers,
KB
     
Junior Member
Join Date: Oct 2000
Location: Germany
Status: Offline
Reply With Quote
Aug 11, 2004, 01:54 PM
 
Thanks very nuch! I think I got it now. How did you understand cocoa so fast? I'm trying to get it right for months now.

Thank you!

Sincerely
Rudy
     
Fresh-Faced Recruit
Join Date: Jul 2004
Status: Offline
Reply With Quote
Aug 11, 2004, 02:28 PM
 
Glad it worked! I did know some C already, but Stephen Kochan's Programming in Objective-C and Aaron Hillegass's Cocoa Programming For Mac OS X (which I'm about two-thirds of the way through) are entirely responsible for any knowledge I have gained in Cocoa.
All the best,
KB
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:08 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2