clear bug?

Ali Çehreli acehreli at yahoo.com
Mon Sep 5 08:46:33 PDT 2011


On Sun, 04 Sep 2011 12:30:58 -0700, Dan Olson wrote:

> Using dmd 2.054 on osx:
> 
> Calling a instance method after using clear() on an instance is giving
> me a bus error.

"Segmentation fault" on my Ubuntu.

> The instance fields look like they are cleared ok, just
> can't safely call a method.

I think that's by design. A cleared object is in an unusable state. In 
fact, I think the implementation actually nulls its vtable pointer.

> I think this used to work after the object
> was cleared,

Correct. In the earlier design the object would be in a default 
initialized state, which would call the destructor twice. Not anymore. I 
think this has changed since the TDPL has been written. (I've just e-
mailed Andrei about the broken link to the "shouldwork" section on http://
erdani.com/tdpl/errata/ )

> and was the reason for clear() over the deprecated delete.

delete used to destroy the object give the memory back. clear just calls 
the destructor. The idea is that the programmer shouldn't have much say 
on a new'ed object's memory.

I think the thought was, if the programmers really cared they could 
allocate the memory from a private memory pool.

> 
> Dan

Ali

> =-=-=
> Sample:
> import std.stdio;
> 
> class A
> {
>    string name = "none";
>    void print() {writeln(name);}
> }
> 
> void main()
> {
>    A a = new A;
>    a.print();                           // none
> 
>    a.name = "xyzzy";
>    a.print();                           // xyzzy
> 
>    clear(a);
>    writeln(a.name);                     // a is reinit to "none" as
>    expected a.print();                           // bus error?
> }
> 
> Output:
> $ dmd clearbug.d
> $ ./clearbug
> none
> xyzzy
> none
> Bus error



More information about the Digitalmars-d-learn mailing list