Likely closure memory corruption

Maxim Fomin maxim at maxim-fomin.ru
Sun Mar 10 13:00:38 PDT 2013


On Sunday, 10 March 2013 at 19:11:07 UTC, deadalnix wrote:
> On Friday, 8 March 2013 at 16:25:56 UTC, deadalnix wrote:
>> Sooooooo,
>>
>> I have a struct. The struct have a context pointer. I have 
>> this method :
>>
>> @property
>> auto save() inout {
>> 	return inout(Lexer)(t, r.save, line, index);
>> }
>>
>> The context pointer IS NOT COPIED.
>>
>> Fixed it that way :
>>
>> @property
>> auto save() inout {
>> 	// XXX: dmd bug, context pointer isn't copied properly
>> 	// doing it manualy using black magic.
>> 	// Context pointer is the last element of the struct. Here in 
>> position 9.
>> 	auto ret = inout(Lexer)(t, r.save, line, index);
>> 	(cast(void**) &ret)[9] = (cast(void**) &this)[9];
>> 	
>> 	return ret;
>> }
>>
>> Very scary that I have to do that kind of things.
>
> Is this a know bug ? Come on, this is a really bad bug, not the 
> type of thing that can be ignored !

(i do not know such bug)

This code works:

auto foo()
{
     int i;
     struct S
     {
         int a, b, c;
         int foo() { return i; }
         auto save() inout {
             return inout (S)(i,i,i);
         }
     }
     return S();
}

void main()
{
     auto s1 = foo();
     auto s2 = s1.save();
     assert(s2.foo() is 0);
}

So, from your description I cannot reproduce the bug. Reducing is 
time consuming and not pleasant activity, but it is sometimes a 
necessity.


More information about the Digitalmars-d mailing list