Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why XCode Clean and Build fix EXC_BAD_ACCESS?

I've seen lots of people (including me) facing the crash with EXC_BAD_ACCESS with unknown reason. A lot of answers with the solution clean and build the project marked as correct.

I'm curious why this happens and how clean and re-build fixed it, but it seems people just don't care and go on with clean-build solution.

Here's an example of those EXC_BAD_ACCESS with clean-build solution.

In my case, I'm facing something as ridiculous as this:

func viewDidLoad() {
    super.viewDidLoad()
    self.childVC.delegate = self // => Crashed EXC_BAD_ACCESS here. Fixed after clean and build
}

As far as I know, this happened in Swift a lot more usually than Objective-C. Could it be Swift's features?

like image 299
Eddie Avatar asked Oct 17 '22 20:10

Eddie


1 Answers

These are my thoughts:

Once run, if project is not already built then Xcode generates the required build files in:

~/Library/Developer/Xcode/DerivedData/

If the project is not cleaned, either manually or automatically, then the same build files are reused in running apps to reduce consequent build time.

You may have already noticed that Xcode sometimes automatically cleans the build after any major code change. For example, on:

  • changing static variables
  • changing objective-c code in the swift project

But Xcode still has it's flaws, and sometimes does not clean itself when needed making it possible to run with the, let's say, "broken" build files that eventually give rise to such EXC_BAD_ACCESS errors.

Cleaning it will clear the old build files and new builds don't have the same problem.

like image 108
Bio-Matic Avatar answered Nov 15 '22 12:11

Bio-Matic