0

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dutchie432
  • 28,798
  • 20
  • 92
  • 109
  • 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 Apr 16 '09 at 14:51

3 Answers3

2

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 throwing an exception in objective-c/cocoa)

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

...
Community
  • 1
  • 1
Richard Stelling
  • 25,607
  • 27
  • 108
  • 188
  • I would be happy to post the code for download if you'd like to take a look. – Dutchie432 Apr 16 '09 at 14:30
  • It definitely does on my system. Strange. – Richard Stelling Apr 16 '09 at 15:11
  • 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 Apr 16 '09 at 15:18
  • it's b/c the callback function is called at different times, depending on the network. – Richard Stelling Apr 16 '09 at 15:31
  • 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 Apr 16 '09 at 16:13
1

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
  • 390,455
  • 97
  • 512
  • 589
0

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

thierryb
  • 3,660
  • 4
  • 42
  • 58