I had a copy-paste error in the second function listing, it should be this:

SCM_DEFINE (libguile_fst_writer_emit_value, "libguile-fst-writer-emit-value",
                    3,0,0,
                    (SCM scm_ctx, SCM scm_fsthandle, SCM scm_val),
                    "Write a change on fstHandle")
{
  // Storage for temporary string
  char s_buf [1024];
  scm_to_locale_stringbuf (scm_val, s_buf, 1024);

  fstWriterEmitValueChange(SCM_TO_CTX (scm_ctx),
                                                   SCM_TO_FSTHANDLE 
(scm_fsthandle),
                                                   s_buf);

  return SCM_UNSPECIFIED;
}

-Brad

From: Whitlock, Bradley D
Sent: Tuesday, October 25, 2011 2:34 PM
To: 'guile-user@gnu.org'
Subject: Guile 1.8 Garbage Collection Question

I have built Guile 1.8.8.2 on MinGW.

I have wrapped some gtkwave code with Guile and have a memory issue. The 
following code apparently never frees 's', but I am not sure why.

SCM_DEFINE (libguile_fst_writer_emit_value, "libguile-fst-writer-emit-value",
                    3,0,0,
                    (SCM scm_ctx, SCM scm_fsthandle, SCM scm_val),
                    "Write a change on fstHandle")
{
  // Storage for temporary string
  char* s = NULL;
  scm_dynwind_begin (0);
  scm_dynwind_unwind_handler (free, s, SCM_F_WIND_EXPLICITLY);

  s = scm_to_locale_string (scm_val);

  fstWriterEmitValueChange(SCM_TO_CTX (scm_ctx),
                                                   SCM_TO_FSTHANDLE 
(scm_fsthandle),
                                                   s_buf);

  scm_dynwind_end();

  return SCM_UNSPECIFIED;
}

The code is called from Scheme land in the following loop:

(let loop ((n 0))
  (if (eq? n 10000000) #t
      (begin

                ;; Emit some changes on the signal
                (fst-emit-value ctx s (number->string (modulo n 2)))
                (fst-emit-value ctx s1 (number->string (modulo (1+ n) 2)))
                (fst-emit-time-change ctx n)

                (loop (1+ n))
                )))

It seems to me that the garbage collector never gets a chance to run, so how do 
I make sure that the gc gets a turn?

If I define my function like the following, the leak disappears, but it's a 
less desirable solution:

SCM_DEFINE (libguile_fst_writer_emit_value, "libguile-fst-writer-emit-value",
                    3,0,0,
                    (SCM scm_ctx, SCM scm_fsthandle, SCM scm_val),
                    "Write a change on fstHandle")
{
  // Storage for temporary string
  char s_buf [1024];
  scm_to_locale_stringbuf (scm_val, s_buf, 1024);
  s = scm_to_locale_string (scm_val);

  fstWriterEmitValueChange(SCM_TO_CTX (scm_ctx),
                                                   SCM_TO_FSTHANDLE 
(scm_fsthandle),
                                                   s_buf);

  return SCM_UNSPECIFIED;
}

Any suggestion?

Thanks,
Brad

Reply via email to