LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-12-2010, 03:15 AM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
GTK+ problem loading image segfault


Ok I have been trying to figure out this issue for a couple of days now and I can't seam to figure it out on my own. Below is the code for a simple image viewer that I am working on. It works alright on my LFS box but when I attempt to use it on my Arch system it gives a segfault. The only diffrence that I can see is that somehow on my LFS system its getting the entire path to the file and on Arch it is only getting the file name for the file being loaded. Also in arch if i supply the full file name path it works correctly. The part in red is where I think the problem is.

Note: Below is not the entire code here is a link to the full source files.

Code:
 
/* Simple gtk program for displaying images from most formats *
 * My goal for this software is to be something very simmilar * 
 * to the windows image viewer in windows xp. I like how its  * 
 * very minimal disallowing you to make any kind of changes   * 
 * the image. I don't like to immate stuff from windows as    * 
 * much as possible but I found that a simple image viewer    * 
 * was not really around other then iv which allows you to do * 
 * too much and does not use the GTK libary.                  * 
 * I hope this software will become usefull to somone other   * 
 * then me. I welcome any suggestions,feedback,suggestions, or* 
 * whatever you think may be relevant.                        * 
 * Some code is derived from the gtk+ widgest II tutorial     * 
 * located at http://zetcode.com/tutorials                    * 
 *                                                            *
 * 	You can contact me at treah.blade@gmail.com           * 
 * NOTE: This software is covered under the GPLv3 licence     *
 *         You can obtain a copy of the licence at            * 
 *         http://www.gnu.org/licenses/gpl-3.0.txt	      *
 */ 

#include"main.h"
#include"functions.h"

int main ( int argc, char *argv[]) 
{ 
  
    char buffer[MAX_NAME_SIZE]; 
    GtkWidget *window; 
    GtkWidget *image; 
    GError *err = NULL; 
    GdkPixbuf *prescale; 
    GdkPixbuf *postscale; 
    unsigned int width, highth = 0; 
    gtk_init(&argc, &argv); 
       
    /*printf("Number of agurments is %i\n",argc); */
    
        	       
	
    prescale = gdk_pixbuf_new_from_file(argv[1],&err);  
    if (err != NULL) { 
        fprintf(stderr,"ERROR: %s\n",err->message);  
            g_error_free (err); 
            return 1;
	}
    memset(buffer,'\0',MAX_NAME_SIZE);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 
    gtk_window_set_resizable(GTK_WINDOW(window),1);  
    width = gdk_pixbuf_get_width (prescale);
    highth = gdk_pixbuf_get_height (prescale);
    processbarmessage(argv[1],width,highth,buffer);
  
 
    setsize(&width,&highth);         
            

     gtk_window_set_default_size(GTK_WINDOW(window), DEFAULT_WIDTH, DEFAULT_HIGHTH); 
/* The below code will check to see if a valid image is loaded 
   however the error is displayed in the standar error not in 
   the window manager this needs fixing in the future so errors 
   are shown properly. This is also why there is a return here 
   in the future I am hoping to use a dialog */    
   

     if ( err != NULL ) 
          { 
            fprintf(stderr,"ERROR: %s\n",err->message);  
            g_error_free (err); 
            return 1; 
          }

     else 
          {
              /* Scale our image to the defined limits */ 

              postscale = gdk_pixbuf_scale_simple(prescale,
                        width, highth, GDK_INTERP_BILINEAR); 
              
               image = gtk_image_new_from_pixbuf(postscale); 
               gtk_container_add(GTK_CONTAINER(window), image); 
  	       gtk_window_set_title(GTK_WINDOW(window),buffer);


   	  } 

    

    g_signal_connect_swapped(G_OBJECT(window), "destroy",
          G_CALLBACK(gtk_main_quit), G_OBJECT(window)); 

    gtk_widget_show_all(window); 
   
    gtk_main(); 
    


return 0; 
}

Last edited by exvor; 09-12-2010 at 03:21 AM.
 
Old 09-13-2010, 06:55 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
The problem is in processmessagebar(), specifically:
Code:
        location = strrchr(filename,'/');
If there is no '/' in filename strrchr will return NULL.

When you get a segfault, using a debugger (like gdb) can help find the problem, but you need to compile with -g so I fixed up your Makefile:
Code:
CFLAGS := -g -Wall -Wextra $(shell pkg-config --cflags gtk+-2.0)
LDFLAGS := $(shell pkg-config --libs gtk+-2.0)

gimageviewer : main.o functions.o
	$(LINK.o) $(LDFLAGS) $^ -o $@
main.o : main.c main.h functions.h
functions.o : functions.c functions.h main.h

clean :
	rm main.o gimageviewer functions.o
install:
	cp gimageviewer /usr/bin

uninstall:
	rm /usr/bin/gimageviewer
 
Old 09-13-2010, 09:32 PM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
The whole reason I created the function was to deal with having the whole location of the file returned to the title of the window. Seams strange why one system would return the whole file name and another one would not.

Ohh well thank you for tracking down the problem I really do aprciate it, and for making the make file a little more sane. I really should learn a bit more about making proper make files.
 
Old 09-13-2010, 09:46 PM   #4
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
I fixed it by adding in a null check. I am not fully happy with the solution that i devised and there is still some dirty hacks to work out but at least its working for now
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
java applet not loading image with relative path but loads image with absolute path amolgupta Programming 2 07-20-2009 01:58 PM
Xterminal + current GTK = segfault crxssi Linux - Software 3 01-28-2006 06:58 PM
GTK segfault. Possible dependencies missing? apachedude SUSE / openSUSE 1 08-08-2005 03:23 PM
Setting X boot image, and loading image untwisted Linux - General 2 03-09-2004 07:40 PM
random image with gtk chirita Programming 1 08-29-2003 08:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration