From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p7G7b9Bl006188 for ; Tue, 16 Aug 2011 09:37:09 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: At8CABAdSk7RVdg2kGdsb2JhbABBqBUIFAEBAQEJCQ0HFAQhgVkCLAEbHgMSEF0BEQEFASI1oTCCVAqMN4JVhSE7iG0CAwaGQQSHWYs5jFk8g2U X-IronPort-AV: E=Sophos;i="4.67,379,1309730400"; d="scan'208";a="115954660" Received: from mail-qw0-f54.google.com ([209.85.216.54]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 16 Aug 2011 09:37:04 +0200 Received: by qwc9 with SMTP id 9so4977105qwc.27 for ; Tue, 16 Aug 2011 00:37:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; bh=7EZRC5ETc9xOxW1XtCp16477uUl5T3uiT3DWFMMrKsU=; b=qwMdoclK93vsyhveyhqWiEUCqs2xvHxZ4AE6dlf4dobYe8PPa1wQlFgHBjLugzEq6U d2Ib0MPdEqxL4D/P2CAoS7aRjc/LjF1+yvNxXTE9cCzdqv4wFWV4wadDCNYFsBY3rlCQ mfsivBFxYnTccBeryAmvqKrBIsJI4ZQTyIFBQ= MIME-Version: 1.0 Received: by 10.229.68.148 with SMTP id v20mr3189157qci.80.1313480223332; Tue, 16 Aug 2011 00:37:03 -0700 (PDT) Received: by 10.229.76.229 with HTTP; Tue, 16 Aug 2011 00:37:03 -0700 (PDT) Date: Tue, 16 Aug 2011 11:37:03 +0400 Message-ID: From: Dmitry Bely To: Caml List Content-Type: text/plain; charset=ISO-8859-1 Subject: [Caml-list] Interfacing with C: bad practice 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". Of course, the stack copy of "list" is not registered as a global root so wrp_ml_cons gets an invalid value. Maybe Ocaml docs should be updated to warn about pitfalls like that? - Dmitry Bely