# Will TCavnas::Clear() release the memory of everything on it?

**URL:** <https://root-forum.cern.ch/t/will-tcavnas-clear-release-the-memory-of-everything-on-it/59759>\
**Category:** Newbie\
**Tags:** root\
**Created:** [June 23, 2024, 4:37pm UTC](https://root-forum.cern.ch/t/will-tcavnas-clear-release-the-memory-of-everything-on-it/59759 "2024-06-23T16:37:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 23, 2024, 4:37pm UTC](https://root-forum.cern.ch/t/will-tcavnas-clear-release-the-memory-of-everything-on-it/59759/1 "2024-06-23T16:37:27Z")

</div>

Dear experts

Do I need to manually delete pointers of objects that already Drawn on a `TCanvas` after I call `TCanvas::Clear()`? e.g.:

```auto
TCanvas*c = new TCanvas();
TGraph* g = new TGraph();
TMarker* m = new Marker(1,1,22);
g->Draw();
m->Draw();

c->Clear();
//Do I need to do this?
//delete g;
//delete m;

```

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [June 24, 2024, 1:41am UTC](https://root-forum.cern.ch/t/will-tcavnas-clear-release-the-memory-of-everything-on-it/59759/2 "2024-06-24T01:41:14Z")

</div>

Try to draw them after `c->Clear()` and see (note that you need to add at least 1 point to the graph in the first place).

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 24, 2024, 1:54am UTC](https://root-forum.cern.ch/t/will-tcavnas-clear-release-the-memory-of-everything-on-it/59759/3 "2024-06-24T01:54:35Z")

</div>

Seems the sub-pads will be deleted but everything drawn on the pads will not.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 24, 2024, 5:50am UTC](https://root-forum.cern.ch/t/will-tcavnas-clear-release-the-memory-of-everything-on-it/59759/4 "2024-06-24T05:50:11Z")

</div>

When you create a graphics object, it exists independently. Drawing it does not transfer its ownership to the canvas/pad. You need to delete it if you want to free the associated memory.

---

<div class="post-metadata">

**Author:** ![ctacke](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ctacke/32/32430_2.png) [@ctacke](https://root-forum.cern.ch/u/ctacke)\
**Post date:** [August 14, 2024, 1:33pm UTC](https://root-forum.cern.ch/t/will-tcavnas-clear-release-the-memory-of-everything-on-it/59759/5 "2024-08-14T13:33:12Z")

</div>

So would it probably be better to use unique-ptrs instead?

```cpp
auto c = std:::make_unique<TCanvas>();
auto g = std:::make_unique<TGraph>();
auto m = std:::make_unique<TMarker>(1,1,22);
g->Draw();
m->Draw();

c->Clear();

```

Or even valued variables?

```cpp
TCanvas c{};
TGraph g{};
TMarker> m{1,1,22};
g.Draw();
m.Draw();

c.Clear();

```
