| Thread overview | ||||||||
|---|---|---|---|---|---|---|---|---|
|
April 20, 2016 about destroy and delete. | ||||
|---|---|---|---|---|
| ||||
I see https://dlang.org/deprecate.html#delete The delete will be removeed, when will be deprecate? and i test destroy/GC.free and delte in struct, the value is difference; struct Struct { string value = "struct"; ~this() { writeln(value); } } void main() { auto s = new Struct(); delete s; writeln("----------------"); } will printf : struct ---------------- But in void main() { auto s = new Struct(); s.destroy; GC.free(s); writeln("----------------"); } will printf : ---------------- struct If I only GC.free(s); only printf: ---------------- so, I want to know why don't destroy direct printf ? | ||||
April 20, 2016 Re: about destroy and delete. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dsby | And ,will destroy mark the memory in GC to be free? | |||
April 20, 2016 Re: about destroy and delete. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dsby | On Wednesday, 20 April 2016 at 08:10:15 UTC, Dsby wrote:
> I see https://dlang.org/deprecate.html#delete
> ...
> so, I want to know why don't destroy direct printf ?
if you call destroy on struct pointer it is same as assign null to it
so
destroy(s) is same as s = null;
OK it is more like
s = (Struct*).init;
But if you do (*s).destroy(), it will work (ok it will call destructor two times but thats not error)
Or if you use class instead of struct it will works as you expected
| |||
April 20, 2016 Re: about destroy and delete. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dsby | On Wednesday, 20 April 2016 at 08:10:15 UTC, Dsby wrote:
> I see https://dlang.org/deprecate.html#delete
> The delete will be removeed, when will be deprecate?
>
> and i test destroy/GC.free and delte in struct, the value is difference;
>
> struct Struct
> {
> string value = "struct";
> ~this()
> {
> writeln(value);
> }
> }
>
> void main()
> {
>
> auto s = new Struct();
> delete s;
>
> writeln("----------------");
>
> }
>
> will printf :
> struct
> ----------------
>
> But in
> void main()
> {
>
> auto s = new Struct();
> s.destroy;
> GC.free(s);
>
> writeln("----------------");
>
> }
>
> will printf :
> ----------------
> struct
>
> If I only GC.free(s); only printf: ----------------
>
> so, I want to know why don't destroy direct printf ?
This is according to the reference, however this behavior should probably be changed to match that of the class, which will call the destructor immediately.
| |||
April 20, 2016 Re: about destroy and delete. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dsby | The semantics of `delete` from C++ are pretty clear. It is
meant for dynamically allocated memory. destroy(…) however is
a generic tool that brings the thing you pass in back to an
initial state. For pointers, null is assigned, for structs and
classes (which are not pointers but references) the dtor is
called.
Making it do the same thing for an argument of struct type T
and T* should not be done lightly. It will break generic
code, where the location that calls destroy(…) does not own
the pointed-to struct.
--
Marco
| |||
April 21, 2016 Re: about destroy and delete. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | On Wednesday, 20 April 2016 at 09:00:41 UTC, Daniel Kozak wrote:
> On Wednesday, 20 April 2016 at 08:10:15 UTC, Dsby wrote:
>> I see https://dlang.org/deprecate.html#delete
>> ...
>> so, I want to know why don't destroy direct printf ?
>
> if you call destroy on struct pointer it is same as assign null to it
> so
> destroy(s) is same as s = null;
>
> OK it is more like
>
> s = (Struct*).init;
>
> But if you do (*s).destroy(), it will work (ok it will call destructor two times but thats not error)
>
> Or if you use class instead of struct it will works as you expected
Thanks for all.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply