Active Topics

 


Reply
Thread Tools
jaeezzy's Avatar
Posts: 664 | Thanked: 160 times | Joined on Jul 2008 @ Australia
#1
Hi,
I'm getting segmentation fault randomly in my desktop app for fremantle and I just couldn't figure out what's causing it and the exact source code. I'm using gtk+, gdb and maemo-summoner in scratchbox. Following is the error message and the backtrace from gdb: Thanks

Code:
Program received signal SIGSEGV, Segmentation fault.
0xb7ef1db9 in malloc_consolidate () from /lib/libc.so.6
0xb7ef1db9 <malloc_consolidate+137>:	cmp    %edi,0xc(%eax)
(gdb) hildon-desktop[4460]: GLIB WARNING ** ClutterX11 - Failed to get XImage of pixmap: e01040, removing
Quit
(gdb) bt
#0  0xb7ef1db9 in malloc_consolidate () from /lib/libc.so.6
#1  0xb7ef3b4d in _int_malloc () from /lib/libc.so.6
#2  0xb7ef5499 in malloc () from /lib/libc.so.6
#3  0xb7739543 in *INT_cairo_create (target=0x8224800) at cairo.c:154
#4  0xb780b756 in setup_backing_rect_method (method=0xbfffecb4, window=0x806f648, paint=0x4f54535f, x_offset_cairo=0, y_offset_cairo=0)
    at gdkwindow.c:2028
#5  0xb780b891 in gdk_window_clear_backing_rect (window=0x806f648, x=0, y=0, width=800, height=47) at gdkwindow.c:2058
#6  0xb780e07e in IA__gdk_window_begin_paint_region (window=0x806f648, region=0x82245c0) at gdkwindow.c:1153
#7  0xb7972ffe in IA__gtk_main_do_event (event=0xbfffed90) at gtkmain.c:1552
#8  0xb780e42d in gdk_window_process_updates_internal (window=0x806f648) at gdkwindow.c:2599
#9  0xb780eaf0 in IA__gdk_window_process_all_updates () at gdkwindow.c:2665
#10 0xb780eb1b in gdk_window_update_idle (data=0x0) at gdkwindow.c:2509
#11 0xb77f38ac in gdk_threads_dispatch (data=0x81d6150) at gdk.c:473
#12 0xb7d04223 in g_idle_dispatch (source=0x821d5f0, callback=0x5353414d, user_data=0x81d6150)
    at /home/bifh4/fremantle-i386-fremantle1.2.cs2007q3/work/glib2.0-2.20.3/glib/gmain.c:3966
#13 0xb7d05fbc in g_main_context_dispatch (context=0x80708a8)
    at /home/bifh4/fremantle-i386-fremantle1.2.cs2007q3/work/glib2.0-2.20.3/glib/gmain.c:1836
#14 0xb7d09595 in g_main_context_iterate (context=0x80708a8, block=1, dispatch=1, self=0x8055ac0)
    at /home/bifh4/fremantle-i386-fremantle1.2.cs2007q3/work/glib2.0-2.20.3/glib/gmain.c:2467
#15 0xb7d09888 in g_main_loop_run (loop=0x81890f0)
    at /home/bifh4/fremantle-i386-fremantle1.2.cs2007q3/work/glib2.0-2.20.3/glib/gmain.c:2675
#16 0xb7973289 in IA__gtk_main () at gtkmain.c:1200
#17 0xb7cb8d06 in main () from /usr/bin/hildon-home.launch
---Type <return> to continue, or q <return> to quit---
 
jaeezzy's Avatar
Posts: 664 | Thanked: 160 times | Joined on Jul 2008 @ Australia
#2
Ok, maybe someone can pin-point why I'm getting segmentation fault on this code, rather than the ugly output above :

Code:
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

char* getUsbMode(void);
gboolean check(gchar*);

