# Raw Pointers from TFile Get\<TTree\>

**URL:** <https://root-forum.cern.ch/t/raw-pointers-from-tfile-get-ttree/64332>\
**Category:** Newbie\
**Created:** [October 17, 2025, 3:26pm UTC](https://root-forum.cern.ch/t/raw-pointers-from-tfile-get-ttree/64332 "2025-10-17T15:26:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unmovingcastle](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/u/ebca7d/32.png) [@unmovingcastle](https://root-forum.cern.ch/u/unmovingcastle)\
**Post date:** [October 17, 2025, 3:26pm UTC](https://root-forum.cern.ch/t/raw-pointers-from-tfile-get-ttree/64332/1 "2025-10-17T15:26:18Z")

</div>

In the following, I purposefully put a `TFile` on the heap.

```auto
TFile * myFile = new TFile("myCoolFile.root");

```

and read a `TTree` from the file

```auto
TTree * myTree = (TTree*) myFile->Get<TTree>("myCoolTree");

```

My Questions are:

1. How do I de-allocate the resources? Should I close the file and delete the pointers?

```auto
myFile->Close();
delete myFile;
delete myTree;

```

1. Is there any way I can avoid using raw pointers? I understand that I can put `TFile` on the stack, but is there a way I can retrieve a reference-like view of the `TTree` via `Get` instead of a 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 17, 2025, 7:07pm UTC](https://root-forum.cern.ch/t/raw-pointers-from-tfile-get-ttree/64332/2 "2025-10-17T19:07:39Z")

</div>

The `TTree` is co-own by the user and the `TFile`. So you can do either:

```cpp
delete myTree;
delete myFile;

```

or simply

```cpp
delete myFile; // This will delete the TTree

```

Related, the `Close` call will also delete the `TTree` object.

You can hold the `TFile` and `TTree` in a `unique_ptr` :

```cpp
   std::unique_ptr<TFile> myFile{TFile::Open("myCoolFile.root")};
   std::unique_ptr<TTree> myTree{myFile->Get<TTree>("myCoolTree")};

```
