D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7349 - assert(0) in class destructor - bad (or incorrect) error
Summary: assert(0) in class destructor - bad (or incorrect) error
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 minor
Assignee: No Owner
URL:
Keywords:
: 15705 (view as issue list)
Depends on:
Blocks:
 
Reported: 2012-01-22 10:38 UTC by Taco
Modified: 2021-06-01 06:18 UTC (History)
6 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Taco 2012-01-22 10:38:18 UTC
class C
{
public:
    this()
    {
    }

    ~this()
    {
        assert (0);
    }
}

void main()
{
    C c = new C;
}

Upon destruction, it throws:
core.exception.InvalidMemoryOperationError

Which is very vague / incorrectly describes the problem. The documentation also does not mention throwing exceptions is forbidden in destructors (or are they not?).

For debugging purposes I do like to assert some stuff in the destructor though...
Comment 1 Joakim 2014-05-20 08:50:01 UTC
Is this a bug or is it not allowed to assert in a destructor?  I ran into this while running the druntime tests on Android/x86, was very annoying as the assert in core.sync.semaphore was swallowed up by the InvalidMemoryOperationError so I didn't know where it was actually coming from till I stuck printfs everywhere.  If the assert shouldn't be allowed, druntime and possibly other D code needs to be cleaned up to get rid of any asserts in destructors.
Comment 2 safety0ff.bugz 2014-05-20 12:15:43 UTC
The current GC does not support allocation during destruction (quality of implementation issue.)
When the assert fails and tries to allocate it causes an invalid memory operation error.
Comment 3 Joakim 2014-05-20 17:15:49 UTC
(In reply to safety0ff.bugz from comment #2)
> The current GC does not support allocation during destruction (quality of
> implementation issue.)
> When the assert fails and tries to allocate it causes an invalid memory
> operation error.

Yeah, I know what's happening, which I wrote about in github, the question is how the people who build D plan on resolving it.

It doesn't help when druntime itself is asserting in its destructors, if this hasn't worked in a long time.  Should we be taking all asserts out of destructors?
Comment 4 Walter Bright 2014-11-13 10:59:40 UTC
The real problem is that assert(0) is attempting to allocate.
Comment 5 anonymous4 2016-02-20 16:31:06 UTC
*** Issue 15705 has been marked as a duplicate of this issue. ***
Comment 6 Walter Bright 2021-01-22 14:17:47 UTC
The offending code:

https://github.com/dlang/druntime/blob/f94e816c1dd229cbb133085a6fc41d1bd6d45594/src/core/exception.d#L417-L449

Andrei suggests using a thread local static object to throw.
Comment 7 Walter Bright 2021-01-22 15:18:27 UTC
https://github.com/dlang/druntime/pull/1710
Comment 8 thomas.bockman 2021-01-22 19:22:45 UTC
Given that assert(0) is intended to crash the program, wouldn't it also be reasonable to just allocate using stdc's malloc, or an OS-specific syscall, and not worry about explicitly freeing the memory? It's just going to get freed by the OS when the process terminates an instant later, anyway...
Comment 9 Mathias LANG 2021-06-01 06:18:11 UTC
Fixed in https://github.com/dlang/druntime/pull/3476