GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

free() invalid next size(fast)

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
ecntrk
Familiar Face


Joined: 25 Oct 2009
Posts: 13

PostPosted: Sat Jan 02, 2010 10:02 am    Post subject: free() invalid next size(fast) Reply with quote

Hello everybody,

I'm having a strange memory problem.

In a small code of mine, I've created a struct of some GtkWidgets and then typedefed it. I am reallocing the memory into the typedefed variable.
I'm freeing it in my main function.

Attaching my code here :

Code: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<gtk/gtk.h>
#include<stdlib.h>


/*building data structures */

GtkWidget *motherbox, *temp;

typedef struct core{
GtkWidget *img,*label,*dirname,*coreVbox,*eventbox,*align,*outerVbox;
} *_icon ;

_icon icon=NULL, icon_t;

int icon_no=0;
/****************************/


/*Prototypes **************************************/

//void filecreator(char *);
void show_dir(char *, char *);
void butn_prsd(GtkWidget *, GdkEventButton *, gpointer );
void set_init_space(void);

/***************************************************/




int main (int argc, char *argv[]){

    gtk_init(&argc,&argv);
    set_init_space();
    show_dir("/home/ecntrk","home");
    gtk_main();
    free(icon);

return 0;
}



void set_init_space(void){

GtkWidget *window, *scrlwindow;

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_size_request(GTK_WIDGET(window), 400,300);
    gtk_window_set_title(GTK_WINDOW(window), "File Manager 1.1");
    g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(gtk_main_quit), NULL);

    scrlwindow = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrlwindow), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

    motherbox = gtk_hbox_new(FALSE,0);
    gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrlwindow), motherbox);
    gtk_container_add(GTK_CONTAINER(window), scrlwindow);
   
    temp = motherbox;
    gtk_widget_show_all(window);

}



void show_dir(char *dname, char *labell){

icon = (_icon)realloc(icon, (++icon_no * sizeof(_icon)) );
icon_t=(_icon)(icon + icon_no -1);


/*creating core Vbox with icon image and label*/

    icon_t->coreVbox = gtk_vbox_new(FALSE,0);
    icon_t->label = gtk_label_new(labell);
    icon_t->img = gtk_image_new_from_file("/home/ecntrk/folder.png");
    icon_t->dirname = gtk_label_new(dname);//printf("tmp ptr %p  butn_n0 %d\n",&temp, butn_no);
   
/*Packing !**************/
    gtk_box_pack_start(GTK_BOX(icon_t->coreVbox), icon_t->img, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(icon_t->coreVbox), icon_t->label, FALSE, FALSE, 0);
   
/******************** ****************************/

/*eventbox *****************************************/
    icon_t->eventbox = gtk_event_box_new();//printf("tmp ptr %p  butn_n0 %d\n",&temp, butn_no);
    gtk_container_add(GTK_CONTAINER(icon_t->eventbox), icon_t->coreVbox);
    gtk_event_box_set_above_child(GTK_EVENT_BOX(icon_t->eventbox),TRUE);

    gtk_widget_set_events (icon_t->eventbox, GDK_BUTTON_PRESS_MASK);

    g_signal_connect (G_OBJECT (icon_t->eventbox), "button_press_event", G_CALLBACK (butn_prsd),(gpointer) icon_t->label);


/*****Alignment **************************************/
    icon_t->align = gtk_alignment_new(.5,.5,0,0);
    gtk_container_add(GTK_CONTAINER(icon_t->align), icon_t->eventbox);



    icon_t->outerVbox = gtk_vbox_new(FALSE,0);//printf("tmp ptr %p  butn_n0 %d\n",&temp, butn_no);
   
    gtk_box_pack_start(GTK_BOX(icon_t->outerVbox),icon_t->align,FALSE,FALSE,0);

/*Icon package complete ! */

    gtk_box_pack_start(GTK_BOX(temp),icon_t->outerVbox,FALSE,FALSE,0);

gtk_widget_show_all(icon_t->outerVbox);
}




