My program segafaults randomly upon return from a certain function



Okay, weird this. My program segv's (but doesn't dump core :-() at the
return point of a function. It doesn't happen everytime, just randomly. I am
wondering if it is a memory problem (most likely!). But can anyone see why
and where? I am a little baffled...

Okay The callback is this:

   gtk_signal_connect (GTK_OBJECT (button_transextract_ok), "clicked",
                       GTK_SIGNAL_FUNC (on_button_transextract_ok_clicked),
                       progress);

The callback function is this:

void on_button_transextract_ok_clicked (GtkButton * button, gpointer
user_data)
{
   extern options pool;

   if (pool.bss == FALSE && pool.caf == FALSE && pool.sb == FALSE) {
      const gchar *text =
         "You must choose at least one of 'bss', 'caf' or 'servebase'.";

      GtkWidget *message = create_messagebox_window (text);

      gtk_widget_show (message);
   } else
      exec_transextract (user_data);
}

and the exec_transaction function is this:

void exec_transextract (gpointer progress)
{
   char **argv = NULL;
   pid_t pid = fork ();
   extern options pool;
   int timer, status, argc = 1;
   const char *prog = "/usr/dec/bin/transextract";

   argv = (char**) malloc (2 * sizeof (char*));
   argv[0] = (char*) malloc (strlen (prog) + 1);
   strcpy (argv[0], prog);

   if (pool.bss == TRUE) {
      argv[argc] = (char*) malloc (4 * sizeof (char));
      argv[argc] = "bss";
      argc++;
   }

   if (pool.caf == TRUE) {
      argv[argc] = (char*) malloc (4 * sizeof (char));
      argv[argc] = "caf";
      argc++;
   }

   if (pool.sb == TRUE) {
      argv[argc] = (char*) malloc (10 * sizeof (char));
      argv[argc] = "servebase";
      argc++;
   }

   argv[argc] = NULL;

   timer = gtk_timeout_add (50, progress_update, progress);
   if (pid == 0) {
      /* in the child process */
      printf ("Child process transextract (%d) started.\n", getpid ());
      execv (prog, argv);
   } else if (pid != -1) {
      /* in the parent process */
      while ((waitpid (pid, &status, WNOHANG)) == 0)
         /* child still exists */
         while (gtk_events_pending ())
            gtk_main_iteration ();

      gtk_timeout_remove (timer);
      gtk_progress_set_value (GTK_PROGRESS (progress), 0);
      if (!WIFEXITED (status)) {
         const gchar *text = "The child process transextract exited
abnormally.";
         GtkWidget *message = create_messagebox_window (text);

         gtk_widget_show (message);
      } else
         printf ("Child process transextract (%d) finished.\n", pid);
   } else
      printf ("Failed to fork() new process (transextract).\n");

   while (--argc != 0)
      free (argv[argc]);

   free (argv); /* sometimes segv's here (or the line before, or after, with
gdb I can't really tell) */
}

Any ideas? Points would be appreciated. Cheers.

Jim

--




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]