guile-user
[Top][All Lists]
Advanced

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

Re: behaviour of scm_run_hook


From: Neil Jerram
Subject: Re: behaviour of scm_run_hook
Date: Sat, 03 Sep 2005 20:53:31 +0100
User-agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux)

address@hidden writes:

> ok,  the code is quite long, so I'll try to isolate the parts that use
> scheme hooks.

Thanks.

>  Here is the definition of the structure which houses a
> hook:
>
> typedef struct _menu_item_s
> {
>   menu_item_type_t type;
>   char* title;
>   char* name;
>   int mouse_state;
>   int item_width;
>   SDL_Surface* icon;//
>   void (*action)(menu_t* menu, struct _menu_item_s* item);
>   SCM guile_action_hook;
>   menu_t* sub_menu;
> }menu_item_t;
>
> and it is initialized with this function :
>
> menu_item_t* new_menu_item(const char* title, const char* name)
> {
>   menu_item_t* new_item;
>   static unsigned int item_number = 0;
>   char default_name[16];
>
>   new_item = (menu_item_t*)malloc(sizeof(menu_item_t));
>
>   new_item->type = MENU_ITEM;
>   new_item->mouse_state = MOUSE_OUT;
>   new_item->icon = NULL;
>
>   if(name == NULL)
>     {
>       sprintf(default_name,"item%u", item_number++);
>       new_item->name = strdup(default_name);
>     }
>   else
>     {
>       new_item->name = strdup(name);
>     }
>
>   ////XXX
>   new_item->guile_action_hook = scm_make_hook(SCM_MAKINUM(0));//SCM_BOOL_F;
>
>   new_item->sub_menu = NULL;
>   new_item->title = title ? strdup(title) : NULL;
>
>   return new_item;
> }

I wonder if the problem is that the hook is being GC'd?  There's
nothing in this code that would protect the hook from that.

All you need to do to fix this is call

  scm_gc_protect_object(new_item->guile_action_hook)

when allocating the menu item (after the scm_make_hook line), and

  scm_gc_unprotect_object(menu_item->guile_action_hook)

when freeing menu_item.

Please let us know if this fixes the problem.

       Neil





reply via email to

[Prev in Thread] Current Thread [Next in Thread]