Exceptions vs destructors

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
embryo2
Member
Member
Posts: 397
Joined: Wed Jun 03, 2015 5:03 am

Re: Exceptions vs destructors

Post by embryo2 »

Rusky wrote:Because all heap objects have an owner somewhere on the stack (directly or indirectly)
If an object was create in function main and the program exit is also in function main, then the object never will be released (only when program exits). It means it is possible to allocate something, to write a destructor for it, but because of an exception, the point, where the destructor is called, will be missed (may be even a thread will be killed, but program doesn't exit, main's stack is present and all it's referenced objects are alive).

Also, it can be said, that every object has an owner somewhere in the heap. But no automatic memory management system can determine if an object is still in use or not if a developer just forget to set the object's reference to null.

Generally speaking, the detection of a situation, when an object should be released, is not as simple as Rust designers think (only stack based deallocation is not enough).
My previous account (embryo) was accidentally deleted, so I have no chance but to use something new. But may be it was a good lesson about software reliability :)
User avatar
Rusky
Member
Member
Posts: 792
Joined: Wed Jan 06, 2010 7:07 pm

Re: Exceptions vs destructors

Post by Rusky »

Nope. An object owned on main()'s stack will be destructed when main() ends, regardless of whether that's through finishing the program or through unwinding. The only way around that is through an abort, which applies equally to finally blocks.

You can keep making things up, but RAII is an established way of managing memory and it has an answer to anything you can come up with.
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Re: Exceptions vs destructors

Post by Roman »

Personally, I always thought that automatic memory management is pretty simple. My compiler would insert code that would increment/decrement reference counters on getting/losing references. This is how ARC in Objective-C works.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
Nable
Member
Member
Posts: 453
Joined: Tue Nov 08, 2011 11:35 am

Re: Exceptions vs destructors

Post by Nable »

Roman wrote:Personally, I always thought that automatic memory management is pretty simple. My compiler would insert code that would increment/decrement reference counters on getting/losing references. This is how ARC in Objective-C works.
And how are you going to fight with circular references?
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Re: Exceptions vs destructors

Post by Roman »

Nable wrote:
Roman wrote:Personally, I always thought that automatic memory management is pretty simple. My compiler would insert code that would increment/decrement reference counters on getting/losing references. This is how ARC in Objective-C works.
And how are you going to fight with circular references?
As I understand, Objective-C (and other languages) solves this with "strong" and "weak" references.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
Post Reply