Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
GC with C issues
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-02-03 (10:50)
From: ronniec95@l...
Subject: GC with C issues
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

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

/*Allocate a new object*/
CAMLprim value api_newObject(value aString)
	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);
	return retVal;
/* Print out the value */
CAMLprim value api_getValue(value inVal)
	someObject* x = NULL;
	x = (someObject*)Field(inVal,0);
	fprintf(stderr,"getValue called:%p",x);
	return copy_string(x->foo);
(* Ocaml test file *)
let _ =
    let x = Api.newObject "testString" (* New allocation everytime - leaks!*)
    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,