On Monday, April 29, 2013 23:04:29 =?UTF-8?B?Ikx1w61z?=.Marques <luismarq...@gmail.com>@puremagic.com wrote: > This crashes in the last line of main: > > class A > { > void foo() {} > } > > void main() > { > A a = new A(); > a.foo(); > clear(a); > assert(a !is null); > a.foo(); // crashes > } > > As far as I understand from TDPL book, this should not crash, but > it does (DMD64 v2.062, OS X). Am I misunderstanding clear()? > > BTW, why not make clear also change 'a' to null?
I think that what TDPL says about clear may be outdated (I don't recall exactly what it said). But clear destroys the class object and zeroes out the vtbl, and it's invalid to do anything with the object after that. It's purely for cases where you want to destroy the object without waiting for the GC to do it (but it doesn't free any memory, so it's pretty much only of use for making sure that the destructor/finalizer gets run). It's very much on purpose that the app crashes when you use an object which you called clear on. Also, clear has been renamed to destroy (leaving clear as an alias to destroy), so new code should be using destroy. - Jonathan M Davis