Menu

Main Menu
Talk Get Daily Search

Member's Online

    User Name
    Password

    [solved] Problems with crashes in C-Code

    Reply
    electroaudio | # 1 | 2012-04-14, 13:51 | Report

    I dont understand this error, but i dont understand C so well either...
    When the code in pink is commented out, then the routine seem to work very well, but when the part in pink is part of the compilation, then the program will crash (sort of..) randomly with error sigsegv-11

    Code:
    GdkPixbuf *get_pixbuf_file (gchar *fullpath)
    {   
    
        GdkPixbuf *pixbuf=NULL; 
    	struct stat statbuf;
    	int counter=0;
    	char mychar;
    	char xxx[50];
    	char *mime_icon_name=NULL;
    	gchar *mime_icon_file=NULL;
    	gchar *test;
    	gchar *file_open;
    	GtkIconTheme *icon_theme;
    	char *ext = strrchr(fullpath, '.');
    
    	if (ext==NULL)
    		...
    	if (strstr(ext,".desktop") && strstr(".desktop",ext))
    		...
    	if (strstr(fullpath, "/folder.png") && (fast_mode==0))
    		...
    	mime_icon_name=NULL;
    	if ((mime_icon_name=strrchr(fullpath, '.'))==0)
    		..
                 if ((statbuf.st_mode &S_IXUSR))
                   ...
    
    
    	memset (xxx,0,sizeof(xxx));
    	  for ( counter = 0 ;  (counter<= (sizeof(xxx)-0)) && (mime_icon_name[counter]!=0) ; counter++ ){
    		  mychar=mime_icon_name[counter];
    		 xxx[counter]=(tolower(mychar));
    		} 
    
    
    	mime_icon_file=g_strconcat ("/home/user/.deskypplet/icons/",xxx+1,".png",NULL);
    
    	if ((pixbuf=gdk_pixbuf_new_from_file_at_size(mime_icon_file,app->icon_x_size,app->icon_y_size,NULL))!=0)
    		{
    		 return pixbuf;
    		}
    	else
    		{
    		 pixbuf= gdk_pixbuf_new_from_file_at_size("/home/user/.deskypplet/icons/dummy.png",app->icon_x_size,app->icon_y_size,NULL);
    		 return pixbuf;
    		}		
    }
    Ps. I have deleted code that i dont believe has anything to do with it and replaced it with "..." to make it more readable

    Edit | Forward | Quote | Quick Reply | Thanks

    Last edited by electroaudio; 2012-04-14 at 23:56.

     
    daperl | # 2 | 2012-04-14, 14:18 | Report

    Maybe change

    Code:
    counter<=
    to

    Code:
    counter<

    Edit | Forward | Quote | Quick Reply | Thanks

     
    daperl | # 3 | 2012-04-14, 14:29 | Report

    xxx must be NULL terminated for the line after the pink code to not randomly crash. The pink code doesn't guarantee that. So, you might actually want to change:

    Code:
    counter<= (sizeof(xxx)-0
    to

    Code:
    counter< (sizeof(xxx)-1

    Edit | Forward | Quote | Quick Reply | Thanks

     
    electroaudio | # 4 | 2012-04-14, 16:10 | Report

    Originally Posted by daperl View Post
    xxx must be NULL terminated for the line after the pink code to not randomly crash. The pink code doesn't guarantee that. So, you might actually want to change:

    Code:
    counter<= (sizeof(xxx)-0
    to

    Code:
    counter< (sizeof(xxx)-1
    First time i have tried to compile over a 3G network from a cafe
    Unfourtunately that didnt help.

    But what about the definitions of the string? is it a guarantee that a declared pointer has any space to hold a string?
    Like the mime_icon_name , how can it have any allocated space to it to hold the 50 chars?

    Edit | Forward | Quote | Quick Reply | Thanks

    Last edited by electroaudio; 2012-04-14 at 16:13.

     
    daperl | # 5 | 2012-04-14, 18:28 | Report

    Yes, exactly, define it the same as xxx. That should get you to point B. It's not very well written.

    So, instead of:

    Code:
    char *mime_icon_name=NULL;
    make it

    Code:
    char mime_icon_name[50];
    There are possibly other memory problems, but start there.

    Edit | Forward | Quote | Quick Reply | Thanks

     
    electroaudio | # 6 | 2012-04-14, 19:21 | Report

    That feels much better, but now i get a lot of "incompatible types in assignment" errors from the compiler when the string is used instead.

    Is it possible to rewrite the functions below to work again with this stringdefinition?

    Code:
    mime_icon_name=NULL;
    if ((mime_icon_name=strrchr(fullpath, '.'))==0)
    mime_icon_name=g_strconcat (".noext-exe","\0",NULL);
    mime_icon_name=g_strconcat (".noext","\0",NULL);
    It didnt complain about these lines, but do they still work as expected?

    Code:
    .. && (mime_icon_name[counter]!=0) 
    mychar=mime_icon_name[counter];
    xxx[counter]=(tolower(mychar));

    Edit | Forward | Quote | Quick Reply | Thanks

    Last edited by electroaudio; 2012-04-14 at 19:26.

     
    electroaudio | # 7 | 2012-04-14, 23:53 | Report

    Sorry, it should be a pointer as it was.
    It got its value from the parts that i had edited out.
    But after adding:
    Code:
     if (mime_icon_name!=NULL){ ...}
    Around it, it works perfectly.

    Now i only need to add a timer to prevent readdir() from getting stuck in /dev/ for example.

    Edit | Forward | Quote | Quick Reply | Thanks

    Last edited by electroaudio; 2012-04-14 at 23:55.
    The Following User Says Thank You to electroaudio For This Useful Post:
    Estel

     
    daperl | # 8 | 2012-04-15, 00:05 | Report

    That's what I figured.

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following User Says Thank You to daperl For This Useful Post:
    Estel

     
vBulletin® Version 3.8.8
Normal Logout