caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: ronniec95@lineone.net
To: caml-list@inria.fr
Subject: GC with C issues
Date: Thu, 3 Feb 2005 10:58:26 +0000	[thread overview]
Message-ID: <20050203105826.GA1669@cradle> (raw)

Hello all,

I'm looking for some help with the following really short example when
I'm trying to reference a C allocated object from Caml (which works
fine), but the GC seems to not like it. Here's the sample code - pretty
short...


/* File: api.c */
typedef struct someObject_
{
	char* foo;
} someObject;

/*Allocate a new object*/
CAMLprim value api_newObject(value aString)
{
	CAMLparam1(aString);
	CAMLlocal1(retVal);
	someObject* x = (someObject*) malloc(1 * sizeof(someObject));
	x->foo = (char*) malloc(256 * sizeof(char));
	strcpy(x->foo, String_val(aString));
	retVal = alloc(1,Abstract_tag);
	Store_field(retVal,0,(value)x);
	fprintf(stderr,"New:%p\n",x);
	return retVal;
}
/* Print out the value */
CAMLprim value api_getValue(value inVal)
{
	CAMLparam1(inVal);
	someObject* x = NULL;
	
	x = (someObject*)Field(inVal,0);
	fprintf(stderr,"getValue called:%p",x);
	
	return copy_string(x->foo);
}
------
(* Ocaml test file main.ml *)
let _ =
    let x = Api.newObject "testString" (* New allocation everytime - leaks!*)
    in
    print_endline (Api.getValue x); (* This works fine *)
    Gc.full_major () (* This causes a segfault *)


I can't see what I'm doing wrong in the allocation? I expect this to
leak because I'm not freeing the allocated 'someObject' but I don't
expect it to segfault though?

Any help appreciated,

Ronnie


             reply	other threads:[~2005-02-03 10:50 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-02-03 10:58 ronniec95 [this message]
2005-02-03 11:06 ` [Caml-list] " Daniel Bünzli
2005-02-03 11:25   ` ronniec95
2005-02-03 11:16 ` Olivier Andrieu
2005-02-03 11:31   ` ronniec95
2005-02-03 12:50     ` Olivier Andrieu
2005-02-03 12:52     ` Damien Doligez

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

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

  git send-email \
    --in-reply-to=20050203105826.GA1669@cradle \
    --to=ronniec95@lineone.net \
    --cc=caml-list@inria.fr \
    /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 NNTP newsgroup(s).