# Nullptr after using chain -\> SetEntryList()

**URL:** <https://root-forum.cern.ch/t/nullptr-after-using-chain-setentrylist/43511>\
**Category:** Newbie\
**Created:** [February 11, 2021, 7:05pm UTC](https://root-forum.cern.ch/t/nullptr-after-using-chain-setentrylist/43511 "2021-02-11T19:05:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmattspartacus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jmattspartacus/32/24820_2.png) [@jmattspartacus](https://root-forum.cern.ch/u/jmattspartacus)\
**Post date:** [February 11, 2021, 7:05pm UTC](https://root-forum.cern.ch/t/nullptr-after-using-chain-setentrylist/43511/1 "2021-02-11T19:05:19Z")

</div>

Hi,

I am using the following code inside a member function of a class. When I call the member function the first time, I have no issues.

However, upon calling the function a second time, EList has become a nullptr, I have not freed or otherwise destroyed EList, so this is somewhat confusing to me.

```auto
void MyClass::GetEventsFromCut(TCut cut)
{
        // get all events from the tree
        this -> Chain -> Draw(">>elist", cut, "entrylist");
        this -> CurrentCut = cut;

        // structure for keeping
        // all events that passed the cut
        this -> EList = (TEntryList*)gDirectory->Get("elist");

        // set the chain to use this entrylist
        this -> Chain -> SetEntryList(this -> EList);

        // number of events to pass cut
        cout << this -> EList -> GetN() << " Events passed cut" << endl;
}

```

I’ve reviewed the source [ROOT: tree/tree/src/TChain.cxx Source File](https://root.cern.ch/doc/master/TChain_8cxx_source.html) but I don’t see where the pointer could be freed or destroyed unless this is scope management issue on my part. Any help is greatly appreciated.

Thanks,

James

* * *

\_ROOT Version:6.14/06  
\_Platform: Linux x8664  
\_Compiler: gcc

* * *

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 11, 2021, 10:19pm UTC](https://root-forum.cern.ch/t/nullptr-after-using-chain-setentrylist/43511/2 "2021-02-11T22:19:48Z")

</div>

Before the “`Draw`”, add:  
`this -> Chain -> SetEntryList(0);`

---

<div class="post-metadata">

**Author:** ![jmattspartacus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jmattspartacus/32/24820_2.png) [@jmattspartacus](https://root-forum.cern.ch/u/jmattspartacus)\
**Post date:** [February 12, 2021, 5:46am UTC](https://root-forum.cern.ch/t/nullptr-after-using-chain-setentrylist/43511/3 "2021-02-12T05:46:37Z")

</div>

Hi,

Thanks for getting back with me, I was still getting a nullptr error on this line

```auto
cout << this -> EList -> GetN() << " Events passed cut" << endl;

```

So to make sure of what was going on I commented out this line and added some cout statements. So the updated version of this is

```auto
void MyClass::GetEventsFromCut(TCut cut)
{
        // get all events from the tree
        this -> Chain -> Draw(">>elist", cut, "entrylist");
        this -> CurrentCut = cut;
        cout << "processed cut!" << endl;
        // structure for keeping
        // all events that passed the cut
        this -> EList = (TEntryList*)gDirectory->Get("elist");

        cout << "got EList!" << endl;

        this -> Chain -> SetEntryList(0);
        
        cout << "emptied entry list!" << endl;
        // set the chain to use this entrylist
        this -> Chain -> SetEntryList(this -> EList);
        
        cout << "set entry list!" << endl;

        // number of events to pass cut
        //cout << this -> EList -> GetN() << " Events passed cut" << endl;
}

```

It seems anytime after

```auto
this -> EList = (TEntryList*)gDirectory->Get("elist");

```

That EList is a nullptr. I’m going to dig more into gDirectory and see if that maybe has anything to do with it, but any additional feedback is very welcome in the meantime.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 12, 2021, 9:27am UTC](https://root-forum.cern.ch/t/nullptr-after-using-chain-setentrylist/43511/4 "2021-02-12T09:27:19Z")

</div>

Put the “SetEntryList(0)” line right BEFORE the “Draw” line.

---

<div class="post-metadata">

**Author:** ![jmattspartacus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jmattspartacus/32/24820_2.png) [@jmattspartacus](https://root-forum.cern.ch/u/jmattspartacus)\
**Post date:** [February 12, 2021, 3:03pm UTC](https://root-forum.cern.ch/t/nullptr-after-using-chain-setentrylist/43511/5 "2021-02-12T15:03:48Z")

</div>

Apologies, I was much too tired when I did that last bit and missed that detail. Doing that works!

Thanks so much for your help.
