core.exception.InvalidMemoryOperationError@(0)

Bayan Rafeh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 25 00:41:24 PST 2015


On Sunday, 25 January 2015 at 08:39:42 UTC, Bayan Rafeh wrote:
> On Sunday, 25 January 2015 at 00:43:43 UTC, Vladimir Panteleev 
> wrote:
>> On Saturday, 24 January 2015 at 12:16:38 UTC, Bayan Rafeh 
>> wrote:
>>> This problem is a tough one. I've been getting this error 
>>> when I run my unittests, and apparently it is caused by 
>>> attempting an allocation in the destructor from what little I 
>>> could find online about the subject.
>>>
>>> The error is triggered once all my tests are complete, so I'm 
>>> assuming the garbage collector is running before termination, 
>>> but I tried placing logging messages in all my destructors 
>>> and I didn't get anything, so none of them are being called.
>>>
>>> Is there any other possible reason to get this error?
>>
>> Hi,
>>
>> I created a wiki page which I hope will help you solve this 
>> problem:
>>
>> http://wiki.dlang.org/InvalidMemoryOperationError
>>
>> Hope this helps.
>
> Thank you very much, this will certainly be useful in debugging 
> it.
>
>> The allocations may be indirect. For example, formatting a 
>> string to log a message may allocate. Try marking your 
>> destructors as @nogc to see whether the compiler agrees.
>
> I tried what you said and I think I see the problem. I managed 
> to create an example program that duplicates the problem:
>
>
> import std.stdio;
>
> class A {
>     string path;
>
>     this(string p) {
>         path = p;
>     }
>     ~this() {
>     }
>     void a(){}
>
>     void b(){}
> }
>
> class B {
>     A a;
>     this() {
>         this.a = new A("laladiv");
>     }
>     ~this() {
>         delete a;
>     }
> }
>
> void main() {
>     B a = new B();
>     B b = new B();
>     delete b;
> }

The solution was just to remove the "delete a" from the 
destructor if someone comes across this later. Could someone tell 
me why though?


More information about the Digitalmars-d-learn mailing list