Consider the case below
When we are using a C API's inside a class to create some data that are allocated in heap using malloc (e.g Object* create_obj()
), and we have to call a certain method (void free_obj()
) in the end of the class lifetime to free the memory manually.
When a language has a destructor, we can easily put the free_obj
in the class destructor so the user does not have to call the free_obj
manually and wait until the class get garbage collected.
My question
Why some garbage collected & OOP programming language (Java [Java has been deprecating it's
finalize
] and Ruby) doesn't have a destructor?Isn't a destructor is necessary when you're interfacing a low level API like for the case above? If it's not necessary, what is the best practice to solve the problem below?