How to Determine the Cause of EXC_BAD_ACCESS in main.m

Asked 2 years ago, Updated 2 years ago, 44 views

During programming with Objective-C, we encountered a runtime error of EXC_BAD_ACCESS on return UIApplicationMain(argc, argv, nil, NSSstringFromClass([AppDelegate class]) in main.m.
Even if you set the breakpoint to all exception, it falls in the same place, and if you set the zombie object, it does not emit an error message, so you are in a state of confusion.
What are the other ways to find out the cause?
I cannot post the code itself, but I would appreciate it if you could let me know if you have similar experiences.

ios objective-c

2022-09-29 20:28

2 Answers

I think the following article will be useful

I think it's because it's surrounded by zombie.
It may be a problem specific to your code, so you have to follow the problem with your code.
Use a breakpoint.


2022-09-29 20:28

This error occurs when AutoreleasePool tries to free up more free space (reference counter is 0).
You can easily wake up with the following code:(ARC not used)

#import "ViewController.h"

@ interface ViewController()
{
    UILabel*lbl;
}

@end

@implementation ViewController

- (void) viewDidLoad
{
    superviewDidLoad;
    // Do any additional setup after loading the view, typically from anib.

    // Point 1
    lbl = [[[UILabel alloc] initWithFrame: CGRectMake(0,0,100,20)] autorelease];
    // Point 2
    lbl release;

}

- (void) DidReceiveMemoryWarning {
    super didReceiveMemoryWarning;
    // Dispose of any resources that can be recreated.
}

@end

Create lbl at Point 1 (reference counter +1) and register with AutoreleasePool.
By manually releasing the lbl at Point 2, the reference counter is -1 and the reference counter is zero, at which point the lbl memory is freed.
After exiting this block and returning to the main function, when the AutoreleasePool is released, the lbl registered with the AutoreleasePool attempts to -1 the reference counter further.
However, it will be EXC_BAD_ACCESS because it tries to access the already freed area of the lbl.

I think the point to review is the number of retain and release.
Even with ARC, the probability of this problem is not zero.
(Since I don't use ARC, I can't give you an example when I use ARC.)

Regarding how to find out the problem,
Wouldn't it be better to comment out where the problem occurs and release it gradually to check the operation?
If you cancel the comment and it starts to drop, the scope of the cancellation is suspicious.
As long as you don't post the code, this is the limit of your advice.


2022-09-29 20:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.