Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSObject finalize is deprecated - what do I do instead?

I've been working on some XML utilities in Objective-C, including my own IO stream objects built around FILE * values. In order to do this safely it is important that I call fclose on the file pointer when the stream object is deallocated (if I had not already done so).

My strategy for this (and many other deallocation tasks) has long been to override the finalize method of NSObject. However I just read (and confirmed by testing) that finalize no longer gets called at all during deallocation.

Garbage collection is deprecated in OS X v10.8; instead, you should use Automatic Reference Counting—see Transitioning to ARC Release Notes.

So apparently I've now been through two versions of OS X without realizing that I was hemorrhaging system resources the whole time. I read through those release notes and I could not find any reference to this particular problem. I was unable to find any alternative method for handling deallocation.

I write Objective-C wrappers for C/C++ data types a lot. I need to some way to release unmanaged memory when the wrapper is being deallocated.

Is there any ARC alternative to finalize? And if not what am I supposed to do about malloc'd C/C++ types in my NSObjects?

like image 974
William Rosenbloom Avatar asked Mar 22 '26 06:03

William Rosenbloom


1 Answers

From the NSObject class reference...

enter image description here

like image 147
William Rosenbloom Avatar answered Mar 25 '26 14:03

William Rosenbloom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!