Invalid Memory Pointer Issue When Creating Plots

I’m using a pre-packaged piece of code to plot graphs for data on interaction rates that I have. The code should output three canvases but at the moment only one of the plots is being produced before an error occurs:

**warning:** **invalid memory pointer passed to a callee:**

gr->SetLineWidth(3);

The line in the code that the error points to is located here:

    graphname=fluxname+"_ibd_"+exptconfig;
    gr = (TGraph*)f.Get(graphname);
    gr->SetLineWidth(3);
    gr->SetLineColor(1);
    gr->Draw("same");
    leg2->AddEntry(gr,"IBD","l");

Funnily enough this code worked a couple of months ago. Since then I have reinstalled newer versions of root and Xcode so I’m not sure if that has anything to do with it. gr->SetLineWidth also appears earlier in the code and seems to work for the first plot. Any help would be greatly appreciated!

Thanks

ROOT Version: 6-22-00
Platform: MacOS Catalina
Compiler: Not Provided


Hi Robert,

can you make sure your TGraph was indeed fetched from the file:

...
gr = (TGraph*)f.Get(graphname);
if (!gr)
{
    printf("ERROR: can't fetch TGraph from TFile\n");
    return;
}
gr->SetLineWidth(3);
...

Hi Yus, thanks for your reply.

I put your code in and it made no difference to the error message that comes up. Not sure if it’s useful but here are all the lines subsequent to the error message itself:

[/Users/robertsmith/Documents/rootbuild/lib/libCling.so] cling_runtime_internal_throwIfInvalidPointer (no debug info)
[<unknown binary>] (no debug info)
[<unknown binary>] (no debug info)
[/Users/robertsmith/Documents/rootbuild/lib/libCling.so] cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (no debug info)
[/Users/robertsmith/Documents/rootbuild/lib/libCling.so] cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (no debug info)
[/Users/robertsmith/Documents/rootbuild/lib/libCling.so] cling::Interpreter::EvaluateInternal(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (no debug info)
[/Users/robertsmith/Documents/rootbuild/lib/libCling.so] cling::Interpreter::process(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cling::Value*, cling::Transaction**, bool) (no debug info)
[/Users/robertsmith/Documents/rootbuild/lib/libCling.so] cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (no debug info)
[/Users/robertsmith/Documents/rootbuild/lib/libCling.so] HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) (no debug info)
[/Users/robertsmith/Documents/rootbuild/lib/libCling.so] TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (no debug info)
[/Users/robertsmith/Documents/rootbuild/lib/libRint.so] TRint::ProcessLineNr(char const*, char const*, int*) (no debug info)
[/Users/robertsmith/Documents/rootbuild/lib/libRint.so] TRint::HandleTermInput() (no debug info)
[/Users/robertsmith/Documents/rootbuild/lib/libCore.so] TUnixSystem::CheckDescriptors() (no debug info)
[/Users/robertsmith/Documents/rootbuild/lib/libCore.so] TMacOSXSystem::DispatchOneEvent(bool) (no debug info)
[/Users/robertsmith/Documents/rootbuild/lib/libCore.so] TSystem::InnerLoop() (no debug info)
[/Users/robertsmith/Documents/rootbuild/lib/libCore.so] TSystem::Run() (no debug info)
[/Users/robertsmith/Documents/rootbuild/lib/libCore.so] TApplication::Run(bool) (no debug info)
[/Users/robertsmith/Documents/rootbuild/lib/libRint.so] TRint::Run(bool) (no debug info)
[/Users/robertsmith/Documents/rootbuild/bin/root.exe] main (no debug info)
[/usr/lib/system/libdyld.dylib] start (no debug info)
[<unknown binary>] (no debug info)
Error in <HandleInterpreterException>: Trying to access a pointer that points to an invalid memory address..
Execution of your code was aborted.
/Users/robertsmith/Documents/Project/software/snowglobes/plots/plot_event_rates.C:236:6: warning: invalid memory pointer passed to a callee:
     gr->SetLineWidth(3);
     ^~

That’s weird. I was able to reproduce the exact same error message (the stack trace and then

Error in <HandleInterpreterException>: Trying to access a pointer that points to an invalid memory address..
Execution of your code was aborted.
ROOT_prompt_2:1:1: warning: invalid memory pointer passed to a callee:
gr->SetLineWidth(3);
^~

) when opening a file and then fetching a TGraph out of it which just isn’t there. I guess I need a small reproducer to help out more.

I managed to fix it. The graph it was trying to get indeed wasn’t there when it should have been. I had to change some code in the file that makes the graphs.

Thanks a lot for your help!