From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id C7033BC88 for ; Thu, 3 Feb 2005 11:50:33 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j13AoXmg007546 for ; Thu, 3 Feb 2005 11:50:33 +0100 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id LAA31289 for ; Thu, 3 Feb 2005 11:50:32 +0100 (MET) Received: from cradle.home.net (chowdhury95.demon.co.uk [80.177.9.225]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j13AoWjp007541 for ; Thu, 3 Feb 2005 11:50:32 +0100 Received: from ucacruc by cradle.home.net with local (Exim 4.34) id 1Cwegs-0000Qz-Dw for caml-list@inria.fr; Thu, 03 Feb 2005 10:58:26 +0000 Date: Thu, 3 Feb 2005 10:58:26 +0000 From: ronniec95@lineone.net To: caml-list@inria.fr Subject: GC with C issues Message-ID: <20050203105826.GA1669@cradle> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.4.2i Sender: X-Miltered: at concorde with ID 420201F9.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 420201F8.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; lineone:01 api:01 typedef:01 struct:01 char:01 foo:01 camlprim:01 api:01 camlparam:01 malloc:01 sizeof:01 foo:01 char:01 malloc:01 sizeof:01 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.7 required=5.0 tests=FORGED_RCVD_HELO, FROM_ENDS_IN_NUMS,NO_REAL_NAME autolearn=disabled version=3.0.2 X-Spam-Level: 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