[airstrike] Re: Refcounting?

Ulf Ekström uekstrom at gmail.com
Thu Dec 30 17:46:50 EST 2004


> 
> So, should it be like this:
> ---------
> sprite_type_t bonus = {
>         "bonus",
>         &bullet_group,
>         bonus_setup,
>         bonus_create,
>         bonus_free,
> ....
>         case MSG_SET_TARGET:
>                 /* get pointer to bonus ring */

I would add, in case this comes twice:
                   if (bs->ring)
                       deref(bs->ring);

>                 bs->ring = ref(msg_get_target(msg));
>                 break;
> ...
>         case MSG_KILL:
>                 /* bonus expired */
>                 if (refcheck(bs->ring)) {
>                         /* remove bonus ring */
>                         sprite_msg(bs->ring, msg_kill());
>                 }
>                 /* remove bonus */
>                 refkill(s);
>                 break;
> ...

Yes.

> static void bonus_free(void *s)
> {
>         bonus_sprite_t *bs = (bonus_sprite_t *)s;
>         if (bs->ring) {
>                 deref(bs->ring);
>         }
>         free(s);
> }

Yes.

> --------
> ?
> 
> bs->ring will not be deref'd anywhere else except in bonus_free()
> (SET_TARGET message should come only once), 

Ok, then you may forget about my comment above. Also you should check the 
target once in a while with refcheck to see if it's still alive. 

> I'm just wondering whether I
> still need to do free(s) in bonus_free() function...?

Yes, you do. There is only one function which is called at refcount =
0, and it's
(int this case) bonus_free.


Ulf



More information about the airstrike mailing list