The weirdest Core Data crash I have seen
Careful how you name your attributes...
Published: Sept. 13, 2024 Sponsored App StoreThis week, I spent many hours trying to diagnose and fix weird and random Core Data crashes in one client project. The app was working fine, but when we built it recently with Xcode 15, it started crashing in various places.
When trying to fix it, I kept running into EXC_BAD_ACCESS
, which is a crash I dislike so much because when you get this, it usually means a lot of digging.
In my case, Core Data was crashing when setting simple values on an entity. And at first glance, there wasn’t anything wrong with that. The code was using the correct managed object context, and I couldn’t understand why this simple thing crashed in 90 % of cases.
Since the problems apparently started appearing with Xcode 15 (and also Xcode 16), I tried to see what was new with Xcode 15 in relation to Core Data. Apparently, quite a bit, but I couldn’t find references to EXC_BAD_ACCESS
just randomly popping up.
Finally, I discovered one Stack Overflow question with a single answer that wasn’t marked as a solution, which basically said, that you cannot have Core Data attribute that starts with “new”. This seemed weird, but I had nothing else to try, so I checked, and sure enough, the entity where it was crashing had one attribute that started with “new”.
So I renamed it to something else, and since then, I couldn’t reproduce a single Core Data crash, which previously took just a few taps in the app.
Maybe if the crash happened when I tried setting this attribute, I would find it faster, but it was crashing on an unrelated attribute modification in an ObjC code that was in the app for almost ten years.
The Stack Overflow answer also links archived Apple documentation title “Transitioning to ARC Release Notes” which states:
To allow interoperation with manual retain-release code, ARC imposes a constraint on method naming:
You cannot give an accessor a name that begins with new. This in turn means that you can’t, for example, declare a property whose name begins with new unless you specify a different getter:
Apparently, this is still relevant in 2024 for Core Data.
Anyway, I wrote that if anyone is in this situation, my blog post might surface and save a few hours.