caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* GC with C issues
@ 2005-02-03 10:58 ronniec95
  2005-02-03 11:06 ` [Caml-list] " Daniel Bünzli
  2005-02-03 11:16 ` Olivier Andrieu
  0 siblings, 2 replies; 7+ messages in thread
From: ronniec95 @ 2005-02-03 10:58 UTC (permalink / raw)
  To: caml-list

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


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

end of thread, other threads:[~2005-02-03 12:52 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-02-03 10:58 GC with C issues ronniec95
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

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).