问题描述:

I am new in iPhone developement and I have a EXC_BAD_ACCESS error.

In my .h file I declare:

NSString *myString;

In my .m file, I have two methods:

1.The (void)locationManager of CLLocationManager interface when I do it:

myString = [NSString stringWithFormat:@"%lf",loc.longitude];

NSLog(@"%@",myString); // it works

2.A (void)sendPosition method with:

NSLog(@"%@",myString); // EXC_BAD_ACCESS

Can you help me?

网友答案:
  1. Retain your string.

    myString = [[NSString stringWithFormat:@"%lf",loc.longitude] retain];

  2. Then in your dealloc method of this class (or add it)

    -(void) dealloc{ [super dealloc]; [myString release]; }

that should fix it, assuming you have declared myString in you .h file.

网友答案:

Are you retaining myString somewhere else in your locationManager method? If not, you should, then release it in your -dealloc method. Otherwise, it could disappear like I suspect is what's happening now. Please read up on the rules for memory management.

What I would recommend is creating a property which retains the value automatically on your class, then assign it with the dot notation when you create the string. Again, releasing it in your dealloc method. This is idiomatic these days for this type of code.

相关阅读:
Top