2008 12 09Don't overload release or retainCount in JSCocoa
release
or retainCount
in JSCocoa will crash your app. To handle ObjC objects, JSCocoa boxes them as a Javascript object's private data. When the Javascript object is destroyed (finalized in JavascriptCore parlance) , JSCocoa will destroy its private data, thereby releasing the boxed ObjC object.
Finalization happens during garbage collection and JavascriptCore has an assert in place to prevent object creation during that time. Therefore creating Javascript objects during collection will crash. This could be fixed by delaying the destroying to the next run loop cycle, but for now : just don't do it :)
Now retainCount
is not called during release
(surely the ObjCRuntime is getting the ivar) so it should be safe to overload. Right ?
Crashing while running in Instruments
I overloaded retainCount
in JSCocoa tests. While fixing memory leaks and running in Instruments, I encountered a crash in … retainCount
. Turns out Instruments does calls retainCount
during alloc
and init
. BUT it does not crash just yet, I have to repeatedly run the tests. And different runs yield different results :
- Running from Instruments' Leaks template :
retainCount
called twice → CRASH - Running from Instruments' ObjectAlloc template then adding Leaks :
retainCount
called once → no crash