Thread overview | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 05, 2021 Can't I allocate at descontructor? | ||||
---|---|---|---|---|
| ||||
The following code returns a memory error. I did notice it did happens whenever I did a memory allocation. Is this not possible in the descontrutor? if so, why?
> core.exception.InvalidMemoryOperationError@src\core\exception.d(647): Invalid memory operation
import std.stdio;
int main()
{
auto a = new A;
return 0;
}
class A
{
this() { }
~this()
{
f();
}
}
void f()
{
auto str = new string[100];
}
|
March 05, 2021 Re: Can't I allocate at descontructor? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack | On Friday, 5 March 2021 at 05:31:38 UTC, Jack wrote:
> The following code returns a memory error. I did notice it did happens whenever I did a memory allocation. Is this not possible in the descontrutor? if so, why?
>
GC prohibits allocation during collection, since this dtor is likely called by GC this is what happens.
If you REALLY need this just allocate using other mechanisms.
|
March 05, 2021 Re: Can't I allocate at descontructor? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack | On Friday, 5 March 2021 at 05:31:38 UTC, Jack wrote: > The following code returns a memory error. I did notice it did happens whenever I did a memory allocation. Is this not possible in the descontrutor? if so, why? https://dlang.org/blog/2021/03/04/symphony-of-destruction-structs-classes-and-the-gc-part-one/ |
March 05, 2021 Re: Can't I allocate at descontructor? | ||||
---|---|---|---|---|
| ||||
Posted in reply to evilrat | On Friday, 5 March 2021 at 05:42:03 UTC, evilrat wrote:
> On Friday, 5 March 2021 at 05:31:38 UTC, Jack wrote:
>> The following code returns a memory error. I did notice it did happens whenever I did a memory allocation. Is this not possible in the descontrutor? if so, why?
>>
>
> GC prohibits allocation during collection, since this dtor is likely called by GC this is what happens.
>
> If you REALLY need this just allocate using other mechanisms.
I didn't know that, it seems even if I use other allocation mechanism there's no guarantee the deconstructor will be called so it seems the native descontrutor will not be of help at all
|
March 05, 2021 Re: Can't I allocate at descontructor? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | On Friday, 5 March 2021 at 09:23:29 UTC, Mike Parker wrote:
> On Friday, 5 March 2021 at 05:31:38 UTC, Jack wrote:
>> The following code returns a memory error. I did notice it did happens whenever I did a memory allocation. Is this not possible in the descontrutor? if so, why?
>
> https://dlang.org/blog/2021/03/04/symphony-of-destruction-structs-classes-and-the-gc-part-one/
thanks for such good article. So if the object was allocated on heap, there's no guarantee that the object's destrutor will be called at all? do destrutor allocate at stack are guarantee to be run?
|
March 05, 2021 Re: Can't I allocate at descontructor? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack | On Friday, 5 March 2021 at 20:03:58 UTC, Jack wrote:
> On Friday, 5 March 2021 at 09:23:29 UTC, Mike Parker wrote:
>> On Friday, 5 March 2021 at 05:31:38 UTC, Jack wrote:
>>> The following code returns a memory error. I did notice it did happens whenever I did a memory allocation. Is this not possible in the descontrutor? if so, why?
>>
>> https://dlang.org/blog/2021/03/04/symphony-of-destruction-structs-classes-and-the-gc-part-one/
>
> thanks for such good article. So if the object was allocated on heap, there's no guarantee that the object's destrutor will be called at all? do destrutor allocate at stack are guarantee to be run?
Destructors of structs on the stack will always run deterministically.
|
March 05, 2021 Re: Can't I allocate at descontructor? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack | On Fri, Mar 05, 2021 at 08:03:58PM +0000, Jack via Digitalmars-d-learn wrote: > On Friday, 5 March 2021 at 09:23:29 UTC, Mike Parker wrote: > > On Friday, 5 March 2021 at 05:31:38 UTC, Jack wrote: > > > The following code returns a memory error. I did notice it did happens whenever I did a memory allocation. Is this not possible in the descontrutor? if so, why? > > > > https://dlang.org/blog/2021/03/04/symphony-of-destruction-structs-classes-and-the-gc-part-one/ > > thanks for such good article. So if the object was allocated on heap, there's no guarantee that the object's destrutor will be called at all? Yes. And also if it does get called, there's no guarantee what order it will be called in w.r.t. other dtors. And you cannot perform any GC operations in it. > do destrutor allocate at stack are guarantee to be run? Yes. T -- If it tastes good, it's probably bad for you. |
March 05, 2021 Re: Can't I allocate at descontructor? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Haughton | On Friday, 5 March 2021 at 20:10:39 UTC, Max Haughton wrote:
> On Friday, 5 March 2021 at 20:03:58 UTC, Jack wrote:
>> On Friday, 5 March 2021 at 09:23:29 UTC, Mike Parker wrote:
>>> On Friday, 5 March 2021 at 05:31:38 UTC, Jack wrote:
>>>> The following code returns a memory error. I did notice it did happens whenever I did a memory allocation. Is this not possible in the descontrutor? if so, why?
>>>
>>> https://dlang.org/blog/2021/03/04/symphony-of-destruction-structs-classes-and-the-gc-part-one/
>>
>> thanks for such good article. So if the object was allocated on heap, there's no guarantee that the object's destrutor will be called at all? do destrutor allocate at stack are guarantee to be run?
>
> Destructors of structs on the stack will always run deterministically.
But the ones heap may never run at all, is that right?
|
March 05, 2021 Re: Can't I allocate at descontructor? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack | On Friday, 5 March 2021 at 20:13:54 UTC, Jack wrote:
> On Friday, 5 March 2021 at 20:10:39 UTC, Max Haughton wrote:
>> On Friday, 5 March 2021 at 20:03:58 UTC, Jack wrote:
>>> On Friday, 5 March 2021 at 09:23:29 UTC, Mike Parker wrote:
>>>> On Friday, 5 March 2021 at 05:31:38 UTC, Jack wrote:
>>>>> [...]
>>>>
>>>> https://dlang.org/blog/2021/03/04/symphony-of-destruction-structs-classes-and-the-gc-part-one/
>>>
>>> thanks for such good article. So if the object was allocated on heap, there's no guarantee that the object's destrutor will be called at all? do destrutor allocate at stack are guarantee to be run?
>>
>> Destructors of structs on the stack will always run deterministically.
>
> But the ones heap may never run at all, is that right?
You can't rely on the garbage collector for deterministic destruction, no.
|
March 05, 2021 Re: Can't I allocate at descontructor? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Haughton | On Friday, 5 March 2021 at 20:18:44 UTC, Max Haughton wrote:
> On Friday, 5 March 2021 at 20:13:54 UTC, Jack wrote:
>> On Friday, 5 March 2021 at 20:10:39 UTC, Max Haughton wrote:
>>> On Friday, 5 March 2021 at 20:03:58 UTC, Jack wrote:
>>>> On Friday, 5 March 2021 at 09:23:29 UTC, Mike Parker wrote:
>>>>> On Friday, 5 March 2021 at 05:31:38 UTC, Jack wrote:
>>>>>> [...]
>>>>>
>>>>> https://dlang.org/blog/2021/03/04/symphony-of-destruction-structs-classes-and-the-gc-part-one/
>>>>
>>>> thanks for such good article. So if the object was allocated on heap, there's no guarantee that the object's destrutor will be called at all? do destrutor allocate at stack are guarantee to be run?
>>>
>>> Destructors of structs on the stack will always run deterministically.
>>
>> But the ones heap may never run at all, is that right?
>
> You can't rely on the garbage collector for deterministic destruction, no.
Are there some kind of replacement or I have to make my own finalize-like method, once I determine somewhat the application no longer need those resources? aside from destructor for memory allocated on stack, what are uses for destrutors?
|
Copyright © 1999-2021 by the D Language Foundation