# Arrays with objects created with a plug-in will be never released from Xojo

**URL:** <https://forum.xojo.com/t/arrays-with-objects-created-with-a-plug-in-will-be-never-released-from-xojo/64926>\
**Category:** Plugins\
**Created:** [August 7, 2021, 10:37am UTC](https://forum.xojo.com/t/arrays-with-objects-created-with-a-plug-in-will-be-never-released-from-xojo/64926 "2021-08-07T10:37:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hans-Jürgen\_Weisheit](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/ac8455/32.png) [@Hans-Jürgen\_Weisheit](https://forum.xojo.com/u/Hans-J%C3%BCrgen_Weisheit)\
**Post date:** [August 7, 2021, 10:37am UTC](https://forum.xojo.com/t/arrays-with-objects-created-with-a-plug-in-will-be-never-released-from-xojo/64926/1 "2021-08-07T10:37:17Z")

</div>

I have a simple class that have a constructor, a destructor and one property. If I use the following code in Xojo:  
**Dim a(-1) As MyClass**  
**a.Append(new MyClass)**  
**a.Remove(0)**

The constructor of the class will be called once, and the destructor of the class will be called once. That’s what I’m expect.  
If I implement a method, that will return a array with my class by myself:  
**REALarray tmpArr = REALCreateArray(kTypeObject, -1);**  
**for (int i = 0; i \< 2; i++) {**  
**REALobject myClass = REALnewInstanceOfClass(&MyClassDefinition);**  
**REALInsertArrayValueObject(tmpArr, i, myClass);**  
**}**  
**REALLockObject((REALobject)tmpArr);**  
**return tmpArr;**  
Only the constructor will be called, never the destructor. I see in the Xojo Debugger the created array, which is correct. If I call a.Remove(0), the destructor will not be called too. So if I’m create arrays by myself, I will create memory leaks with objects.  
What’s wrong with the code?

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [August 7, 2021, 10:41am UTC](https://forum.xojo.com/t/arrays-with-objects-created-with-a-plug-in-will-be-never-released-from-xojo/64926/2 "2021-08-07T10:41:33Z")

</div>

> [@Hans-Jürgen\_Weisheit](#):
>
> REALLockObject((REALobject)tmpArr);

That is too much.

---

<div class="post-metadata">

**Author:** ![Hans-Jürgen\_Weisheit](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/ac8455/32.png) [@Hans-Jürgen\_Weisheit](https://forum.xojo.com/u/Hans-J%C3%BCrgen_Weisheit)\
**Post date:** [August 7, 2021, 10:52am UTC](https://forum.xojo.com/t/arrays-with-objects-created-with-a-plug-in-will-be-never-released-from-xojo/64926/3 "2021-08-07T10:52:10Z")

</div>

Hello Christian,  
thank you for your fast response. I have removed the line with the lock command. But it will not change the behaviour. The objects in the array will never be released. Any other suggestions?

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [August 7, 2021, 11:10am UTC](https://forum.xojo.com/t/arrays-with-objects-created-with-a-plug-in-will-be-never-released-from-xojo/64926/4 "2021-08-07T11:10:46Z")

</div>

> [@Hans-Jürgen\_Weisheit](#):
>
> REALnewInstanceOfClass

there is also a REALUnlockObject missing there for this.

---

<div class="post-metadata">

**Author:** ![Hans-Jürgen\_Weisheit](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/ac8455/32.png) [@Hans-Jürgen\_Weisheit](https://forum.xojo.com/u/Hans-J%C3%BCrgen_Weisheit)\
**Post date:** [August 7, 2021, 11:38am UTC](https://forum.xojo.com/t/arrays-with-objects-created-with-a-plug-in-will-be-never-released-from-xojo/64926/5 "2021-08-07T11:38:57Z")

</div>

Thank you Christian, the problem is solved. The documentation about the reference counting is very poor. I wasn’t aware, that REALInsertArrayValueObject will increment the reference counter. I expect that the REALSetArrayValueObject does the same?! I wish that a plug-In developer has the chance to access the current value of the object reference counter for testing propose. Or an event better SDK documentation to avoid such questions.

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [August 7, 2021, 11:59am UTC](https://forum.xojo.com/t/arrays-with-objects-created-with-a-plug-in-will-be-never-released-from-xojo/64926/6 "2021-08-07T11:59:16Z")

</div>

Yes, they will both add one to reference count.

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [August 7, 2021, 12:00pm UTC](https://forum.xojo.com/t/arrays-with-objects-created-with-a-plug-in-will-be-never-released-from-xojo/64926/7 "2021-08-07T12:00:43Z")

</div>

> [@Hans-Jürgen\_Weisheit](#):
>
> I wish that a plug-In developer has the chance to access the current value of the object reference counter for testing propose.

Dereference the object pointer. It starts with 3 pointers and then the int with the reference count.

---

<div class="post-metadata">

**Author:** ![Hans-Jürgen\_Weisheit](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/ac8455/32.png) [@Hans-Jürgen\_Weisheit](https://forum.xojo.com/u/Hans-J%C3%BCrgen_Weisheit)\
**Post date:** [August 7, 2021, 12:03pm UTC](https://forum.xojo.com/t/arrays-with-objects-created-with-a-plug-in-will-be-never-released-from-xojo/64926/8 "2021-08-07T12:03:36Z")

</div>

Perfect 👍, Thank you!

---

<div class="post-metadata">

**Author:** ![Björn\_Eiríksson](https://forum.xojo.com/letter_avatar_proxy/v4/letter/b/da6949/32.png) [@Björn\_Eiríksson](https://forum.xojo.com/u/Bj%C3%B6rn_Eir%C3%ADksson)\
**Post date:** [August 7, 2021, 5:43pm UTC](https://forum.xojo.com/t/arrays-with-objects-created-with-a-plug-in-will-be-never-released-from-xojo/64926/9 "2021-08-07T17:43:09Z")

</div>

**Basic locking rules:**

1. You call method, do not lock.
2. You return Object or string - Lock
3. You call event, same as method, do not lock
4. You get in object as parameter, lock only if you want to store it. (and then unlock when no longer needing).  
5.You get object in as property, lock if you want to store it (and then unlock when no longer needing).
5. Property getter, lock when returning, same was rule 2.

---

<div class="post-metadata">

**Author:** ![Hans-Jürgen\_Weisheit](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/ac8455/32.png) [@Hans-Jürgen\_Weisheit](https://forum.xojo.com/u/Hans-J%C3%BCrgen_Weisheit)\
**Post date:** [August 8, 2021, 5:28pm UTC](https://forum.xojo.com/t/arrays-with-objects-created-with-a-plug-in-will-be-never-released-from-xojo/64926/10 "2021-08-08T17:28:06Z")

</div>

Thank you Björn. Since Christian told me to remove the REALLockObject((REALobject)tmpArr); command before I return the array, I have another question about your lock rules:  
2.) Need a REALArray never to be locked if I return it? Or only, if I return a REALarry without to insert something?  
4, 5) The same handling for REALarray?

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [August 8, 2021, 5:40pm UTC](https://forum.xojo.com/t/arrays-with-objects-created-with-a-plug-in-will-be-never-released-from-xojo/64926/11 "2021-08-08T17:40:58Z")

</div>

**REALCreateArray** returns an array with a lock reference count of 1.  
So you can just return it. Or put it in a C++ class variable.  
But if you do both, add another reference by calling lock again.
