# Picture Array / Memory /  Circular Reference

**URL:** <https://forum.xojo.com/t/picture-array-memory-circular-reference/18415>\
**Category:** General\
**Created:** [August 6, 2014, 4:19pm UTC](https://forum.xojo.com/t/picture-array-memory-circular-reference/18415 "2014-08-06T16:19:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Denise\_Adams](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/f08c70/32.png) [@Denise\_Adams](https://forum.xojo.com/u/Denise_Adams)\
**Post date:** [August 6, 2014, 4:19pm UTC](https://forum.xojo.com/t/picture-array-memory-circular-reference/18415/1 "2014-08-06T16:19:26Z")

</div>

Hi,

I have a global array of pictures that remains in memory while the app is running and this array has some pictures added to it which are created on the fly (with p = New Picture and array.append p etc) and others which are simply references to images dragged into the project (i.e. array.append image1).

Is it okay to add references to the same image resource each time within the same array or should I always create a new picture object and draw the image resource to that new picture object?

I’m guessing it’s actually saving on memory to just use the reference but wanted to make sure I wasn’t doing something considered “bad practice” and causing myself some kind of circular reference problem.

Thanks!

---

<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 6, 2014, 5:07pm UTC](https://forum.xojo.com/t/picture-array-memory-circular-reference/18415/2 "2014-08-06T17:07:51Z")

</div>

reusing references keeps memory usage down.

---

<div class="post-metadata">

**Author:** ![Denise\_Adams](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/f08c70/32.png) [@Denise\_Adams](https://forum.xojo.com/u/Denise_Adams)\
**Post date:** [August 6, 2014, 11:39pm UTC](https://forum.xojo.com/t/picture-array-memory-circular-reference/18415/3 "2014-08-06T23:39:24Z")

</div>

Right so the following code is okay then?

(assuming I have dragged an image resource into the project called “image1” and myArray is a global array.

```auto
Dim p, pic As Picture

myArray.append image1

p = New Picture(image1.width, image1.height,32)
p.graphics.forecolor = rgb(0,0,0)
p.graphics.fillrect 0,0,p.width,p.height

myArray.append p

pic = image1

myArray.append pic

pic = p

myArray.append pic
myArray.append p

p.graphics.forecolor = rgb(50,50,50)
p.graphics.fillrect 0,0,p.width,p.height

myArray.append p
pic = p

myArray.append pic

```

What happens if I have referenced p by assigning its value to pic and then change p?

Does the instance of pic in the array change too?

Or does the picture appended to the array remain in the same state it was in when it was added?

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [August 7, 2014, 2:02am UTC](https://forum.xojo.com/t/picture-array-memory-circular-reference/18415/4 "2014-08-07T02:02:03Z")

</div>

You have a total of 2 images, one of which started as a black rectangle which you then turned into a gray rectangle. Your array contains the following references

Image1  
Gray  
Image1  
Gray  
Gray  
Gray  
Gray

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [August 7, 2014, 2:11am UTC](https://forum.xojo.com/t/picture-array-memory-circular-reference/18415/5 "2014-08-07T02:11:07Z")

</div>

And note that the values in the array didn’t change. They’re all separate references to the same object (a picture), the attributes of which did change.

---

<div class="post-metadata">

**Author:** ![Denise\_Adams](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/f08c70/32.png) [@Denise\_Adams](https://forum.xojo.com/u/Denise_Adams)\
**Post date:** [August 7, 2014, 7:35am UTC](https://forum.xojo.com/t/picture-array-memory-circular-reference/18415/6 "2014-08-07T07:35:21Z")

</div>

Thanks for clarfiying.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:21pm UTC](https://forum.xojo.com/t/picture-array-memory-circular-reference/18415/7 "2020-10-29T17:21:49Z")

</div>


