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]
> 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
>
>

Reply via email to