D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 8139 - Make objects really disposable by addition of "Object finalized" assertion
Summary: Make objects really disposable by addition of "Object finalized" assertion
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords: contracts, diagnostic
Depends on:
Blocks:
 
Reported: 2012-05-24 03:39 UTC by Denis Shelomovskii
Modified: 2024-12-13 18:00 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Denis Shelomovskii 2012-05-24 03:39:44 UTC
Original NG thread:
True disposable objects (add "Finalized!" assertion)
http://forum.dlang.org/thread/jo0p3a$aue$1@digitalmars.com

First message from NG thread:
This idea is too obvious and I suppose I'm the only one not knowing it, 
but I have never seen it's implementation. Why?

The idea:
1. `Object` class has hidden `isAlive` field which is true since 
construction and up to finalization.
2. Every method asserts that the object is alive first.
3. There is an `finalize` function that just rt_finalize an object in 
debug mode but can even free memory in release mode.

Isn't it no-brainer? Isn't it the only way to debug manual memory 
management and shared resources without error-prone boilerplate?

This is what I missed in C#, where I can dispose an object but I have to 
manually check the object isn't disposed every time I use it or in every 
it's method to find where I'm doing something with a disposed object by 
a mistake.


IMHO finding using of dead references is almost as major as not allowing 
to free memory of alive objects (I mean GC), but GC is often implemented 
and dead references detection isn't.

Strongly require your thoughts.
Comment 1 Alex Rønne Petersen 2012-05-24 03:46:56 UTC
The idea in and of itself is not bad. In fact, it would make debugging wonderfully easy. My only concern is this: Object size. We already store two words of memory in *every single object header*. This means 8 bytes on 32-bit and 16 bytes on 64-bit.

Now suppose we added an extra bool field to Object. Not only would the compiler have to be changed to align fields correctly, but it would also result in objects eating 12 bytes on 32-bit and 24 bytes on 64-bit (simply because the GC only power of two allocations or something along those lines).

Now, the memory concern is not a problem for a class like this:

class A
{
    bool b;
    short s;
}

Obviously we don't need word alignment here, and we could probably optimize given that. But consider:

class B
{
    A a;
}

Suddenly that bool field has to suck an entire machine word's worth of space for 'a' to be aligned correctly.
Comment 2 Denis Shelomovskii 2012-05-24 04:00:11 UTC
(In reply to comment #1)
> Now suppose we added an extra bool field to Object.

From Sean Kelly's reply at NG:
> rt_finalize currently nulls out the vtbl pointer, which can server as an isAlive flag if desired.

Link: http://forum.dlang.org/thread/jo0p3a$aue$1@digitalmars.com#post-mailman.323.1336157840.24740.digitalmars-d:40puremagic.com
Comment 3 Alex Rønne Petersen 2012-05-24 04:00:51 UTC
In that case, I have no objections to doing this.
Comment 4 Jacob Carlborg 2012-05-24 05:10:01 UTC
> From Sean Kelly's reply at NG:
> > rt_finalize currently nulls out the vtbl pointer, which can server as an isAlive flag if desired.
> 
> Link:
> http://forum.dlang.org/thread/jo0p3a$aue$1@digitalmars.com#post-mailman.323.1336157840.24740.digitalmars-d:40puremagic.com

The vtbl is only needed for virtual methods.
Comment 5 Alex Rønne Petersen 2012-05-24 05:11:06 UTC
It's still set for all objects because it contains a pointer to type info.

(The other machine word is the monitor.)
Comment 6 dlangBugzillaToGithub 2024-12-13 18:00:03 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/18442

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB