Thanks, Keno!
Keeping an object on the C++ heap, but having it GC'ed by Julia, would
probably mean I have to use a unique_ptr or similar, like (as a trivial
example)
s = icxx""" std::unique_ptr<std::string>(new std::string("foo")); """;
But then, things like
@cxx s->length()
won't work anymore, obviously. I really like the `@cxx foo->bar()` syntax
because if work for both objects and pointers, so it's easy to write
generic code that can take both. Especially for what I'm working on (the
ROOT interface) that's very handy - ROOT's design is a bit archaic and the
API uses raw pointers almost everywhere. So I'm try to make that appear
sane (and be safe) from the Julia side, while keeping things as simple as
possible.
Hm, maybe it's not too much of a hack if I rely on the current GC
behaviour, and use pinning if/when it changes?
Cheers,
Oliver
On Wednesday, February 17, 2016 at 4:28:57 AM UTC+1, Keno Fischer wrote:
>
> Sorry, didn't see this before.
>
> The answer is that anything of type CppValue will be owned by julia and
> destructed upon GC. Right now that should be stable, but I will not
> guarantee that given future possible directions of the language, so for use
> cases like this I would recommend keeping the object on the C++ heap so you
> have explicit control over addresses.
>
> On Thu, Feb 11, 2016 at 7:09 AM, Oliver Schulz <[email protected]
> <javascript:>> wrote:
>
>> Hi,
>>
>> is the memory address (seen from C++) of objects created via `@cxx
>> SomeClass()` (or similar) stable over time (GC cycles)?
>>
>> I need to use a container class whose elements keep a pointer to the
>> container itself. I want Julia to own the container, so it should be
>> subject to garbage collection. The container owns it's elements and deletes
>> them on destruction, so they can't outlive the container object. But
>> obviously, the address of the container object must not change over time,
>> else things will break. So is it safe to do
>>
>> container = @cxx Container()
>>
>> or
>>
>> container = icxx""" Container(); """
>>
>> Or do I need to do something like
>>
>> container = icxx""" std::unique_ptr<Container>(new Container); """
>>
>> I would assume that "container.data" moves around during GC cycles, so
>> that `icxx""" &$container """` wouldn't be stable over GC cycles. But
>> this little test seems to indicate that it actually may be stable:
>>
>> cxx"""
>> struct MyContainer {
>> MyContainer* m_origAddr;
>> MyContainer* origAddr() { return m_origAddr; }
>> MyContainer* currAddr() { return this; }
>> bool checkAddr() { return currAddr() == origAddr(); }
>> MyContainer() { m_origAddr = this; }
>> };
>> """
>>
>> typealias MyContainer
>> cxxt"MyContainer"{Int(icxx"""sizeof(MyContainer);""")}
>>
>> rndarray = rand(1000,1000,100)
>>
>> containers = Vector{MyContainer}()
>> for i in 1:20000000 push!(containers, @cxx MyContainer()) end
>> assert(all(x -> icxx""" $x.checkAddr(); """, containers))
>>
>> rndarray = rand(10,10,10)
>> gc()
>>
>> assert(all(x -> icxx""" $x.checkAddr(); """, containers))
>>
>> Keno, help ... ? ;-)
>>
>>
>> Cheers,
>>
>> Oliver
>>
>>
>