Manually calling the superclass's dealloc in the overridden dealloc method causes a crash

I have a class object created dynamically using Runtime, and I want to release some manually allocated memory resources when this object is deallocated. To achieve this, I added a custom implementation of the dealloc method using the following code:

SEL aSel = NSSelectorFromString(@"dealloc");
class_addMethod(kvoClass, aSel, (IMP)custom_dealloc, method_getTypeEncoding(class_getInstanceMethod(kvoClass, aSel)));

However, I encountered some issues. If I don't call the superclass's dealloc method in the cus_dealloc function, the superclass's dealloc implementation will not be executed. On the other hand, if I explicitly call the superclass's dealloc method, the program crashes.

Here is the implementation of the cus_dealloc function:

void custom_dealloc(id self, SEL _cmd) {
    // Release other memory
![]("https://developer.apple.com/forums/content/attachment/c7b0c16b-be23-4776-b8db-f22b661c5e7d" "title=iShot_2025-01-03_19.31.34.png;width=1080;height=1895")

    
    Class superClass = class_getSuperclass(object_getClass(self));
    void (*originIMP)(struct objc_super *, SEL, ...) = (void *)objc_msgSendSuper;
    struct objc_super *objcSuper = &(struct objc_super){self, superClass};
    originIMP(objcSuper, _cmd);
}

demo

Answered by DTS Engineer in 820667022

I believe the issue here is that you’re using ARC. Specifically, in custom_dealloc the call to objc_msgSendSuper succeeds but then, after that, the code calls objc_storeStrong to nil out the reference it has to the object. However, the object has been deallocated and thus you die in objc_release.

If you’re going to play the Silly Runtime Hacks™ game, you have to use MRR.


Taking a step back, what are you actually trying to do here?

It looks like you’re trying to reimplement KVO, and I strongly recommend against going down that path. KVO is super hard to implement correctly, to the point that I’m pretty sure that Apple’s implementation still has bugs in some corner cases.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

I believe the issue here is that you’re using ARC. Specifically, in custom_dealloc the call to objc_msgSendSuper succeeds but then, after that, the code calls objc_storeStrong to nil out the reference it has to the object. However, the object has been deallocated and thus you die in objc_release.

If you’re going to play the Silly Runtime Hacks™ game, you have to use MRR.


Taking a step back, what are you actually trying to do here?

It looks like you’re trying to reimplement KVO, and I strongly recommend against going down that path. KVO is super hard to implement correctly, to the point that I’m pretty sure that Apple’s implementation still has bugs in some corner cases.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Attached is a screenshot of the code:

I’m not sure what the purpose of this screenshot is. It seems to just show the problem in action; it doesn’t seem to be related to your attempt to fix the problem, per the suggestion in my previous post.

Also, if you want to continue this thread, I really need an answer to this:

Written by DTS Engineer in 820667022
Taking a step back, what are you actually trying to do here?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Manually calling the superclass's dealloc in the overridden dealloc method causes a crash
 
 
Q