Thanks for the replies! They were very helpful for understanding ownership a little better.
If I understand correctly, will the following work correctly without giving me a dangling pointer?
TH1D* get_histogram() {
TFile filename ("input_file.root");
TH1D* histogram_in_function = filename.Get<TH1D>("histogram_name");
histogram_in_function->SetDirectory(nullptr); // I added this line
return histogram_in_function;
}
int main() {
TH1D* histogram_in_main = get_histogram(); // will this be a dangling pointer??
delete histogram_in_main;
}
Or better yet, with smart pointers:
std::unique_ptr<TH1D> get_histogram() {
TFile filename ("input_file.root");
std::unique_ptr<TH1D> histogram_in_function( filename.Get<TH1D>("histogram_name") );
histogram_in_function->SetDirectory(nullptr); // I added this line
return histogram_in_function;
}
int main() {
std::unique_ptr<TH1D> histogram_in_main = get_histogram(); // will this be a dangling pointer??
}