--- trunk/src/geotoad.c 2009/11/24 14:02:35 207 +++ trunk/src/geotoad.c 2009/11/24 15:21:22 208 @@ -68,6 +68,8 @@ GtkWidget *username, *password, *filename; GtkWidget *lat, *lon, *dst; + + int use_cnt; } gt_context_t; @@ -76,14 +78,20 @@ } static void context_free(gt_context_t *context) { - // printf("freeing context\n"); + context->use_cnt--; + + if(context->use_cnt > 0) + printf("still in use by %d, keeping context\n", context->use_cnt); + else { + printf("freeing context\n"); - if(context->info) g_free(context->info); + if(context->info) g_free(context->info); - g_ptr_array_foreach(context->argv, arg_free, NULL); - g_ptr_array_free (context->argv, TRUE); + g_ptr_array_foreach(context->argv, arg_free, NULL); + g_ptr_array_free (context->argv, TRUE); - g_free(context); + g_free(context); + } } static void appendf(struct log_s *log, char *colname, @@ -211,11 +219,13 @@ /* this printf will actually be redirected through the pipe */ printf("Error: Failed to execute!\n"); + context_free(context); + exit(0); } else { /* we're having major problems... */ close(p[0]); close(p[1]); - /* this printf will actually be redirected through the pipe */ + /* this printf will actually be redirected through the pipe??? No! */ printf("Error: Failed to fork!\n"); } @@ -235,10 +245,16 @@ fh = gt_popen(context, (char**)(context->argv->pdata),"r", &context->pid); if(!fh) { - append_parentf(context, COLOR_ERR, _("Failed to run GeoToad")); + printf("fail free\n"); + context_free(context); + // g_thread_exit(NULL); return NULL; } + /* the client is running */ + printf("client inc use_cnt\n"); + context->use_cnt++; + /* switch to line buffered mode */ if(setvbuf(fh, NULL, _IOLBF, 0)) perror("setvbuf(_IOLBF)"); @@ -261,10 +277,12 @@ g_io_channel_unref(gh); pclose(fh); append_parentf(context, COLOR_SYSTEM, "Job done!"); - - g_thread_exit(NULL); + + /* TODO: this happens on the copy!!!! */; + printf("client context free = %d\n", context->use_cnt); context_free(context); + // g_thread_exit(NULL); return NULL; } @@ -282,7 +300,7 @@ /* build list of arguments to call geotoad */ context->argv = g_ptr_array_new(); - g_ptr_array_add (context->argv, g_strdup_printf(GEOTOAD)); + g_ptr_array_add (context->argv, g_strdup_printf(GEOTOAD "a")); g_ascii_dtostr(str, sizeof(str), context->appdata->gt.distance); g_ptr_array_add (context->argv, g_strdup_printf("--distanceMax=%s", str)); g_ptr_array_add (context->argv, g_strdup_printf("--output=%s", context->appdata->gt.filename)); @@ -591,6 +609,7 @@ gt_context_t *context = g_new0(gt_context_t, 1); context->appdata = appdata; + context->use_cnt++; // parent still uses this context->update_mutex = g_mutex_new(); context->update_cond = g_cond_new(); @@ -600,6 +619,9 @@ if(gui_setup(context)) gui_run(context); + + printf("main context free\n"); + context_free(context); } gboolean geotoad_available(void) {