# Possible leak with dynamic\_cast and TKey::ReadObj?

**URL:** <https://root-forum.cern.ch/t/possible-leak-with-dynamic-cast-and-tkey-readobj/56799>\
**Category:** Newbie\
**Created:** [October 21, 2023, 10:10am UTC](https://root-forum.cern.ch/t/possible-leak-with-dynamic-cast-and-tkey-readobj/56799 "2023-10-21T10:10:47Z")\
**Posts on this page:** 7\
**Page:** 1

<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:** [October 21, 2023, 10:10am UTC](https://root-forum.cern.ch/t/possible-leak-with-dynamic-cast-and-tkey-readobj/56799/1 "2023-10-21T10:10:47Z")

</div>

I am looking at the [`TKey::ReadObj()`](https://root.cern.ch/doc/v628/classTKey.html#adb768880752e46818736f82b1532e54f) docs for ROOT 6.28. The “can create a new object […] This new object […]” lets me assume, that `ReadObj` returns an owning pointer (the implementation code also looks like this)?

A bit down under “Example2” it recommends to use `dynamic_cast` to cast to `MyClass`. If the TKey actually contains a MyClass, then everything is fine. But if it contains something else (that is not “castable” to MyClass), then I think, this code will produce a leak? (Obviously a `static_cast` is even worse.)

If this does not leak, I would like to learn how this works internally.

If this could leak, I would suggest to at least add a small warning to the documentation?

And I would like to learn, what is the recommended way to not leak (assuming MyClass is derived from TObject)?

I can think of two variants:

1. Handling it manually:

2. Using [`TKey::ReadObject<T>()`](https://root.cern.ch/doc/v628/classTKey.html#a686f5692b42620e16fc737cf8e8ae031):

Can you please recommend on how to handle things?

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [October 23, 2023, 9:50pm UTC](https://root-forum.cern.ch/t/possible-leak-with-dynamic-cast-and-tkey-readobj/56799/2 "2023-10-23T21:50:27Z")

</div>

> [@ctacke](#):
>
> then I think, this code will produce a leak?

You are right ☹

> Handling it manually:

Indeed you code works.

> But the documentation looks like this is not intended for classes deriving from TObject?

The new interface can be used for `TObject`.

---

<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:** [October 24, 2023, 8:22am UTC](https://root-forum.cern.ch/t/possible-leak-with-dynamic-cast-and-tkey-readobj/56799/3 "2023-10-24T08:22:57Z")

</div>

> [@pcanal](#):
>
> The new interface can be used for `TObject`.

So would you recommend using `TKey::ReadObject<T>()`?

This seems quite compact and nice? (I would have liked a `std::unique_ptr` to convey that it is an owning pointer).

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [October 24, 2023, 4:52pm UTC](https://root-forum.cern.ch/t/possible-leak-with-dynamic-cast-and-tkey-readobj/56799/4 "2023-10-24T16:52:17Z")

</div>

> So would you recommend using `TKey::ReadObject<T>()`?

Yes.

> (I would have liked a `std::unique_ptr` to convey that it is an owning pointer).

Not quite always. For `TTree`, `TH*` and a couple of other it is actual shared between TFile and the user.

---

<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:** [October 25, 2023, 8:53am UTC](https://root-forum.cern.ch/t/possible-leak-with-dynamic-cast-and-tkey-readobj/56799/5 "2023-10-25T08:53:24Z")

</div>

> [@pcanal](#):
>
> Not quite always. For `TTree`, `TH*` and a couple of other it is actual shared between TFile and the user.

Hmmm. But you’re still allowed to `delete` those via the pointer returned from `ReadObj` / `ReadObject<T>`, right? Otherwise the manual checking variant (1) would be wrong (let’s assume the key points to a TTree, but I was looking for MyClass).

Assuming you’re always allowed to `delete` what is returned from those methods, a `std::unique_ptr` wouldn’t be too wrong? It would just delete in cases, where TFile would also do it later on?

The problem would arise, if the TFile gets destructed before the TTree, because then we would have a double delete?

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [October 25, 2023, 2:16pm UTC](https://root-forum.cern.ch/t/possible-leak-with-dynamic-cast-and-tkey-readobj/56799/6 "2023-10-25T14:16:04Z")

</div>

> But you’re still allowed to `delete` those via the pointer returned from `ReadObj` / `ReadObject<T>`, right?

Yes.

> The problem would arise, if the TFile gets destructed before the TTree, because then we would have a double delete?

This is correct.

> Assuming you’re always allowed to `delete` what is returned from those methods, a `std::unique_ptr` wouldn’t be too wrong?

It would give the wrong ‘indication/semantic’ for `TTree` and `TH1` as for those there is a shared ownership (with the issue you pointed out).

We attempted to upgrade the return type of `ReadObject` but we failed to find a way that was backward compatible.

---

<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:** [October 27, 2023, 11:23am UTC](https://root-forum.cern.ch/t/possible-leak-with-dynamic-cast-and-tkey-readobj/56799/7 "2023-10-27T11:23:53Z")

</div>

> [@pcanal](#):
>
> We attempted to upgrade the return type of `ReadObject` but we failed to find a way that was backward compatible.

Yeah. As the return type is not part of the signature, it does not take part in overload resolution. The only way around this is to introduce a new name. Something like `Get<T>()` (like in `TFile::Get`). I don’t know. I don’t really have a good suggestion.

That said, the other question would be: What would be the correct return type? As you pointed out, `std::unique_ptr` is not 100% correct (it’s correct as long, as the TFile is alive). A `std::shared_ptr` would match things more correctly (for TH1 & Co, TFile would also have a `std::shared_ptr`, for all the others, the returned `shared_ptr` would be the single owner). But implementing the old ReadObj / ReadObject will be a small headache.
