NSString value mangled in NSDictionary with Garbage Collection
NSString value mangled in NSDictionary with Garbage Collection
- Subject: NSString value mangled in NSDictionary with Garbage Collection
- From: "Jason Kravitz" <email@hidden>
- Date: Thu, 10 Apr 2008 12:44:07 +0200
I created a while loop where I am reading through a text file and pulling
out certain words based on RegEx criteria. I want to add these words as a
comma separated list under different keys in an NSMutableDictionary. I've
created a simplified psuedo-code representation of what I'm trying to do as
it may make it clearer and potentially expose what I'm doing wrong (see
below).
I am running with Garbage Collection set to "Required" (I also tested
Supported with the same behavior and turning it off crashes my program).
I loop through the text and pass each sentence to a function which pulls out
different RegEx matched words. I used StringWithFormat in this function to
build the comma separated list of words (not sure if that affects why it is
getting freed early as I thought StringWithFormat would ensure an
autoreleased string?) which are returned to the function that loops through
the text file. This function adds the return string to the dictionary using
setObjectForKey.
Occasionally, my strings get mangled several iterations through the loop so
when I call objectForKey, I get strings like "Ø«˝˛_˛˛@" instead of the
comma separated list. If I watch it in the debugger, I can see where one
iteration, the string is correct and then the next loop through, it is
mangled.
This does not happen every time, I can go through for several passes of the
text file without any words getting mangled and then randomly it happens
again.
Here is a simplified illustration of what I'm doing - if there is a better
way to organize this (perhaps store value as array and not string?) I am
open to trying anything or if I'm doing something blatantly wrong that is
messing up memory, I'd like to learn how to do it correctly.
thanks!
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
while ([scanner isAtEnd] == NO)
{
NSString *sentence = @"a big long sentence of words from scanner... ";
NSString *words = [obj functionThatReturnsString:sentence];
[dict setObject:words forKey:@"some key"];
// in real program, scanner keeps looping and sentence is changing...
}
// function that returns string
-(NSString *)functionThatReturnsString:(NSString *)text
{
NSString *returnstring = @"";
// pick some words out of text
// lets say we loop and get regEx matches and each match we add to
return string
while (loop_matching_pattern) {
// found pattern and put in string "matched", add to return string
returnstring = [NSString stringWithFormat:@
"%@,%@",returnstring,matched];
}
return returnstring;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden