NSString EXC_BAD_ACCESS

I am stuck with the next bit of code.

NSString *gridRef = [[NSString alloc] initWithFormat: @"%@", [converter LatLongToOSGrid: latLong]]; NSLog(@"Grid Ref: %@", gridRef); self.answerLabel.text = [[NSString alloc] initWithFormat: @"%@", gridRef]; 

When I register gridRef, it displays the correct result. However, setting the answerLabel.text line causes an EXC_BAD_ACCESS error, and the program crashes. IB connected to the correct label, what is the problem?

thanks


I updated the code as follows:

  - (IBAction)convertLatLong { NSArray *latLong = [[NSArray alloc] initWithObjects: latTextField.text, longTextField.text, nil]; GridRefsConverter *converter = [[GridRefsConverter alloc] init]; NSString *gridRef = [[NSString alloc] initWithFormat: @"%@", [converter LatLongToOSGrid: latLong]]; NSLog(@"Grid Ref: %@", gridRef); NSLog(@"Label: %@", self.answerLabel.text); answerLabel.text = @"Yippy"; self.answerLabel.text = gridRef; [gridRef release]; [converter release]; [latLong release]; } 

answerLabel is initialized via @property @synthesize when the view controller is pushed onto the stack. (I don’t know how this happens, besides, this is one of the magical things that IB does for you. Or, I assume that I used the exact same method in other view controllers and did not have this problem.


I found the criminals - the question is, how can I free them?

 NSString *eString = [[NSString alloc] initWithFormat: @"%f", e]; NSString *nString = [[NSString alloc] initWithFormat: @"%f", n]; eString = [eString stringByPaddingToLength: (digits/2) withString: @"0" startingAtIndex: 0]; nString = [nString stringByPaddingToLength: (digits/2) withString: @"0" startingAtIndex: 0]; NSString *theGridRef = [letterPair stringByAppendingString: eString]; theGridRef = [theGridRef stringByAppendingString: nString]; [eString release]; [nString release]; return theGridRef; 

and

 NSArray *gridRef = [[NSArray alloc] init]; gridRef = [gridRef arrayByAddingObject: [NSNumber numberWithDouble: E]]; gridRef = [gridRef arrayByAddingObject: [NSNumber numberWithDouble: N]]; gridRef = [gridRef arrayByAddingObject: [NSNumber numberWithInteger: 8]]; NSString *theGridRef = [[NSString alloc] initWithFormat: @"%@", [self gridRefNumberToLetter: gridRef]]; [gridRef release]; [theGridRef autorelease]; return theGridRef; 

}

+4
source share
5 answers

You must enable zombie detection by setting the NSZombieEnabled environment NSZombieEnabled to YES so you can see which object is causing bad access (remember to delete it again when you find the error).

You can also use the Tools to find the place where the object is really freed. To do this, start a new session "Tools" and use the "Selection" tool. In the tool settings, check the box "Enable NSZombie detection" and "Record number of links". When you start a session, you break the place where the error occurs and you see a record of all the save / releases.

One place where you can quickly see if your object is improperly freed is in the -viewDidUnload method, where you must free the output and set it to zero . If you forget the latter and somehow connect to the outlet, this will result in EXC_BAD_ACCESS.

Edited according to your update:

The problem is that you are assigning eString (and nString) to a new line that was alloc / init-ed. Then you override them in the following operations, because -stringByPaddingToLength: (as well as all other methods -stringBy... ) return a new and auto-implemented string object. Thus, you lost the link to the old line, which means that there is a memory leak. In addition, at the end, you release already autoreclosed objects explicitly, which causes poor access.

Instead, you should create strings with auto-implementation from the very beginning ([NSString stringWithFormat: ...]) and not release them at the end.

+4
source

Check if asnwerLabel not null. You should also change this line:

 self.answerLabel.text = [[NSString alloc] initWithFormat: @"%@", gridRef]; 

To:

 self.answerLabel.text = [NSString stringWithFormat: @"%@", gridRef]; 

Otherwise, you will lose a memory leak on this line.

0
source

Perhaps the label is not indicated at this point in your code, try checking it. Why are you assigning a new NSString? Just do:

 self.label.text = gridRef; [gridRef release]; 
0
source

How is answerLabel created? You may need to save this. Or do you need to release some things (gridRef)?

I do not see other problems with your code.

You can (and probably should) install

 answerLabel.text = gridRef; 

gridRef is already an NSString, so you no longer need to allocate it.

EXC_BAD_ACCESS usually refers to the memory associated with your save / release account without balancing (or in my extensive experience: p).

0
source

Well, the problem was to release NSStrings, so I stopped doing it and the problem was resolved.

Can someone clarify how strings are saved and freed. I got the impression that:

string = @"My String"; auto implemented. NSString *string = [[NSString alloc] init...]; not auto-implemented and must be done manually.

0
source

All Articles