Problem with TF1 pointer

what i intended to do is to have one master fit function (in this case the fit_func_x). For every new idx at jdx=0, i call the master fit function to do the fit, then clone the master function to proceed with the rest of jdx. Therefore, we should see at every jdx=0 the pointer of the master function is being called, and another pointer for jdx = 2~4 (different for all idx, 2nd output in my posting).

void debug(){
    vector <int> Filters = {6,4}; //{1,2,3,4,6};
    int Ni= Filters.size();

    vector <double> EndPoints = {40,150,350,550,650};
    int Nj = EndPoints.size();

    // TF1* fit_func_x = new TF1(Form(func_name_x, muonID, 0, 0), func_2freq_constA_constW, ta, tb, init_vals_x.size());
    TF1* fit_func_x = new TF1("func_00", "[0] + [1]*cos([2]*x + [3]) + [4]*cos([5]*x + [6])",30,40);
    fit_func_x->SetNpx(500000);
    fit_func_x->SetLineColor(2);


    // GRid scan !
    cout << "    scanning x graphs ... " << endl; //debug

    for(int idx=0; idx<Ni; idx++){  // loop through fittiing of a graph at particular pts/Tc. clone the func at the beginning (8pts)
        int pts = int(48/Filters[idx]);

        TF1* fit_func_x_working=nullptr;
        cout << "    ptr of master func (every muonID, we use a new func): "<< fit_func_x  << endl; //debug

        for(int jdx=0; jdx<Nj; jdx++){ // for the same graph of pts, we study the change in fitting range
            double endpt = EndPoints[jdx];

            // cout << Form("        %d_%.0f",pts,endpt)<< endl;

            if(jdx == 0){ 
                fit_func_x_working = fit_func_x;
            };

            // I skip fitting codes here ...

cout << Form("        %d_%.0f. name : %s ; ptr is: (1 != 2=3=4=...)",pts,endpt, fit_func_x_working->GetName()) << fit_func_x_working  << endl; //debug
            cout <<  "       "<< fit_func_x_working->GetName() << endl; // debug

            if(jdx == 0){ // clone the state after the first fit. we want to maintain the state of the master func to the next pts (idx)
                fit_func_x_working = (TF1*) fit_func_x->Clone(); //Form("%d_%d",idx,jdx)
                // cout << fit_func_x_working << endl;
            };
            
        };

        delete fit_func_x_working;         
}
}

with the last line delete fit_func_x_working, i got the following which is not expected:

ptr of master func (every muonID, we use a new func): 0x55a09cb5f330
        8_40. name : func_00 ; ptr is: (1 != 2=3=4=...)0x55a09cb5f330
        8_150. name : func_00 ; ptr is: (1 != 2=3=4=...)0x55a09c5f9eb0
        8_350. name : func_00 ; ptr is: (1 != 2=3=4=...)0x55a09c5f9eb0
        8_550. name : func_00 ; ptr is: (1 != 2=3=4=...)0x55a09c5f9eb0
        8_650. name : func_00 ; ptr is: (1 != 2=3=4=...)0x55a09c5f9eb0
    ptr of master func (every muonID, we use a new func): 0x55a09cb5f330
        12_40. name : func_00 ; ptr is: (1 != 2=3=4=...)0x55a09cb5f330
        12_150. name : func_00 ; ptr is: (1 != 2=3=4=...)0x55a09c5f9eb0
        12_350. name : func_00 ; ptr is: (1 != 2=3=4=...)0x55a09c5f9eb0
        12_550. name : func_00 ; ptr is: (1 != 2=3=4=...)0x55a09c5f9eb0
        12_650. name : func_00 ; ptr is: (1 != 2=3=4=...)0x55a09c5f9eb0

when i commented that line, i got expected result instead,

ptr of master func (every muonID, we use a new func): 0x561560a74e00
        8_40. name : func_00 ; ptr is: (1 != 2=3=4=...)0x561560a74e00
        8_150. name : func_00 ; ptr is: (1 != 2=3=4=...)0x56156055eba0
        8_350. name : func_00 ; ptr is: (1 != 2=3=4=...)0x56156055eba0
        8_550. name : func_00 ; ptr is: (1 != 2=3=4=...)0x56156055eba0
        8_650. name : func_00 ; ptr is: (1 != 2=3=4=...)0x56156055eba0
    ptr of master func (every muonID, we use a new func): 0x561560a74e00
        12_40. name : func_00 ; ptr is: (1 != 2=3=4=...)0x561560a74e00
        12_150. name : func_00 ; ptr is: (1 != 2=3=4=...)0x561560ab4dd0
        12_350. name : func_00 ; ptr is: (1 != 2=3=4=...)0x561560ab4dd0
        12_550. name : func_00 ; ptr is: (1 != 2=3=4=...)0x561560ab4dd0
        12_650. name : func_00 ; ptr is: (1 != 2=3=4=...)0x561560ab4dd0

please help. omit fit quality for now .


ROOT Version: 6.26.00 (conda)
Platform: centos7
Compiler: gcc9


Hi @NgJunKai,

You’re deleting the pointer fit_func_x_working while you’re still in the larger loop: for(int idx=0; idx<Ni; idx++), so that’s why you get the same memory address twice, i.e. 0x55a09c5f9eb0 in your exact example above.

Since you’re not initialising your fit_func_x_working pointer with new you don’t need to delete it. You should delete the fit_func_x pointer before closing the final curly bracket (your example is missing this one final bracket as well).

Cheers,
Marta

sorry for incomplete description and typo in the code (already updated).
using the same pointer for jdx=2,3,4 is what i am trying to do. but what concerned is this pointer of (jdx=2,3,4) should not be the same across idx ,which is something similar to my 2nd output.

so doing this without the delete will not cause memory leakage ?
ps: i am actually using fit_func_x_working as a temporary “container” to switch between the master function and its clone. i am not sure about the validity of doing so…

Hi,

yes, I understood that.

The pattern you want is in a way:

A
A
B
B
B
B
A
A
C
C
C
C

The issue appears when you have both

TF1* fit_func_x_working=nullptr;
and
delete fit_func_x_working;

if you don’t initialize to nullptr but do TF1* fit_func_x_working instead and then delete the pointer it will also work as expected.

Cheers,
Marta

tried the following combination. seems like the only thing that matters is the delete line …

A = TF1* fit_func_x_working=nullptr; ?
B = delete fit_func_x_working; ?

A | B | result
0 0 expected
0 1 not
1 0 expected
1 1 not

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.