# Invalid Memory Pointer Issue When Creating Plots

**URL:** <https://root-forum.cern.ch/t/invalid-memory-pointer-issue-when-creating-plots/40994>\
**Category:** Newbie\
**Created:** [August 14, 2020, 3:51am UTC](https://root-forum.cern.ch/t/invalid-memory-pointer-issue-when-creating-plots/40994 "2020-08-14T03:51:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RobertSmith](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/6a8cbe/32.png) [@RobertSmith](https://root-forum.cern.ch/u/RobertSmith)\
**Post date:** [August 14, 2020, 3:51am UTC](https://root-forum.cern.ch/t/invalid-memory-pointer-issue-when-creating-plots/40994/1 "2020-08-14T03:51:20Z")

</div>

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:

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

gr->SetLineWidth(3);

```

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

```auto
    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

* * *

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [August 14, 2020, 1:43pm UTC](https://root-forum.cern.ch/t/invalid-memory-pointer-issue-when-creating-plots/40994/2 "2020-08-14T13:43:19Z")

</div>

Hi Robert,

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

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

```

---

<div class="post-metadata">

**Author:** ![RobertSmith](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/6a8cbe/32.png) [@RobertSmith](https://root-forum.cern.ch/u/RobertSmith)\
**Post date:** [August 14, 2020, 4:59pm UTC](https://root-forum.cern.ch/t/invalid-memory-pointer-issue-when-creating-plots/40994/3 "2020-08-14T16:59:50Z")

</div>

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:

```auto
[/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);
     ^~

```

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [August 14, 2020, 5:19pm UTC](https://root-forum.cern.ch/t/invalid-memory-pointer-issue-when-creating-plots/40994/4 "2020-08-14T17:19:06Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![RobertSmith](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/6a8cbe/32.png) [@RobertSmith](https://root-forum.cern.ch/u/RobertSmith)\
**Post date:** [August 18, 2020, 2:54pm UTC](https://root-forum.cern.ch/t/invalid-memory-pointer-issue-when-creating-plots/40994/5 "2020-08-18T14:54:40Z")

</div>

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!
