Way to init a var with a dynamicObject


I'm wondering if there is an elegant way to make a var with a dynamicObject inside so that I can use it like a dictionary or javascript object.

Obviously doing this:

var(new DynamicObject());

works but leaks

Doing this

DynamicObject::Ptr dyna = new DynamicObject();


Also works with no leaks but looks complicated

Is there any better suggestion ?


huh? That won't leak unless you leak the var.


Arf… right the "var" were actually leaking!

I thought writing DynamicObject*test = new DynamicObject would put the refCount to 1 but it is == 0.

I truely don't understand the mechanism behind this, who destructs "test" if I do not manually delete it ? Ok it will if I do put it into a var, but if I just create a DynamicObject like above (sure it's useless) would it then leak ?


Well it's reference-counted.. Var just keeps a reference to the object like a ReferenceCountedObjectPtr does.


Ok got it but what about just writing DynamicObject*test = new DynamicObject; alone

would it leak or not ? In my understanding, yes because it is not held into any reference-counted container and therefore will never be incremented to 1 to then come back to 0 and being deleted. Otherwise I truely do not understand how it does work under the hood.

I'm used with objective-c where any new object (or copy) will comes with a refCount == 1 (vs 0 in this case)


Yes, of course that'd leak because there's no smart-pointer to delete it when it goes out of scope. DynamicObject::Ptr p = new DynamicObject() would be safe though. Unlike obj-C the objects start with refcount = 0 because it'll be automatically incremented to 1 when it first gets added to a container.


okay this is what I deduced… thank you =)