unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* how to correctly  define using scm_define ?
@ 2004-11-02 15:54 Tano Fotang
  2004-11-02 16:18 ` Marius Vollmer
  0 siblings, 1 reply; 3+ messages in thread
From: Tano Fotang @ 2004-11-02 15:54 UTC (permalink / raw)


I am trying to define some variable (libguile 1.7.1)  but it's going the 
wrong way all day:

SCM tt;
void set_tt(SCM proc){ //called often to set tt
  assert(scm_is_true(scm_procedure_p((proc)))); //passes
  scm_define(tt, proc);
  assert(scm_is_true(scm_procedure_p((tt)))); //fails
}

So I decide to skip scm_define and just do:

    tt = proc;

That appears okay until  the following line is seen some minutes later:

    scm_apply_0(tt, args)

then this error is displayed:

wrong-type-arg: [apply] Wrong type argument in position 1: #<freed cell 
0x406303b0; GC missed a reference>

I'd be glad for any pointers on how to proceed.
/fotang



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: how to correctly  define using scm_define ?
  2004-11-02 15:54 how to correctly define using scm_define ? Tano Fotang
@ 2004-11-02 16:18 ` Marius Vollmer
  0 siblings, 0 replies; 3+ messages in thread
From: Marius Vollmer @ 2004-11-02 16:18 UTC (permalink / raw)
  Cc: guile-user

Tano Fotang <fotang@yahoo.com> writes:

> So I decide to skip scm_define and just do:
>
>     tt = proc;

This is the correct way.  scm_define is for defining new global Scheme
variables.  See

    http://www-dt.e-technik.uni-dortmund.de/~mvo/guile.html/Accessing-Modules-from-C.html

> That appears okay until  the following line is seen some minutes later:
>
>     scm_apply_0(tt, args)
>
> then this error is displayed:
>
> wrong-type-arg: [apply] Wrong type argument in position 1: #<freed
> cell 0x406303b0; GC missed a reference>
>
> I'd be glad for any pointers on how to proceed.

You need to 'protect' the SCM value stored into a global C variable.
Otherwise, the garbage collector might not know that it is in use and
will free that SCM value.  See

    http://www-dt.e-technik.uni-dortmund.de/~mvo/guile.html/Garbage-Collection-Functions.html

and more generally

    http://www-dt.e-technik.uni-dortmund.de/~mvo/guile.html/Garbage-Collection.html

The functions scm_gc_protect and scm_gc_unprotect are likely what you
want:

    SCM tt;

    void
    set_tt (SCM proc)
    {
      scm_gc_unprotect (tt);
      tt = proc;
      scm_gc_protect (tt);
    }

There is also scm_gc_register_root, but it is not documented yet...


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: how to correctly  define using scm_define ?
@ 2004-11-02 17:03 Tano Fotang
  0 siblings, 0 replies; 3+ messages in thread
From: Tano Fotang @ 2004-11-02 17:03 UTC (permalink / raw)


Thanks. That was the magic (scm_gc_(un)protect).

--- Marius Vollmer <marius.vollmer@uni-dortmund.de> wrote:
...
 > You need to 'protect' the SCM value stored into a
 > global C variable.
 > Otherwise, the garbage collector might not know that
 > it is in use and
 > will free that SCM value.  See
http://www-dt.e-technik.uni-dortmund.de/~mvo/guile.html/Garbage-Collection-Functions.html
...


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-11-02 17:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-11-02 15:54 how to correctly define using scm_define ? Tano Fotang
2004-11-02 16:18 ` Marius Vollmer
  -- strict thread matches above, loose matches on Subject: below --
2004-11-02 17:03 Tano Fotang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).