Ownership semantics
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Jan 31 12:20:52 PST 2016
On 1/31/16 3:15 PM, Matt Elkins wrote:
> On Sunday, 31 January 2016 at 20:11:07 UTC, Matt Elkins wrote:
>> On Sunday, 31 January 2016 at 20:10:03 UTC, Matt Elkins wrote:
>>> On Sunday, 31 January 2016 at 20:07:26 UTC, Steven Schveighoffer wrote:
>>>> What is likely happening is that ptr is already collected, and you
>>>> are invalidly attempting to re-free it.
>>>
>>> The GC can collect this memory even though there is still an
>>> outstanding root-reachable pointer to it?
>>
>> Or maybe it isn't root-reachable?
>
> No, it is still present even if root-reachable:
>
> [code]
> unittest
> {
> import std.algorithm;
>
> int* x;
> {
> auto map = new UniquePtr!int[1];
> auto uniqueX = UniquePtr!int(5);
> x = uniqueX.get();
> map[0] = move(uniqueX);
> }
> }
> [/code]
>
> [output]
> core.exception.InvalidMemoryOperationError at src\core\exception.d(679):
> Invalid memory operation
> ----------------
> Program exited with code 1
> Made 632560 for this 18FD90
> Disposing null for this 18FD70
> Disposed null for this 18FD70
> Disposing null for this 18FD90
> Disposed null for this 18FD90
> All unit tests have been run successfully.
> Disposing 632560 for this 632550
> [/output]
Oh, nevermind. This is actually simpler.
You can't do memory operations inside a destructor during collection. I
forgot about that.
But the rule I stated is still in force.
-Steve
More information about the Digitalmars-d-learn
mailing list