Re: Memory question



You aren't unreffing the gdkbitmap and gdkpixmaps that you create

use gdk_bitmap_unref(bitmap)
and gdk_pixmap_unref(pixmap)

Maher

On Fri, 17 Aug 2001 21:59:27 +0800, Jonne Zutt said:

> Recently, I installed MemProf, because I had a 
>  memory leak
>  in my first gtk program. I was surprised by the fact 
>  that it
>  is real easy to use and very effective.
>  In no-time I found a bug in my program that resulted 
>  in the
>  memory leak.
>  MemProf does not find leaks in my program anymore.
>  However, I still have a problem.
>  If I run 'top', I see that the memory used by X
>  is still increasing until my computer is going to
>  swap so badly, that I have to reset it...
>  Even after my program is quitted, the memory is
>  not released.
>  
>  I think, this is because I use gtk in a terribly 
>  wrong
>  way. The following listing is a function that I call
>  after each legal mouse click in the game and after
>  each move of the engine.
>  I created a new fixed layout, create new cards,
>  scoreboard, player labels and add them to the new
>  layout.
>  Then, at the end of the function, I destroy the
>  old fixed layout and add the new one
>  (I do not destroy the widgets inside the old
>   container).
>  
>  Can anyone explain me what is going wrong?
>  Are the widgets in the old fixed layout not
>  destroyed automatically?
>  Should I store a list of these widgets and
>  destroy all of them individually?
>  
>  Thanks for any help,
>  Jonne.
>  
>  ----
>  /* Global variables */
>  
>  GtkWidget *window, *pixmapwid, *fixed,
>            *lab_p1, *lab_p2, *lab_p3, *lab_p4,
>            *menubar, *main_vbox;
>  GdkPixmap *pixmap;
>  GdkBitmap *mask;
>  GtkStyle *style;
>  
>  /* Function draw_table */
>  
>  void draw_table(Scoreboard *sb) {
>    GtkWidget *new_fixed;
>    char **rot;
>    int i;
>    int suit, card;
>    static int firsttime = 1;
>    static gchar *st_titles[]  = { "Round",
>                                   "Computer 1",
>  	     		         "Computer 2",
>         			         "Computer 3",
>  			         "Player 1" };
>  
>    new_fixed = gtk_fixed_new();
>    
>    lab_p1 = gtk_label_new(sb->get_player_label(0));
>    gtk_widget_set_usize(lab_p1,CARD_HEIGHT + 
>  2*BORDER_GAP,BORDER_GAP);
>    lab_p2 = gtk_label_new(sb->get_player_label(1));
>    
>  gtk_widget_set_usize(lab_p2,7*INNER_GAP+CARD_WIDTH,BORDER_GAP);
>    lab_p3 = gtk_label_new(sb->get_player_label(2));
>    gtk_widget_set_usize(lab_p3,CARD_HEIGHT + 
>  2*BORDER_GAP,BORDER_GAP);
>    lab_p4 = gtk_label_new(sb->get_player_label(3));
>    
>  gtk_widget_set_usize(lab_p4,7*INNER_GAP+CARD_WIDTH,BORDER_GAP);
>    
>    /* Player 1 */
>    gtk_fixed_put(GTK_FIXED(new_fixed), lab_p1, 
>  PLAYER_1_NX, PLAYER_1_NY);
>    gtk_widget_show(lab_p1);
>    for(i=0; i<8; i++) {
>      if(sb->player[0].cards[i] == 0)
>        continue;
>      suit = SUIT_OF(sb->player[0].cards[i]);
>      card = CARD_OF(sb->player[0].cards[i]);
>      rot = rotate(CARD_XPM(suit,card), 90);
>      pixmap = 
>  gdk_pixmap_create_from_xpm_d(window->window, &mask,
>                           
>  &style->bg[GTK_STATE_NORMAL], rot);
>      pixmapwid = gtk_pixmap_new(pixmap, mask);
>      gtk_fixed_put(GTK_FIXED(new_fixed), pixmapwid, 
>  PLAYER_1_X, PLAYER_1_Y + INNER_GAP*i);
>      gtk_widget_show(pixmapwid);
>      free_xpm_str(rot);
>    }
>    
>    /* Player 2 */
>    ...
>    
>    /* Player 3 */
>    ...
>    
>    /* Player 4 */
>    ...
>    
>    /* Score table */
>    GtkWidget *score_table = 
>  gtk_clist_new_with_titles(NO_PLAYERS+1, st_titles);
>    for(i=0; i<sb->round_id+1; i++)
>      gtk_clist_append((GtkCList *)score_table, 
>  sb->get_score_line(i));
>    gtk_fixed_put(GTK_FIXED(new_fixed), score_table, 
>  SCORE_TABLE_X, SCORE_TABLE_Y);
>    gtk_widget_show(score_table);
>   
>    /* Draw cards on table */
>    for(i=sb->who_started(); 
>  i<sb->who_started()+NO_PLAYERS; i++)
>      
>  draw_move(new_fixed,i%NO_PLAYERS,sb->player[i%NO_PLAYERS].card_on_table);
>  
>    /* Finally, show it all */
>    if(!firsttime) {
>      //gtk_container_remove(GTK_CONTAINER(main_vbox), 
>  fixed);
>      gtk_widget_destroy(fixed);
>    }
>  
>    fixed = new_fixed;
>    gtk_container_add(GTK_CONTAINER(main_vbox), fixed);
>    gtk_widget_show(fixed);
>    
>    if(firsttime)
>      firsttime = 0;
>  }
>  
>  
>  
>  -- 
>  
>  _______________________________________________
>  FREE Personalized E-mail at Mail.com 
>  http://www.mail.com/?sr=signup 
>  
>  Talk More, Pay Less with Net2Phone Direct(R), up to 1500 minutes free! 
>  http://www.net2phone.com/cgi-bin/link.cgi?143 
>  
>  
>  _______________________________________________
>  gtk-list mailing list
>  gtk-list gnome org
>  http://mail.gnome.org/mailman/listinfo/gtk-list
>  
>  
>  

-- 
http://www.muhri.net -- muhri muhri net





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