# Way to init a var with a dynamicObject

**URL:** <https://forum.juce.com/t/way-to-init-a-var-with-a-dynamicobject/12037>\
**Category:** General JUCE discussion\
**Created:** [December 14, 2013, 12:16pm UTC](https://forum.juce.com/t/way-to-init-a-var-with-a-dynamicobject/12037 "2013-12-14T12:16:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xample](https://avatars.discourse-cdn.com/v4/letter/x/d9b06d/32.png) [@Xample](https://forum.juce.com/u/Xample)\
**Post date:** [December 14, 2013, 12:16pm UTC](https://forum.juce.com/t/way-to-init-a-var-with-a-dynamicobject/12037/1 "2013-12-14T12:16:34Z")

</div>

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();var(dyna.getObject());
```

Also&nbsp;works with no leaks but looks complicated

Is there any better suggestion ?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [December 14, 2013, 1:23pm UTC](https://forum.juce.com/t/way-to-init-a-var-with-a-dynamicobject/12037/2 "2013-12-14T13:23:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Xample](https://avatars.discourse-cdn.com/v4/letter/x/d9b06d/32.png) [@Xample](https://forum.juce.com/u/Xample)\
**Post date:** [December 14, 2013, 2:17pm UTC](https://forum.juce.com/t/way-to-init-a-var-with-a-dynamicobject/12037/3 "2013-12-14T14:17:39Z")

</div>

Arf… right the "var" were actually leaking!

I thought writing&nbsp;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 ?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [December 14, 2013, 2:29pm UTC](https://forum.juce.com/t/way-to-init-a-var-with-a-dynamicobject/12037/4 "2013-12-14T14:29:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Xample](https://avatars.discourse-cdn.com/v4/letter/x/d9b06d/32.png) [@Xample](https://forum.juce.com/u/Xample)\
**Post date:** [December 14, 2013, 2:47pm UTC](https://forum.juce.com/t/way-to-init-a-var-with-a-dynamicobject/12037/5 "2013-12-14T14:47:42Z")

</div>

Ok got it but what about just writing&nbsp;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&nbsp;==&nbsp;1 (vs 0 in this case)

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [December 14, 2013, 2:56pm UTC](https://forum.juce.com/t/way-to-init-a-var-with-a-dynamicobject/12037/6 "2013-12-14T14:56:48Z")

</div>

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&nbsp;when it first gets added to a container.

---

<div class="post-metadata">

**Author:** ![Xample](https://avatars.discourse-cdn.com/v4/letter/x/d9b06d/32.png) [@Xample](https://forum.juce.com/u/Xample)\
**Post date:** [December 14, 2013, 2:59pm UTC](https://forum.juce.com/t/way-to-init-a-var-with-a-dynamicobject/12037/7 "2013-12-14T14:59:30Z")

</div>

okay this is what I deduced… thank you =)
