unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Mike Gran <spk121@yahoo.com>
To: "Ludovic Courtès" <ludo@gnu.org>,
	"guile-user@gnu.org" <guile-user@gnu.org>
Subject: Re: Proper way of making a single-pointer smob
Date: Thu, 30 May 2013 00:10:28 -0700 (PDT)	[thread overview]
Message-ID: <1369897828.49673.YahooMailNeo@web120403.mail.ne1.yahoo.com> (raw)
In-Reply-To: <87ppw95u0k.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 1084 bytes --]

> Mike Gran <spk121@yahoo.com> skribis:


>> 3. Some versions of Guile have a bug where the last smob defined
>> in your program is never freed, even if you've cleared out the
>> pointers and called scm_gc.

> What is this?  I’d be happy to read more details.  :-)

To be more specific, if you want to make sure the GC frees every smob,
you should call scm_gc before the program ends.

But, if you place the scm_gc call inside of a function
designated by C's atexit() function, instead of, say, at the end of 
main(), it may not free all the SMOBs.  I never investigated why.
I assumed it was a race.

So, it is probably not really a Guile bug, but, rather, that calling
scm_gc from within atexit() is a bad idea.

The program below gives me the following output in Guile 2.0.9

Allocating #1
Allocating #2
Allocating #3
Allocating #4
Allocating #5
Allocating #6
Allocating #7
Allocating #8
Allocating #9
Allocating #10
Freeing #5
Freeing #6
Freeing #3
Freeing #4
Freeing #2
Freeing #1
gc at exit
Freeing #9
Freeing #7
Freeing #8

-Mike

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: tmp.cc --]
[-- Type: text/x-c++src; name="tmp.cc", Size: 955 bytes --]

#include <libguile.h>
#include <stdio.h>

#define SIZE (1000000 * sizeof(int))
static scm_t_bits model_smob_t;

static size_t free_model(SCM smob)
{
    int* ptr = (int *)SCM_SMOB_DATA(smob);
    printf("Freeing #%d\n", *ptr);
    fflush(stdout);
    scm_gc_free (ptr, SIZE, "int array");
    ptr = NULL;
    return 0;
}

void func(void)
{
    printf("gc at exit\n");
    fflush(stdout);
    scm_gc();
}

int main (int argc, char ** argv)
{
    SCM s;

    atexit(func);
    scm_init_guile();
    
    model_smob_t = scm_make_smob_type("model", sizeof(int *));
    scm_set_smob_free(model_smob_t, free_model);
    for (int i = 1; i <= 10; i ++)
    {
        printf("Allocating #%d\n", i);
        int *ptr = (int *) scm_gc_malloc (SIZE, "int array");
        *ptr = i;
        SCM_NEWSMOB(s, model_smob_t, (scm_t_bits) ptr);
        s = SCM_BOOL_F;
    }
    
    // printf("gc at end of main\n");
    // fflush(stdout);
    //scm_gc();
    return 0;
}


  reply	other threads:[~2013-05-30  7:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-29  6:16 Proper way of making a single-pointer smob Sun Yijiang
2013-05-29 13:48 ` Panicz Maciej Godek
2013-05-29 14:02 ` Mike Gran
2013-05-29 21:57   ` Ludovic Courtès
2013-05-30  7:10     ` Mike Gran [this message]
2013-05-30 11:56       ` Ludovic Courtès
2013-05-31  3:08   ` Sun Yijiang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1369897828.49673.YahooMailNeo@web120403.mail.ne1.yahoo.com \
    --to=spk121@yahoo.com \
    --cc=guile-user@gnu.org \
    --cc=ludo@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).