SmartRef: The Smart Pointer In D

biozic via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Mon Jan 16 04:30:45 PST 2017


On Monday, 16 January 2017 at 01:54:35 UTC, Dsby wrote:
> On Sunday, 15 January 2017 at 17:24:25 UTC, biozic wrote:
>> On Sunday, 15 January 2017 at 15:56:30 UTC, Dsby wrote:
>>> and : In 
>>> https://github.com/dlang/phobos/blob/master/std/typecons.d#L147
>>>     ~this()
>>>     {
>>>         debug(Unique) writeln("Unique destructor of ", (_p is 
>>> null)? null: _p);
>>>         if (_p !is null) destroy(_p);
>>>         _p = null;
>>> }
>>>  if the 'T' is a struct, it will not exec the Destory 
>>> function. Is it a bug?
>>
>> What do you mean? This works for me:
>> ---
>> import std.stdio, std.typecons;
>>
>> struct Foo {
>>     ~this() {
>>         writeln("I'm destroyed");
>>     }
>> }
>>
>> void main() {
>>     Unique!Foo foo = new Foo;
>> } // Prints "I'm destroyed"
>> ---
>
> the "writeln("I'm destroyed");" not run the ~this in the Unique 
> destroy function.
> it run in the app exit , THe GC distroy all memony.
> it example can show :
> import std.stdio;
> import std.typecons;
>
>  struct Foo {
>      ~this() {
>          writeln("I'm destroyed");
>      }
>  }
>
>  void fun(){
>     Unique!Foo foo = new Foo;
>     writeln("exit the fun.");
>  }
>
>  void main() {
>      fun();
>       writeln("exit the Main.");
>  }
>
> It is the printf:
>  ~/tmp  rdmd ./type.d
> 
>                                                                 
> 2017年01月16日 星期一 09时50分00秒
> exit the fun.
> exit the Main.
> I'm destroyed
>  ~/tmp 
>
> if you use the struct in Unique, the struct's Destory function 
> is not run in the Unique destroy, it is also run in the GC 
> collet.
> I think it is not the Unique should be.

Right, good point. This behaviour is indeed caused by destroy() 
and is not specific to Unique. But it the case of Unique, relying 
on this (undefined?) behaviour of destroy is a bug (a regression).



More information about the Digitalmars-d-announce mailing list