PDA

View Full Version : memory problems with NSScanner & NSString


Simon Schoelly
2004.09.24, 07:25 AM
I have a NSScanner

NSScanner *scanner = [NSScanner scannerWithString:fileContents];

but when I want to release it width [scanner release] crashes. And the same thing happens with [scanner dealloc].

Now I have a NSString

NSString *stringBuffer;
[scanner scanString:@"EDGES" intoString:&stringBuffer];

This object also can't be released nor dealloced. Why? And is there a method to get the retain count of an object at runtime?

Steven
2004.09.24, 08:12 AM
Keep in mind that if you use the classWithSomething: variant of messages (like scannerWithString, or stringWithContentsOfFile, etc) it will be returned autoreleased and you don't have to worry about releasing it unless you retain it again. If you use alloc and then initWithString or something of that sort then you do have to release it when you're done. That's probably your problem there- releasing that scanner would cause a double dealloc. Bad stuff :p

Simon Schoelly
2004.09.24, 08:34 AM
…and when the Scanner is released it sends a release message to stringBuffer. Is that correct?

DoG
2004.09.24, 05:07 PM
…and when the Scanner is released it sends a release message to stringBuffer. Is that correct?
i guess you should initialize to stringBuffer = nil at least. If it does not release the passed instance (check docs on it!) then it will return an autoreleased object.

Steven
2004.09.24, 08:48 PM
If you're passing in a pointer to a NSString *, I would assume that it would create an autoreleased object. You can't pass in one, because it's not mutable. So it would create its own. But check the docs.