Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0007423OCamlruntime system and C interfacepublic2016-11-29 11:192016-12-27 10:18
Assigned Toshinwell 
PlatformLinuxOSUbuntuOS Version14.04
Product Version4.03.0 
Target VersionFixed in Version 
Summary0007423: caml_invalid_argument can't be used with dynamically allocated strings
DescriptionFrom [^]

caml_invalid_argument(str) is no return and does not free it's
argument. So calling it with a string constructed dynamically will mean it'll never get freed.
I could construct an ocaml string and pass that to caml_raise_with_arg,
but I don't seem to be able to get to the caml_exn_Invalid_argument from

Reply from Gabriel Scherer:
It may make sense to have caml_{failwith,invalid_argument}_value
variants of the exception-raising functions that take a parameter, and
be implemented using caml_raise_with_arg(s) directly instead of
caml_raise_with_string. Could you open a mantis issue or submit a
github pull request to track the question and continue discussion?
Steps To Reproducechar* error = new char[50];
snprintf(error, 50, "error: %d", 50);

# allocated memory is lost here.
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
gasche (administrator)
2016-12-03 18:08

Patch proposed in [^]
xleroy (administrator)
2016-12-03 19:56

I'm not convinced by the repro case (just declare "char error[50];") nor by the need in general.
gasche (administrator)
2016-12-03 20:02

Richard W. Jones mentioned that he could use a similar feature on the mailing-list: [^]

> We have the same problem in libguestfs in a few places. The solution
> is to use an allocation on the C stack, either a fixed size buffer or
> [although we don't currently use this] a variable sized one using
> alloca. The string is freed when the stack is unwound. Examples:
> [^]
> [^]
> Be nice to have a "freeing" version of caml_raise* I suppose.

The stack-based solutions rely on guessing arbitrary size limits on the dynamic strings, which is often possibles but sometimes inconvenient/inelegant.
shinwell (developer)
2016-12-27 10:18

Moving discussion to here: [^]

- Issue History
Date Modified Username Field Change
2016-11-29 11:19 sdev New Issue
2016-12-03 18:08 gasche Note Added: 0016627
2016-12-03 19:56 xleroy Note Added: 0016628
2016-12-03 20:02 gasche Note Added: 0016630
2016-12-06 15:41 frisch Severity minor => feature
2016-12-08 10:42 shinwell Status new => acknowledged
2016-12-27 10:18 shinwell Note Added: 0017046
2016-12-27 10:18 shinwell Status acknowledged => closed
2016-12-27 10:18 shinwell Assigned To => shinwell
2016-12-27 10:18 shinwell Resolution open => duplicate
2017-02-23 16:43 doligez Category OCaml runtime system => runtime system
2017-03-03 17:45 doligez Category runtime system => runtime system and C interface

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker