From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p7G84YNC007080 for ; Tue, 16 Aug 2011 10:04:34 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AjkEAHEjSk7RVdY2imdsb2JhbABBmSGPAggUAQEBCgkNBxIGIYFAAQEBAQMSAiwBGxwCAwwGBQsNCRYPCQMCAQIBEREBBQEcEwgCHodQm3cKjDeCVYUqO4htAgMGhkEEh1mLOYUMgSiGJTyDYw X-IronPort-AV: E=Sophos;i="4.67,379,1309730400"; d="scan'208";a="105562366" Received: from mail-bw0-f54.google.com ([209.85.214.54]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 16 Aug 2011 10:04:29 +0200 Received: by bkat8 with SMTP id t8so6984306bka.27 for ; Tue, 16 Aug 2011 01:04:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:x-enigmail-version:content-type :content-transfer-encoding; bh=QmEU7KUsnpBd0zXI0urkSlh7RDipoLsD8eS3Iy3W8X0=; b=INlYQog7VsHdVHF748z6BF6YSz/0bpqvhuUJYT82VMuNLvURM72XwCeqx6hiDF1XAL TF9UOD5mLRsAJr6BhrbuCxkQUIfMg/3Gp/wVuGV2AvygYAv/tFDTm85tRiHO9+OFxKUc G+VmBSRCi7VJjErhSb7+XzLRJwExYl0ft+hmc= Received: by 10.205.64.70 with SMTP id xh6mr154223bkb.309.1313481868529; Tue, 16 Aug 2011 01:04:28 -0700 (PDT) Received: from [192.168.1.101] ([79.114.94.185]) by mx.google.com with ESMTPS id x19sm1794838bkt.42.2011.08.16.01.04.25 (version=SSLv3 cipher=OTHER); Tue, 16 Aug 2011 01:04:26 -0700 (PDT) Message-ID: <4E4A2488.4050706@gmail.com> Date: Tue, 16 Aug 2011 11:04:24 +0300 From: =?ISO-8859-1?Q?T=F6r=F6k_Edwin?= User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110626 Icedove/3.1.11 MIME-Version: 1.0 To: caml-list@inria.fr References: In-Reply-To: X-Enigmail-Version: 1.1.2 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Subject: Re: [Caml-list] Interfacing with C: bad practice On 08/16/2011 10:37 AM, Dmitry Bely wrote: > I would like to share my experience of writing bad C bindings. The > following code is wrong, although no "living in harmony with the > garbage collector" rule seems to be violated: > > value wrp_ml_cons (value v, value l) > { > CAMLparam2(v, l); > CAMLlocal1(cell); > cell = caml_alloc_small(2, Tag_cons); > Field(cell, 0) = v; > Field(cell, 1) = l; > CAMLreturn(cell); > } > > value string_list(const char ** s) > { > CAMLparam0(); > CAMLlocal1(list); > list = Val_emptylist; > while (*s != NULL) { > list = wrp_ml_cons(caml_copy_string(*s), list); /* bug! */ > } > CAMLreturn(list); > } > > In the line > > list = wrp_ml_cons(caml_copy_string(*s), list); /* bug! */ > > C compiler first puts "list" pointer on stack and then calls > caml_copy_string(*s), potentially invalidating "list". list is a local root though, shouldn't that prevent the invalidation? Best regards, --Edwin