caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jakob Lichtenberg <jakobl@windows.microsoft.com>
To: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: FW: CamlIDL: Returning a whole array of cows
Date: Mon, 5 Nov 2007 10:31:58 -0800	[thread overview]
Message-ID: <DACB514F44E4864CAFDC7DAD144A7E6CBC1FB2A05F@NA-EXMSG-W601.wingroup.windeploy.ntdev.microsoft.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2890 bytes --]

Resending due to no answer.

- Jakob

From: Jakob Lichtenberg
Sent: Monday, September 17, 2007 1:11 PM
To: 'caml-list@inria.fr'
Subject: CamlIDL: Returning a whole array of cows

1. Summary: Stub code generated by Camlidl seems to call camlidl_malloc with an uninitialized size.


2. Details:

I am declaring a function 'void get_cows([out] int* len, [length_is(*len), size_is(*len), out] cow** cows);' that creates and returns an array of cows.  (Each cow is just a pointer to a structure.)  The generated stub code seems incorrect:

cow_stubs.c:
value camlidl_cow_get_cows(value _unit)
{
  int *len; /*out*/
  cow **cows; /*out*/
  int _c1;
  mlsize_t _c2;
  value _v3;
  value _vres;

  struct camlidl_ctx_struct _ctxs = { CAMLIDL_TRANSIENT, NULL };
  camlidl_ctx _ctx = &_ctxs;
  len = &_c1;
  cows = camlidl_malloc(*len * sizeof(cow *), _ctx);
  get_cows(len, cows);
  _vres = camlidl_alloc(*len, 0);
  Begin_root(_vres)
    for (_c2 = 0; _c2 < *len; _c2++) {
      _v3 = camlidl_c2ml_cow_cow(&*cows[_c2], _ctx);
      modify(&Field(_vres, _c2), _v3);
    }
  End_roots()
  camlidl_free(_ctx);
  return _vres;
}

As you can see camlidl_malloc is called with an uninitialized value.

Is this a bug in camlidl, or am I writing my IDL file incorrectly.  Another way to ask:  How do I declared that a function reserves an array


3. Implementation details:

This is my C api I'd like to access from ocaml:

cow.h:
typedef struct _cow {
  char* name;
  int age;
} *cow;

cow get_dummy_cow();

void get_cows_inout(int inputlen, int *outputlen, cow ca[]); // Write cows to 'ca', however not more than 'inputlen' elements.  Write number of cows written to '*outputlen'.

void get_cows(int* len, cow **ca); // Malloc array for cows.  Save number of elements to *len, save address for array in '*ca'.

void print_cow(cow o);

I'd like to access this API from OCaml using CamlIDL.  I use the following idl file:

cow.idl:
typedef [abstract] void* cow;

[pointer_default(ref)] interface Cow {

cow get_dummy_cow(void);

void get_cows_inout([in] int inputlen, [out] int * outputlen,
                    [in,out,size_is(inputlen),length_is(*outputlen)] cow d[]);

void get_cows([out] int* len, [length_is(*len), size_is(*len), out] cow** cows);

void print_cow(cow o);

}

I compile this using:

                camlidl cow.idl

And use it from the following ML program:

mlmain.ml:
let main use_inout =
  let cows =
    if use_inout then Cow.get_cows_inout(Array.create 3 (Cow.get_dummy_cow()))
    else Cow.get_cows()
  in
  Array.iter Cow.print_cow cows

let _ = main true;

This works fine.  However, if I change the call to main to 'main false'  I get a crash.

Thanks,


-        Jakob

PS. I know that I am leaking memory - that I can fix with a simple quote(dealloc, "free(*cows);");

[-- Attachment #2: Type: text/html, Size: 16915 bytes --]

             reply	other threads:[~2007-11-05 18:32 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-05 18:31 Jakob Lichtenberg [this message]
2007-11-05 20:50 ` [Caml-list] " Christopher L Conway
2007-11-05 21:20   ` Jakob Lichtenberg
2007-11-05 21:28     ` Christopher L Conway
2007-11-05 21:35       ` Jakob Lichtenberg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DACB514F44E4864CAFDC7DAD144A7E6CBC1FB2A05F@NA-EXMSG-W601.wingroup.windeploy.ntdev.microsoft.com \
    --to=jakobl@windows.microsoft.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).