On Tue, Jun 28, 2016 at 7:17 PM, Oscar Blumberg <oscar.blumb...@ens.fr> wrote:
> https://github.com/JuliaLang/julia/blob/master/src/julia.h#L588

Oh, sorry, this thread fall off my reply list twice....

> Yichao, could you explain why this is unsafe?

Yes, exactly what Oscar said. The results of `jl_box_*` should be
rooted before you call another function if you want it to be valid
after that function call. The GC might collect it otherwise.

> Do you know of an alternative which is safe?

Root them with `JL_GC_PUSH*` functions.

The equivalent with `jl_tuple*` (assuming you still want to construct
a tuple) is `jl_new_structv` with the tuple type passed in.
The tuple type can be constructed with one of

```
JL_DLLEXPORT jl_value_t *jl_apply_type(jl_value_t *tc, jl_svec_t *params);
JL_DLLEXPORT jl_tupletype_t *jl_apply_tuple_type(jl_svec_t *params);
JL_DLLEXPORT jl_tupletype_t *jl_apply_tuple_type_v(jl_value_t **p, size_t np);
```

(The name of these functions should hopefully be descriptive enough)

Also note that instead of boxing the int's and passing them as
jl_value_t* to the constructor, you can take advantage of julia's
C-compatible object layout. The layout of `Tuple{Int64,Int64}` is
exactly the same with the C `struct {int64_t; int64_t;};` so you can
just pass a pointer to such a C type to `jl_new_bits` (with the write
julia type) or getting an uninitialized object with
`jl_new_struct_uninit` and cast to your C type and fill it with your
data later.


>
>
> On Monday, June 27, 2016 at 2:16:01 PM UTC-4, Stephen Chisholm wrote:
>>
>>
>> On Monday, 20 June 2016 16:31:29 UTC-3, Yichao Yu wrote:
>>>
>>> On Mon, Jun 20, 2016 at 3:27 PM, Stephen Chisholm <sbchi...@gmail.com>
>>> wrote:
>>> > While helping out with
>>> >
>>> > http://stackoverflow.com/questions/37913625/call-julia-svd-from-c/37929076#37929076
>>> > I noticed jl_tuple2 was removed in
>>> > afd4eb038dcd9fa3e512509f4c332a9e7763ba59
>>> > by Jeff Bezanson while refactoring tuples.  Just wondering if anyone is
>>> > able
>>> > to help me find an alternative.  Below is what I used to be able to do.
>>> > My
>>> > current work around is to use jl_eval_string("(2,2)"), but I'm hoping
>>> > that
>>> > there is still a way to build the dims parameter without a call to
>>> > jl_eval_string.
>>> >
>>> >
>>> >       double m[] = {1,2,3,4};
>>> >
>>> >       jl_value_t *array_type = jl_apply_array_type(jl_float64_type, 2);
>>> >       jl_tuple_t *dims = jl_tuple2(jl_box_int64(2), jl_box_int64(2));
>>>
>>> FYI, this is generally unsafe.
>>>
>>> >       jl_array_t *jl_m = jl_ptr_to_array(array_type, m, dims, 0);
>>> >
>>> >

Reply via email to