Thread overview
delegate memory layout help needed
Feb 13, 2007
nobody
Feb 14, 2007
torhu
Feb 14, 2007
nobody
February 13, 2007
I am trying to debug some code that behaves strangely around delegates. I have been assuming that delegates are just structs with two int-sized fields (just like arrays). Is this correct? Is this struct defined anywhere in DMD supplied D code?

More importantly I have been trying to watch two delegate members of a struct to  make sure they are getting initialized:

    printf("&opIndexDg : [%X]\n", opIndexDg );
    printf("    +0 -> [%X]\n", *(cast(int*)(&opIndexDg)+0) );
    printf("    +1 -> [%X]\n", *(cast(int*)(&opIndexDg)+1) );

    printf("&opIndexAssignDg : [%X]\n", &opIndexAssignDg );
    printf("     +0 -> [%X]\n", *(cast(int*)(&opIndexAssignDg)+0) );
    printf("     +1 -> [%X]\n", *(cast(int*)(&opIndexAssignDg)+1) );

According to what I have seen the delegates in the struct are getting initialized but I keep getting an Array Bounds Error unless I do the exact same assignment done during the initialization afterwards in main(). Am I correctly interpreting the memory layout?
February 14, 2007
nobody wrote:
> More importantly I have been trying to watch two delegate members of a struct to   make sure they are getting initialized:
> 
>      printf("&opIndexDg : [%X]\n", opIndexDg );
>      printf("    +0 -> [%X]\n", *(cast(int*)(&opIndexDg)+0) );
>      printf("    +1 -> [%X]\n", *(cast(int*)(&opIndexDg)+1) );
> 
>      printf("&opIndexAssignDg : [%X]\n", &opIndexAssignDg );
>      printf("     +0 -> [%X]\n", *(cast(int*)(&opIndexAssignDg)+0) );
>      printf("     +1 -> [%X]\n", *(cast(int*)(&opIndexAssignDg)+1) );
> 
> According to what I have seen the delegates in the struct are getting initialized but I keep getting an Array Bounds Error unless I do the exact same assignment done during the initialization afterwards in main(). Am I correctly interpreting the memory layout?

You can access the context pointer with .ptr, and the function pointer with .funcptr.  See:

http://www.digitalmars.com/d/function.html#closures

As for the array bounds error, it's hard to tell without some more context.  A common mistake is to return a delegate whose .ptr points to stack data.
February 14, 2007
torhu wrote:
> nobody wrote:
>> More importantly I have been trying to watch two delegate members of a struct to   make sure they are getting initialized:
>>
>>      printf("&opIndexDg : [%X]\n", opIndexDg );
>>      printf("    +0 -> [%X]\n", *(cast(int*)(&opIndexDg)+0) );
>>      printf("    +1 -> [%X]\n", *(cast(int*)(&opIndexDg)+1) );
>>
>>      printf("&opIndexAssignDg : [%X]\n", &opIndexAssignDg );
>>      printf("     +0 -> [%X]\n", *(cast(int*)(&opIndexAssignDg)+0) );
>>      printf("     +1 -> [%X]\n", *(cast(int*)(&opIndexAssignDg)+1) );
>>
>> According to what I have seen the delegates in the struct are getting initialized but I keep getting an Array Bounds Error unless I do the exact same assignment done during the initialization afterwards in main(). Am I correctly interpreting the memory layout?
> 
> You can access the context pointer with .ptr, and the function pointer with .funcptr.  See:
> 
> http://www.digitalmars.com/d/function.html#closures

Thanks for the suggestion. I realized when I tried to reference .ptr and .funcptr and the compiler complained that these properties did not exist that I was still using an older version of DMD. When I upgraded the strange bug disappeared!