Hello,
I realized that this question is probably not specific to Cxx.jl, but
concerns Julia's garbage collection in general: Is it save to keep a
pointer to a Julia-owned object in an FFI environment (e.g. from C), as
long as the object is not GC'ed? From what I saw, the current Julia garbage
collector is non-moving. Should I be prepared for Julia to switch to a
moving/compacting GC in the foreseeable future, or is a next-generation GC
for Julia likely to be non-moving as well?
Cheers,
Oliver
On Thursday, February 11, 2016 at 1:09:44 PM UTC+1, Oliver Schulz 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
>
>