Re: Cocoa Newbie Thread/Memory Problems
Re: Cocoa Newbie Thread/Memory Problems
- Subject: Re: Cocoa Newbie Thread/Memory Problems
- From: Dave <email@hidden>
- Date: Tue, 16 Mar 2010 21:38:04 +0000
Hi Quincey,
I added a call is NSLog() after each of the string assignment and it
seems to work! At least I get sensible values for the fields.
However, there still seems to be a problem. I need to make several
URL calls when the button is clicked, so I changed the top-level
button handler as so:
- (NSString*)makeURLString:(NString*) theBaseURL ForDate:(NSDate*)
theDate UsingDatabse:(NString*) theDatabaseType
{
NSString* myURLString = nil;
NSString* myMonthString = nil;
NSString* myDayString = nil;
NSDateFormatter myDateFormatter = nil;
myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:@"MMM"];
myMonthString = [myDateFormatter stringFromDate: theDate];
[myDateFormatter setDateFormat:@"dd"];
myDayString = [myDateFormatter stringFromDate: theDate];
myURLString = [NSString stringWithFormat:@"%@month=%@&day=%@&dbase=%
@", theBaseURL, myMonthString, myDayString, theDatabaseType];
return myURLString;
}
- (IBAction)getData:(id) sender
{
Class myParserClass = nil;
NSDate* myCurrentDate;
NSString* myURLString;
int myIndex
myParserClass = [ParserXML class];
myCurrentDate = [NSDate date];
for (myIndex=0;myIndex++;myIndex < 8)
{
self.mParserBase = [[[myParserClass alloc] init] autorelease];
mParserBase.mParserDelegate = self;
myURLString = [self myURLString:kBaseURL ForDate:myCurrentDate
UsingDatabase:@"rs_main"];
[mParserBase myURLString ForStructure:kField_List];
myCurrentDate = [myCurrentDate addTimeInteval:(24 * 60 * 60)];
}
}
Now when I run it, I get a crash on the second iteration of the loop.
I'm guessing because I am starting another thread before the first
one has finished. In general, what is the best way to handle this?
What would happen if I were only doing one iteration, but the User
Clicked the button before the first event had finished?
Thanks for a lot for any advice.
All the Best
Dave
On 16 Mar 2010, at 17:57, Quincey Morris wrote:
On Mar 16, 2010, at 09:57, Dave wrote:
At present I am not doing anything with the result, except
assigning the values so I can look at them in the debugger. The
problem is that when I look at the items in the array passed to
the view controller the data retrieved is not as it should be
(contains "invalid" or "out of scope" in the debugger). Sometimes
the values are correct, but most of the time they are garbage.
The method called:
- (void)parser:(ParserBase*)theParser didParseItems:(NSArray*)
theItemDictionaryArray
{
NSEnumerator* myArraytEnumerator = [theItemDictionaryArray
objectEnumerator];
NSDictionary* myDictionary;
NSString* myObjectNameString;
NSString* myFactYearString;
NSString* mtFactMonthString;
NSString* myFactDayString;
NSString* myFactSourceDatabaseString;
NSString* myFactTextString;
static int myObjectCount=0;
while (myDictionary = [myArraytEnumerator nextObject])
{
myObjectNameString = [myDictionary objectForKey:[ParserXML
parserObjectFieldName]];
myFactYearString = [myDictionary objectForKey:kField_FactYear];
mtFactMonthString = [myDictionary objectForKey:kField_FactMonth];
myFactDayString = [myDictionary objectForKey:kField_FactDay];
myFactSourceDatabaseString = [myDictionary
objectForKey:kField_FactSourceDatabase];
myFactTextString = [myDictionary objectForKey:kField_FactText];
myObjectCount++;
}
}
myObjectNameString, myFactYearString, mtFactMonthString,
myFactDayString, myFactSourceDatabaseString, myFactTextString are
gargage most of the time.
You may just be falling afoul of code optimization. Almost all of
your variables are unreferenced after being set, and the compiler
is very good at shortening their "effective" lifetime (in terms of
source code scope). Your while loop might actually be compiled as
if it was written:
while (myDictionary = [myArraytEnumerator nextObject])
{
[myDictionary objectForKey:[ParserXML parserObjectFieldName]];
[myDictionary objectForKey:kField_FactYear];
[myDictionary objectForKey:kField_FactMonth];
[myDictionary objectForKey:kField_FactDay];
[myDictionary objectForKey:kField_FactSourceDatabase];
[myDictionary objectForKey:kField_FactText];
myObjectCount++;
}
which would produce the debug-time problems you describe.
Make sure you're working with the Debug configuration of your app,
not the Release configuration, and that you haven't accidentally
turned on compiler optimizations in the Debug configuration. If
that doesn't get you anywhere, you may have to add code to
reference the variables, in order to keep their symbol information
"alive" long enough to examine them in the debugger. Another
approach is to NSLog the variables, which will keep them alive as
well as display them in the run log window.
_______________________________________________
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:
40looktowindward.com
This email sent to email@hidden
_______________________________________________
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