sky
sky

Reputation: 23

EXC_BAD_ACCESS error

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]; }

Upvotes: 2

Views: 8644

Answers (3)

tustvold
tustvold

Reputation: 148

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.

Upvotes: 1

Becca Royal-Gordon
Becca Royal-Gordon

Reputation: 17861

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".

Upvotes: 9

user23743
user23743

Reputation:

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.

Upvotes: 1

Related Questions