void* pointers get corrupted: D bug or misunderstanding?

Federico Santamorena federico at santamorena.me
Sun Jul 28 19:00:44 UTC 2019


On Sunday, 28 July 2019 at 18:54:48 UTC, Federico Santamorena 
wrote:
> On Sunday, 28 July 2019 at 18:37:26 UTC, Dennis wrote:
>> On Sunday, 28 July 2019 at 18:32:24 UTC, Federico Santamorena 
>> wrote:
>>> So the problem I am noticing is very simple: as you may know 
>>> GTK callbacks have a void* user_data argument, what happens, 
>>> is that if the flow of code is this: D => C => D the void* 
>>> pointer when reaching the third step and finally emerging to 
>>> the D language will change its value and get corrupted.
>>
>> Can you give your exact callback function definition? I once 
>> had my context pointer corrupted because I marked my callback 
>> extern(C) instead of extern(Windows). Maybe you also have an 
>> error in your definition somewhere.
>
> It's a bit complex but:
>
>
> context is a pointer to a D struct
> extern(C) void gtk_search_changed(GtkEditable* widget, void* 
> data) { //body }
> context.search_input.g_signal_connect("changed", 
> &gtk_search_changed, context);
>
> Then inside gtk_search_changed, startCrawling gets called and 
> context is just passed to it as the last argument:
>
> DrillContext* startCrawling(in const(DrillConfig) config,
>                                    in immutable(string) 
> searchValue,
>                                    in immutable(void 
> function(immutable(FileInfo) result, void* userObject)) 
> resultCallback,
>                                    in void* userObject) { 
> //body }
>
> And gets called like this:
> startCrawling(drillConfig, searchString, &resultFound, context);
>
>
> Then when the resultCallback gets called its userObject (that 
> now should be a pointer to context) is now corrupt:
>
> void resultFound(immutable(FileInfo) result, void* userObject) 
> { //body}
>
> And now inside resultFound the void* is completely corrupt

Here, the GitHub file:

https://github.com/yatima1460/Drill/blob/576b4b691ea5357e5271115433eb11d4c0beeae7/Source/Frontend/GTK/Main.d

It's an experimental test using GTK bindings.

Here the flow:

D:
https://github.com/yatima1460/Drill/blob/576b4b691ea5357e5271115433eb11d4c0beeae7/Source/Frontend/GTK/Main.d#L586

extern(C):
https://github.com/yatima1460/Drill/blob/576b4b691ea5357e5271115433eb11d4c0beeae7/Source/Frontend/GTK/Main.d#L323

https://github.com/yatima1460/Drill/blob/576b4b691ea5357e5271115433eb11d4c0beeae7/Source/Frontend/GTK/Main.d#L384

D again:
https://github.com/yatima1460/Drill/blob/576b4b691ea5357e5271115433eb11d4c0beeae7/Source/Frontend/GTK/Main.d#L299

Now userObject inside resultFound is corrupt


More information about the Digitalmars-d mailing list