[Issue 704] `class` destructor is called even if constructor throws

2021-12-23 Thread d-bugmail--- via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=704

feklushkin.de...@gmail.com changed:

   What|Removed |Added

 CC||feklushkin.de...@gmail.com

--- Comment #16 from feklushkin.de...@gmail.com ---
(I spent a few days looking for an issue caused by this mandatory call of dtor
when ctor thrown an exception.)

Why it is need to call dtor if ctor isn't sucessful finished?
For purposes of simple correct cleanup we already have scope guards - it can be
used inside of ctor.

And this behaviour should be at least described in the manual.

--


[Issue 704] `class` destructor is called even if constructor throws

2019-05-20 Thread d-bugmail--- via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=704

Mathias LANG  changed:

   What|Removed |Added

 Status|REOPENED|RESOLVED
 CC||pro.mathias.l...@gmail.com
 Resolution|--- |FIXED

--- Comment #15 from Mathias LANG  ---
The dtor of scope allocated classes is not called anymore.

--


[Issue 704] `class` destructor is called even if constructor throws

2018-01-18 Thread d-bugmail--- via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=704

--- Comment #14 from anonymous4  ---
(In reply to Steven Schveighoffer from comment #13)
> (what the dtor is expecting)

D has options there unlike C++.

--


[Issue 704] `class` destructor is called even if constructor throws

2018-01-18 Thread d-bugmail--- via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=704

Steven Schveighoffer  changed:

   What|Removed |Added

 CC||schvei...@yahoo.com

--- Comment #13 from Steven Schveighoffer  ---
(In reply to anonymous4 from comment #12)
> Isn't it only true for C++ that destructor can't be called on an object that
> wasn't constructed? In C++ destructor can't be called because there's no
> default initialization before constructor, but in D I think destructor can
> be called because it won't run on garbage.

It's not garbage, but it's also potentially not a valid object (what the dtor
is expecting).

I'm unsure who is responsible for cleaning up the non-GC resources. Normally
the destructor does this, but if you are throwing in the constructor, then you
could do it there. However, base constructors will not be able to catch this
condition.

If I had to define it myself, I think it shouldn't be left to the GC to clean
it up. If the destructor should be called after the constructor fails via
exception, it should happen immediately (and only the destructors for which the
constructors have completed).

e.g.:

class A
{
int x;
this() { writeln("A.ctor"); x = 5; }
~this() { writeln("A.dtor"); assert(x == 5); x = 0; }
}

class B : A
{
int y;
this(bool bad) { writeln("B.ctor"); super(); if(bad) throw new Exception();
y = 6; }
~this() { writeln("B.dtor"); assert(y == 6); y = 0; }
}

void main()
{
try { new B(true); } catch() {}
writeln("done main");
}

expected output:
A.ctor
B.ctor
A.dtor
done main

--


[Issue 704] `class` destructor is called even if constructor throws

2018-01-18 Thread d-bugmail--- via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=704

--- Comment #12 from anonymous4  ---
Isn't it only true for C++ that destructor can't be called on an object that
wasn't constructed? In C++ destructor can't be called because there's no
default initialization before constructor, but in D I think destructor can be
called because it won't run on garbage.

--


[Issue 704] `class` destructor is called even if constructor throws

2018-01-17 Thread d-bugmail--- via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=704

hst...@quickfur.ath.cx changed:

   What|Removed |Added

 CC||hst...@quickfur.ath.cx

--- Comment #11 from hst...@quickfur.ath.cx ---
Does this bug only apply for scope classes? I tested the following code on dmd
git master:

--
class MyClass {
this() {
writeln("ctor");
throw new Exception("");
}
~this() {
writeln("dtor");
}
}
void main() {
try {
scope c = new MyClass;
} catch(Exception e) {
// ignore
}
}
--

Only the ctor is called, not the dtor, because the ctor throws and does not
properly construct the object. This is correct behaviour.

However, changing `scope` to `auto` will cause the dtor to still be called, in
spite of the ctor throwing an exception.  According to Andrei's comment, this
is wrong, since the object was not fully initialized, and the exception from
the ctor should be taken to mean that construction of the object has been
abandoned. Not sure if this belongs to this bug, though, or to a different bug,
since the original complaint involved "auto" classes, which I'm assuming was
the original name of scope classes.

--


[Issue 704] `class` destructor is called even if constructor throws

2015-06-09 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=704

Andrei Alexandrescu and...@erdani.com changed:

   What|Removed |Added

Version|D1  D2 |D2

--


[Issue 704] `class` destructor is called even if constructor throws

2014-11-13 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=704

--- Comment #9 from Walter Bright bugzi...@digitalmars.com ---
Why was this reopened? I can't figure out what the bug is from the comments.

--


[Issue 704] `class` destructor is called even if constructor throws

2014-11-13 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=704

Steven Schveighoffer schvei...@yahoo.com changed:

   What|Removed |Added

 CC|schvei...@yahoo.com |

--


[Issue 704] `class` destructor is called even if constructor throws

2014-11-13 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=704

Steven Schveighoffer schvei...@yahoo.com changed:

   What|Removed |Added

 CC||schvei...@yahoo.com

--- Comment #10 from Steven Schveighoffer schvei...@yahoo.com ---
(In reply to Gide Nwawudu from comment #3)
 I noticed this issue in my code. I think the problem is that the following
 code should not be allowed.
 
scope MyClass c; // or auto MyClass c;
c = new MyClass();
 
 The following code works correctly.
scope MyClass c = new MyClass();
 
 Maybe the tests should be changed in dstress.

This is the comment where it was reopened, if that helps (I'm with you, I think
this should be closed).

--


[Issue 704] `class` destructor is called even if constructor throws

2014-07-11 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=704

Denis Shelomovskij verylonglogin@gmail.com changed:

   What|Removed |Added

 CC||verylonglogin@gmail.com
   Hardware|x86 |All
Summary|destructors are called even |`class` destructor is
   |if the constructor throws   |called even if constructor
   |an exception|throws
 OS|Linux   |All

--- Comment #8 from Denis Shelomovskij verylonglogin@gmail.com ---
---(In reply to Andrei Alexandrescu from comment #7)
 On the other hand we don't throw if a struct's ctor throws. So this is an
 inconsistency.I think it's fair in D to consider a throwing constructor as
 abandoning all construction of the object.

Also `scope` classes behaves like structs:
---
class C
{
this() { throw new Exception(); }

~this() { assert(0); } // never called
}

void main()
{
try scope c = new C(); catch(Exception) { }
}
---

As for structs, we also have Issue 13095.

--