Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't the Objective-c runtime set dealloc'd object instances to nil?

Sometimes I set objects to nil after releasing them to avoid crashes due to unexpected messages sent to dealloc'd objects.

Why doesn't the Objective-c runtime do this automatically when an object is finally dealloc'd?

like image 281
SundayMonday Avatar asked Dec 04 '22 20:12

SundayMonday


1 Answers

Because pointers are passed by value and not reference. All dealloc gets is a local copy of the pointer; any changes it makes are discarded. Same goes for C's free and C++'s delete.

like image 137
Mike DeSimone Avatar answered Feb 23 '23 01:02

Mike DeSimone