views:

5010

answers:

5

I have a line of code that is cause a "EXC_BAD_ACCESS" error. The line of code is as follows (formatted into one line and nested code removed for ease of reading).

if (![sendData isEqualToString:@"-"]){ ... }

The actual error occurs on the IF line. the odd thing is that if I put a breakpoint on that line, the NSString called sendData (shown as NSCFString with a value of "-" without the quotes). Why would this be causing an error? Please advise.

+1  A: 

It sounds like you're trying to send a message to an object whose memory has been deallocated. Make sure you're following the proper memory management techniques discussed in the Memory Management Programming Guide for Cocoa. The fix would probably be to add a [sendData retain] at some point before it gets deallocated, but to understand why that works, you have to read the aforementioned guide.

Adam Rosenfield
Im not sure why that would be. sendData is a property of the AppDelegate and is not being deallocated (by me) at all.. Also, if the object were deallocated, would I still see its value in the debugger?
Dutchie432
Do you get or create the data in sendData? If so you may have to call retain.
rjstelling
I am creating the data like:sendData=@"-";
Dutchie432
+2  A: 

To catch this problem you'll have to put break points in all callback methods.

The problem is simple, the code is trying to access memory it cant find.

Finding that line of code is harder because the callbacks are not called sequentially.

  1. Add more break points
  2. Add more NSLog(..)
  3. Consider catching exceptions (see http://stackoverflow.com/questions/324284/how-to-throw-an-exception-in-objective-c-cocoa/324301)


Output form the console:

Attaching to program: `/Users/rjstelling/Library/Application Support/iPhone Simulator/User/Applications/C04A40BB-1D98-402E-BBEF-37E6FB860089/TwoViewApp.app/TwoViewApp', process 24032.
Re-enabling shared library breakpoint 1
2009-04-16 16:16:45.830 TwoViewApp[24032:20b] stream event 1
2009-04-16 16:16:45.831 TwoViewApp[24032:20b] on input stream
2009-04-16 16:16:45.831 TwoViewApp[24032:20b] stream event 1
2009-04-16 16:16:45.832 TwoViewApp[24032:20b] on output stream
2009-04-16 16:16:45.832 TwoViewApp[24032:20b] stream event 4
2009-04-16 16:16:45.832 TwoViewApp[24032:20b] on output stream
2009-04-16 16:16:45.833 TwoViewApp[24032:20b] stream has space open
(gdb) continue
2009-04-16 16:17:06.405 TwoViewApp[24032:20b] We made it - ok!
2009-04-16 16:17:06.406 TwoViewApp[24032:20b] stream event 2
2009-04-16 16:17:06.406 TwoViewApp[24032:20b] on input stream
2009-04-16 16:17:06.407 TwoViewApp[24032:20b] Processing: +OK CONN PinkNotes® Plus Master v5.00.26 Beta (v4 compatible)
:tPNPStr
2009-04-16 16:17:06.407 TwoViewApp[24032:20b] SendData= USER (null):tPNPStr
Current language:  auto; currently objective-c
Program received signal:  “EXC_BAD_ACCESS”.
(gdb)

The problem occurs some where in or after ProcessData which is a callback I think. Try and put a break point around line 157 in TwoViewAppAppDelegate.m


It's not that line that is causing the EXC_BAD_ACCESS if you add:

else
{
 NSLog(@"We made it - ok!");
}

To the if() statement you can see it passes over the if ( ![sendData isEqualToString:@"-"] ){...}

The error occurs when you return form the method call.


Ok form you comments this might help:

If you create strings using @"My string" the compiler will map these to he same memory if they have the same content, i.e.:

NSString *var1 = @"string1";
NSString *anotherstring = @"string1";
NSString *morestringivars = @"string1";

Will all point at the same memory space.

This may help, but I'm not sure how? Maybe you can post more code so I can run it on my set up.


Remember an auto release pool is created at the start of the event cycle on the iPhone.

Therefore it is a good idea to call autorelease on sendData as soon as you assign it to the ivar.

...

[sendData autorelease];

...
rjstelling
I would be happy to post the code for download if you'd like to take a look.
Dutchie432
Actually, the code does NOT skip over the IF statement when I include an ELSE...
Dutchie432
It definitely does on my system. Strange.
rjstelling
Its weird b/c I was just stepping through it (for the 100th time) and it DID step over the IF - then I ran it again and it didnt again. Weird.
Dutchie432
it's b/c the callback function is called at different times, depending on the network.
rjstelling
So what do you recommend? It seems depending on where I put my breakpoints, different lines cause the problem now.... should I not be using a property? Is there a way to otherwise declare a public NSString that can be access from all functions in a file?
Dutchie432
A: 

I realized when I was declaring the property sendData in my appDelegate header file, I was not using 'retain' - however I changed that and am still getting the error. The odd thing is that I am using other properties the same way as this one, and this one is the only one to yield the error.

Dutchie432
A: 

Set your object to nil after a release in order to prevent a crash when sending a message to a non available object.

thierryb
A: 

Man the problem is in this line

if([appVersion isNotEqualTo:currentVersion])

"isNotEqualTo" is supported at mac os x 10.X or later but not on iphone OS . so calling this causes an exception at device . now i hope you get the whole point instead change your comparison logic to if(![appVersion isEqualToString:currentVersion]) , i think it will work fine ..