2

I made an application that should run constant until I stop it. What it basically does is getting some data from connected another device and send that data to the server periodically using NSURLConnection, and read data from the server periodically and visualize that data as a graph using NSXMLParser.

I ran the instrument to check allocs and leaks. No leak at all. memory monitor shows consistent 5.2 MB. Objectalloc graph is stable, Net bytes of objectallo is arouend 480000 and #net is around 6400.

It crashed about 10 ~ 15 hours later. So I added breakpoint in malloc_error_break. Now I gets "EXC_BAD_ACCESS" error on Debugger console about 12 hours later.

Any idea?

One suspicious part is SENDING data.


- (void) sendDataToServerWithX:(float)x Y:(float)y{ 
NSAutoreleasePool *uiUpdatePool = [[NSAutoreleasePool alloc] init]; 
NSString *urlString = [[NSString alloc] initWithFormat:@"http://www.url.com/save_data.php?user=user1&x=%f&y=%f", x, y];
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) { NSLog(@"sending success"); } //else { }

NSLog( @"data sent.");
[urlString release];
[theConnection release]; [uiUpdatePool drain]; }

Another suspicious part is READING data:


- (void) readCurrentDataFromServer: (NSTimer *) timer {
NSAutoreleasePool *uiUpdatePool = [[NSAutoreleasePool alloc] init]; XMLParser *parser = [[XMLParser alloc] initXMLParser];
NSURL *url = [[NSURL alloc] initWithString:aString]; NSXMLParser *readXmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[readXmlParser setDelegate:parser]; [readXmlParser parse];

(...)

[parser release];
[url release];
[readXmlParser release]; [uiUpdatePool drain]; }

3 Answers 3

9

Try running it in the iPhone Simulator with "Guard Malloc" (in Xcode's Run menu) enabled. That's a special setting designed to make hidden memory-access bugs cause immediate crashes rather than hiding for a while. (It will also make your app very slow and consume much more memory than normal, which is why it isn't always on.) It's not certain that it will help, but it might.

UPDATE: The instructions above are for Xcode 3. In Xcode 4, turn on Guard Malloc by clicking your scheme name in the toolbar, selecting "Edit Scheme", clicking "Run" in the sheet's source list, choosing the "Diagnostics" tab, and checking "Enable Guard Malloc".

2
  • you saved my butt. Guard Malloc threw the error without holding it for hours so that i was able to find where the problem caused. Thanks@
    – sky
    Commented Feb 27, 2009 at 17:02
  • Yay, being able to reproduce 100% times an error that happened once a week is a good thing!
    – redent84
    Commented Oct 26, 2011 at 11:00
1

If you get EXC_BAD_ACCESS then you presumably also get a crash log, which will help you work out where the crash is. Probably dereferencing a pointer to memory which is not in play - for instance it's unclear where your aString or url variables come from. It's also not clear whether your app has multiple threads, in which case your local NSAutoreleasePool might be releasing things you'd rather it didn't. All very hard to tell.

4
  • Yes there is a crash log. can you see it? I have no idea... wonderment.org/crashlog.txt
    – sky
    Commented Feb 26, 2009 at 0:19
  • BTW why the hell does it crash the amount of time later?
    – sky
    Commented Feb 26, 2009 at 0:21
  • It looks from your crash log like the NSTimer you've got has been set to nil somewhere. Make sure you're not releasing any timers while they're in use - particularly check that you're only using the timer on one thread/run loop.
    – user23743
    Commented Feb 26, 2009 at 8:06
  • Ah, should have woken up first. Not the timer itself, but the object which will receive the message when the timer fires.
    – user23743
    Commented Feb 26, 2009 at 8:16
1

Profiling it with the Zombies instrument can also help, as it provides detailed feedback on what it is that is throwing up the error. For example a deallocated array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.