void butn_prsd(GtkWidget *evntbx, GdkEventButton *event , gpointer labeltwo){


gchar *hu=(gchar *)gtk_label_get_text(GTK_LABEL(labeltwo));
g_print(" ** %s  **",hu);

}



First of all after execution, it says
Code: (Plaintext)
1
2
3
4
5
6
7
8
9
10
ecntrk@mithai-Dell:~$ ./expndr11

(expndr11:5387): GLib-GObject-CRITICAL **: g_object_new: assertion `G_TYPE_IS_OBJECT (object_type)' failed

(expndr11:5387): Gtk-CRITICAL **: gtk_box_pack: assertion `GTK_IS_BOX (box)' failed

(expndr11:5387): Gtk-CRITICAL **: gtk_box_pack: assertion `GTK_IS_WIDGET (child)' failed

(expndr11:5387): Gtk-CRITICAL **: gtk_widget_show_all: assertion `GTK_IS_WIDGET (widget)' failed


Here, the desired icon and the label is not appearing. (though it appeared in the first time i tried to run the program.) Only the blank window is there.

then upon closing it, gives the error
Code: (Plaintext)
1
2
3
4


*** glibc detected *** ./expndr11: free(): invalid next size (fast): 0x083f4cf0 ***

followed by a large memory backtrace.


Am I missing something there?

/* tadeboro, buddy, are you listning? :) */
Back to top
tadeboro
Never Seen the Sunlight


Joined: 23 Jul 2008
Posts: 2114
Location: Slovenia

PostPosted: Wed Jan 06, 2010 12:01 am    Post subject: Reply with quote

Hello.

The problem you're experiencing is not directly GTK+ related. I'll do my best to explain the problem, but you should consult your preferred C language literature for more details.

The problem starts at _icon type definition. You define _icon like this:
Code: (C)
1
2
3
4
typedef struct core
{
    /* Struct members here */
} *_icon;

To rephrase this in English: _icon is a pointer to a struct core.

This is all fine. But then we encounter line 72:
Code: (C)
1
icon = (_icon)realloc(icon, (++icon_no * sizeof(_icon)) );

And here things break, because you allocate insufficient amount of memory. And the culprit is the sizeof() operator. You want to allocate enough memory to hold another core struct, but what you're really doing is allocating just enough memory to hold pointer to a core struct. Proper way of memory allocation in this case would be:
Code: (C)
1
icon = (_icon)realloc(icon, (++icon_no * sizeof(struct core)) );


I'm not sure if I managed to explain this well enough, but hopefully, you'll manage to extract some information out of this post.

Tadej
Back to top
ecntrk
Familiar Face


Joined: 25 Oct 2009
Posts: 13

PostPosted: Wed Jan 06, 2010 8:32 am    Post subject: Reply with quote

Hey, Thanks for the nice help. That was an eye opener.
Btw, I was also trying this with (sizeof(struct core *)) but that also gives the problem...
I ran some trial and error through my code and after deleting the free(icon) , My code ran without a hitch !!!
After some time, I added some more functions to it ( I created a separate file for it) then tried to run it again. Alas.. the memory corruption happened again !
From that on, my original code that ran at first, is not running as well.

This has totally boggled my mind !

Dunno what's the reason .. !
Back to top
dreblen
Never Seen the Sunlight


Joined: 14 Jun 2007
Posts: 936
Location: Falun, WI USA

PostPosted: Thu Jan 07, 2010 4:06 am    Post subject: Reply with quote

I think that your problem is that you're still allocating the size of one pointer (in this case, sizeof(struct core *)).
This will allocate enough memory for one pointer (say, 4 bytes), but your struct has seven pointers and needs more memory (28 bytes).
To get the right amount of memory, you need to use sizeof(struct core) without the asterisk that's in your current code.
Back to top
ecntrk
Familiar Face


Joined: 25 Oct 2009
Posts: 13

PostPosted: Sat Jan 09, 2010 6:59 am    Post subject: Reply with quote

Hey thanks for your nice help :)

yeah, exactly,
I realised that and my problem's gone.

Thank you very much for your time and nice support.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP