# Graphics(image) stops working after I draw image in another Graphics

**URL:** <https://forum.juce.com/t/graphics-image-stops-working-after-i-draw-image-in-another-graphics/33444>\
**Category:** General JUCE discussion\
**Created:** [May 17, 2019, 4:08pm UTC](https://forum.juce.com/t/graphics-image-stops-working-after-i-draw-image-in-another-graphics/33444 "2019-05-17T16:08:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![burkphil](https://avatars.discourse-cdn.com/v4/letter/b/d2c977/32.png) [@burkphil](https://forum.juce.com/u/burkphil)\
**Post date:** [May 17, 2019, 4:08pm UTC](https://forum.juce.com/t/graphics-image-stops-working-after-i-draw-image-in-another-graphics/33444/1 "2019-05-17T16:08:14Z")

</div>

I create a Graphics for an Image in the constructor of my MainComponent, which extends AnimatedAppComponent:

```
mImage.reset(new Image(Image::RGB, 200, 200, true));
mImageGraphics.reset(new Graphics(*mImage));
mImageGraphics->fillAll (Colours::blue);

```

I can draw into the image using mImageGraphics:

```
mImageGraphics->drawLine(x0, y0, x1, y1);

```

Then I draw the image into my MainComponent using:

```
g.drawImageAt(*mImage, x, y);

```

Everything works fine up to this point. But when I continue drawing into my image none of the new drawing shows up at the end. If I create a new Graphics(image) and draw into that then it works. But I would prefer not to have to reallocate it every time.

It is as if drawing the image into another Graphics makes the first Graphic(image) stop working. Are the Image and the original Graphics getting disconnected somehow?

My goal is to keep an image that accumulates drawing operations over time. When the paint method is called I want to just draw the current image into the window to see the changes. (Currently all of this is happening in one GUI thread but I might do the drawing into the Image in another thread eventually.)

I tried to upload my class code but was not allowed.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [May 17, 2019, 6:17pm UTC](https://forum.juce.com/t/graphics-image-stops-working-after-i-draw-image-in-another-graphics/33444/2 "2019-05-17T18:17:48Z")

</div>

You are creating two objects on the heap, that are not supposed to be there:

[Image](https://docs.juce.com/develop/classImage.html#details) is a lightweight wrapper around a shared [ImagePixelData](https://docs.juce.com/develop/classImagePixelData.html) storage. So passing an Image from one point to another is cheap. It is internally reference counted.

If you want to create an independent copy of an image, so you don’t draw accidentally on the original, call [createCopy()](https://docs.juce.com/develop/classImage.html#ab01ea9a3ee0fdf41398542859685c9af).

The second is the [Graphics](https://docs.juce.com/develop/classGraphics.html) context. This is again a class to be created on the stack and to be forgotten straight after drawing.

```auto
// class member
Image mImage;

// to setup in the constructor just assign an Image
mImage = Image (Image::RGB, 200, 200, true);

// to draw into the Image:
Graphics g (mImage);
g.fillAll (Colours::blue);

// and in paint:
g.drawImageAt (mImage, x, y);

```

Hope that helps

---

<div class="post-metadata">

**Author:** ![burkphil](https://avatars.discourse-cdn.com/v4/letter/b/d2c977/32.png) [@burkphil](https://forum.juce.com/u/burkphil)\
**Post date:** [May 18, 2019, 1:56am UTC](https://forum.juce.com/t/graphics-image-stops-working-after-i-draw-image-in-another-graphics/33444/3 "2019-05-18T01:56:14Z")

</div>

Thanks. That works. I am a little worried about the construction of a Graphics object every time I draw. But I’ll try it.

I still wonder why my Graphics object is unusable after drawing the Image in another Graphics. An unsolved mystery.

BTW, loving JUCE! So much better than using Objective C on Mac OS.

---

<div class="post-metadata">

**Author:** ![alassandro](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/alassandro/32/4297_2.png) [@alassandro](https://forum.juce.com/u/alassandro)\
**Post date:** [May 20, 2019, 5:32pm UTC](https://forum.juce.com/t/graphics-image-stops-working-after-i-draw-image-in-another-graphics/33444/4 "2019-05-20T17:32:13Z")

</div>

To clarify on @daniel’s points about the heap:

Both the `Image` and `Graphics` classes employ a concept called Resource Acquisition Is Initialisation (RAII). Essentially this means that they perform setup and tear-down functionality (via the class constructors and destructors) any time you create/destroy one of them.

As Daniel mentioned, the `Image` class is a reference-counting wrapper around some shared pixel data. When you create an image simply by `Image myImage;` it doesn’t refer to anything, but creating it via `Image myImage(/* constructor arguments */);` will create new backing pixel data for you.

Copy-assignment of images (i.e. `Image someImage = myExistingImage;`) simply bumps up the reference count of an underlying pixel buffer, so both instances of the class refer to the same backing data. When all instances of `Image` are destroyed or are set to a null image, the reference count of their shared underlying data will drop to 0 and it will automatically be de-allocated.

The `Graphics` object is similar, you initialise it with a given `Image` and it will be tied _to that image_ for its lifespan.

The reason you normally don’t heap allocate either of these types of objects is because of their RAII properties.

For starters, the `Image` class doesn’t really hold any data itself besides that shared reference count, so you can simply pass `Image` objects around as if there were like pointers or references. As long as you’re doing copy-construction/copy-assignment (i.e. `Image someImage = myExistingImage;`) the backing pixel data will remain “alive”. This means you can put `Image` objects into arrays and other data structures without actually using the `new` operator to heap allocate them.

Similarly, you only need to instantiate a `Graphics` context in the immediate place you need to use it. When you draw into a stored `Image` cache, you can simply create a local `Graphics` object on the stack. Keeping multiple `Graphics` contexts alive that are all drawing into the same image isn’t really advised, and it may be why you’re running into issues.

---

<div class="post-metadata">

**Author:** ![t0m](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/t0m/32/16413_2.png) [@t0m](https://forum.juce.com/u/t0m)\
**Post date:** [May 22, 2019, 11:32am UTC](https://forum.juce.com/t/graphics-image-stops-working-after-i-draw-image-in-another-graphics/33444/5 "2019-05-22T11:32:52Z")

</div>

There’s some internal caching of CoreGraphics-backed images that’s used when you call `drawImageAt`, and this cache is only cleared when you create a new `Graphics` that references the internal data. Subsequent `drawImageAt` calls will fetch the previously rendered image.

Unfortunately there’s no way of clearing this cache directly, so you’ll need to create a new `Graphics` object before each `drawImageAt`.
