Bug 13038 – Calling to!String in the destructor

Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2014-07-04T03:22:00Z
Last change time
2014-07-04T04:52:46Z
Assigned to
nobody
Creator
zhnavigator

Comments

Comment #0 by zhnavigator — 2014-07-04T03:22:17Z
import std.stdio; import std.conv; class CDummy { static size_t counter; this() { counter++; } ~this() { counter--; writeln(to!string(counter)); // <= Ok!!! } } int main(string[] argv) { foreach(i; 0..10) { auto obj = new CDummy; destroy(obj); } return 0; } Ok!!! //--------------------------------------------- import std.stdio; import std.conv; class CDummy { static size_t counter; this() { counter++; } ~this() { counter--; writeln(to!string(counter)); //<== core.exception.InvalidMemoryOperationError } } int main(string[] argv) { foreach(i; 0..10) { auto obj = new CDummy; //destroy(obj); } return 0; } Crash!!! core.exception.InvalidMemoryOperationError //--------------------------------------------- import std.stdio; import std.conv; class CDummy { static size_t counter; this() { counter++; } ~this() { counter--; writeln(counter); // <== ok } } int main(string[] argv) { foreach(i; 0..10) { auto obj = new CDummy; //destroy(obj); } return 0; } Ok!!!
Comment #1 by issues.dlang — 2014-07-04T04:52:46Z
It is invalid to allocate memory in a class destructor, which to!string is going to do unless it's passed a string. It's probably working when you call destroy, because then the destructor is not being called by the GC, but the GC does not allow you to do anything with it when it's running a collection - which means that class destructors cannot manipulate the GC heap. It might be annoying at times, but it's a limitation that we have to live with.