3

I am developing a GTK+3 application in C using MSVC (Visual Studio) on windows for a college project. I've run the debugger and found that the application crashes while returning from a libffi call. The stack is corrupted and hence the program's return address is garbage.

The thing is, it runs fine in Release mode, probably due to optimization, but crashes in Debug mode. What could be the cause?

I have no clue how to resolve the problem... Any help would be appreciated.

Here is the part of the code which causes the error:

  ffi_call_win64 (stack, frame, closure);
} // Error here
Exception thrown: read access violation.
pn was 0xFFFFFFFFFFFFFFFB.

MCVE

#include <gtk/gtk.h>

static void on_activate(GtkApplication* app) {
    // Create a new window
    GtkWidget* window = gtk_application_window_new(app);
    // Create a new button
    GtkWidget* button = gtk_button_new_with_label("Hello, World!");
    // When the button is clicked, destroy the window passed as an argument
    g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_widget_destroy), window);
    gtk_container_add(GTK_CONTAINER(window), button);
    gtk_widget_show_all(window);
}

int main(int argc, char* argv[]) {
    // Create a new application
    GtkApplication* app = gtk_application_new("com.example.GtkApplication",
        G_APPLICATION_FLAGS_NONE);
    g_signal_connect(app, "activate", G_CALLBACK(on_activate), NULL);
    return g_application_run(G_APPLICATION(app), argc, argv);
}
  • Try creating an MCVE and reproducing the problem. Check whether some variables are uninitialized. Also try dynamic code analysis. – Alexander Dmitriev Mar 19 '20 at 08:14
  • Hey, Thanks for the reply! Assuming that MCVE means the minimal code required to reproduce the problem... I have copied the Hello World gtk sample program from gtk.org itself and the crash occurs even before a window shows up. The issue is I am not familiar with the low level implementation of libffi, so i can't really make out whether the variables are initialized or not? – Ujwal Kundur Mar 19 '20 at 16:33
  • Can you post your mcve? – Alexander Dmitriev Mar 20 '20 at 08:22
  • Sure.. I've edited the question to include it – Ujwal Kundur Mar 20 '20 at 10:45
  • 1
    Well... This code looks ok (and doesn't crash on linux), but it doesn't include any ffi calls. Does it crash too? – Alexander Dmitriev Mar 20 '20 at 12:04
  • Yep, this and any code that calls a Gtk library-based function. Maybe its an issue with the port? But why doesn't it crash in release mode though? Should I re-install the library and see if it fixes anything? – Ujwal Kundur Mar 20 '20 at 14:45
  • I tried re-installing the library, and it still crashes... I went through the disassembly and found that the release version has a LOT less assembly code and the part that causes the crash is optimized away.... So should I just bank on it working safely in release mode? – Ujwal Kundur Mar 20 '20 at 15:38
  • Here's a related issue on github: https://github.com/microsoft/vcpkg/issues/10230 – Marcin Zawiejski Jun 01 '20 at 15:04
  • Hey, Thanks for the link!! I'm pretty sure its a Visual Studio-specific bug now... – Ujwal Kundur Jun 02 '20 at 18:27

0 Answers0