int main(){
	gchar *dir = "scratchbox/users/user/home/user/photoapplet/200910a0";
	GMainLoop *loop = NULL;
	loop = g_main_loop_new(NULL, FALSE);
	g_timeout_add_seconds(3, (GSourceFunc)check, dir);
	
	g_main_loop_run(loop);
	
	return 0;
}

gboolean check(gchar *dir){
	if (g_file_test(dir, G_FILE_TEST_EXISTS)){
		printf("Can access %s\n", dir);
	}else{
		printf("Can't access directory.\n");
		char *usb_mode = NULL;
		usb_mode = getUsbMode();
		if (strcmp(usb_mode, "MASS_STORAGE") == 0){
			printf("USB mass storage mode activated.\n");
		}
	}
	return TRUE;
}

char* getUsbMode(){
	char *file = "/tmp/.current_usb_mode";
	char *mode, val[15];
	mode = (char *) malloc(sizeof(char*));
	assert(mode != NULL);
	FILE *fPtr;
	if ((fPtr = fopen(file, "r")) != NULL){
		fgets(val, 15, fPtr);
		val[strlen(val) - 2] = '\0';
		strcpy(mode, val);
		printf("%s\n", mode);
		fclose(fPtr);
		return mode;
	}else
		return "READ_FAILED";
}
thanks..

Last edited by jaeezzy; 2010-06-13 at 08:58.
 
Posts: 726 | Thanked: 345 times | Joined on Apr 2010 @ Sweden
#3
Code:
mode = (char *) malloc(sizeof(char*));
What are you allocating here?
 

The Following User Says Thank You to Joorin For This Useful Post:
jaeezzy's Avatar
Posts: 664 | Thanked: 160 times | Joined on Jul 2008 @ Australia
#4
Originally Posted by Joorin View Post
Code:
mode = (char *) malloc(sizeof(char*));
What are you allocating here?
memory to store string which is read from the file and stored in array of char.

EDIT: I can't believe it, I thought it was needed but it was the source of problem damn me.... THANK YOU.

Last edited by jaeezzy; 2010-06-13 at 09:18.
 
qwerty12's Avatar
Posts: 4,274 | Thanked: 5,358 times | Joined on Sep 2007 @ Looking at y'all and sighing
#5
Originally Posted by jaeezzy View Post
memory to store string which is read from the file and stored in array of char.
Since you're using GLib, here's the cheat way: http://maemo.org/api_refs/5.0/5.0-fi...e-get-contents
 

The Following User Says Thank You to qwerty12 For This Useful Post:
jaeezzy's Avatar
Posts: 664 | Thanked: 160 times | Joined on Jul 2008 @ Australia
#6
Ok guys, I'm confused again. I did without malloc() and worked fine in scratchbox(cmdline) and in the device(cmdline). So, thinking it will work, I implemented the same in my real app(Desktop App) but it failed, so I put all the print statements( I'm tooo lazy to use gdb unless its completely random) to see where exactly the seg fault is comming from then I found the line strcpy(mode, val) is the source coz the printf statement just before that gets executed and not the one immediately after . What do youse have to say about it? Thanks

BTW, I got over it using qwerty's suggestion but just curious to know why it didn't happen that other way....
 
Posts: 726 | Thanked: 345 times | Joined on Apr 2010 @ Sweden
#7
Originally Posted by jaeezzy View Post
memory to store string which is read from the file and stored in array of char.

EDIT: I can't believe it, I thought it was needed but it was the source of problem damn me.... THANK YOU.
No. You allocate enough memory to store one pointer to a char. Read the line again and think about what sizeof actually does.

If you're still in doubt, use
Code:
printf("%d\n", sizeof(char *));
to understand more.

sizeof(char *) will return the size of a memory pointer, usually an unsigned int, 4 bytes. This is not what you want to allocate if you are to store something in the allocated memory.
 
Reply


 
Forum Jump


All times are GMT. The time now is 08:50.