I'm writing a cross platform app in C. On Linux and macOS it works fine - no leaks, no crashes, large datasets can be loaded and edited without problem.
On Windows (with GTK 3, MinGW and using CLion) it can be relied upon to crash - I can't predict how many hundred bytes of dataset it will load before crashing, but sooner rather than later it will crash. The crash always occurs when freeing memory. If I remove the free then the code runs in debug mode (but not in non-debug mode). If I'm editing the file then the crash happens - anywhere that memory gets freed the code might crash.
One example of code which crashes is this:
void tree_cell_edited (GtkCellRendererText *cell, const gchar *path_string, const gchar *new_text, gpointer data) {
GtkTreeModel *model = (GtkTreeModel *)data;
GtkTreePath *path = gtk_tree_path_new_from_string (path_string);
GtkTreeIter iter;
gint column = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (cell), "column_number"));
gtk_tree_model_get_iter (model, &iter, path);
char *upd_text=(char *)calloc( strlen(new_text) * 2, sizeof(char) );
strcpy(upd_text, new_text);
gtk_tree_store_set(GTK_TREE_STORE (model), &iter, column, upd_text, -1);
free(upd_text);
gtk_tree_path_free (path);
}
If I comment out free(upd_text);
then it won't crash - but it only won't crash in Debug mode, and this isn't the right solution to the problem.
Is there something wrong with this code? Why would freeing memory cause the crash - it hasn't been freed anywhere else (unless Windows has some garbage collection mojo going on even for C.)
SOLVED For some reason, the issue was down to insufficient memory being allocated. Curious that it didn't cause a problem on MacOS or Linux.