Error filling vector in gui event handler

Hello,

I have made a gui program based on the tutorial gui/guitest.C.

In the main class TestMainFrame, I have declared a vector, vector peaks. I have a histogram in an embedded canvas that I have connected to processedEvents with:

    gVirtualX->SetInputFocus(fEc1->GetContainer()->GetId());
    c1->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", "TestMainFrame",
          fHpx, "ExecuteEvent(Int_t,Int_t,Int_t,TObject*)");

Where c1 is the canvas, fHpx is the plotted histogram and fEc1 is the embedded canvas. The function ExecuteEvent decides if the event is a keypress or not, and fills a vector with the X-axis location whenever the ‘p’ key is pressed. However, in the function below, it seems to think the vector has not been allocated or something and so crashes when I try to do, peaks.push_back(last_x) or gives me some giant value when I call peak.size(). The function is:


void TestMainFrame::ExecuteEvent(Int_t event, Int_t px, Int_t py, TObject *sel){
          // some magic to get the coordinates...
         float x = gPad->AbsPixeltoX(px);
        float y = gPad->AbsPixeltoY(py);
           x = gPad->PadtoX(x);
           y = gPad->PadtoY(y);
           static float last_x;
           static float last_y;

           if(event!=kKeyPress)
             {
               last_x=x;
               last_y=y;
               //return;
             }
          if(event == kKeyPress){
            switch(px){
              case 'b':
                cout << "background" << endl;
                break;
                case 'p':
                  cout << "Peak Select" << endl;
                  peaks.push_back(last_x);
                   cout << last_x << "    " << last_y << " " << peaks.size() << endl;
                  break;
              };
            }
        }

The vector is declared as a private member of the TestMainFrame class, while this function is a public member. Following the convention of the guitest.C tutorial I based most things on. If I ask any of my other functions handing signal/slots, like button presses in the gui or menu actions, they all know the peak vector exists and can fill and give me the correct size, but there is something with this particular function that it won’t. Since it is handling the keyboard presses and runs on the ProcessedEvents() I wasn’t sure if they were related.

This is the output when this function is run and I press ‘p’:

libc++abi.dylib: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc

The code is attached,
guitest.C (28.0 KB)

Thanks,


_ROOT Version:6.14.06
_Platform:MacOSX 10.14.2
Compiler: Not Provided


You code has errors and cannot work… For example, this is wrong:

    c1->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", "TestMainFrame",
          fHpx, "ExecuteEvent(Int_t,Int_t,Int_t,TObject*)");

fHpx is not a TestMainFrame

And this:

   fComboR = new TGComboBox(fF2, 200);

Should be:

   fComboR = new TGComboBox(fF3, 200);

Then I can reproduce the problem and I’ll check what’s going wrong

I have changed the second item you mention, but the histogram fHpx is defined as a member of the TestMainFrame class, so I thought it was valid there. I am really uncertain how else to write that line.

The code as is, without the corrections you listed, runs fine for me except for the fact that the function, ExecuteEvent(), has no knowledge of anything from the TestMainFrame class. I got around the first problem by declaring the vector as a global variable outside of the class but there are other thing (methods, other variables) in the TestMainFrame class that I need/want access to inside of ExecuteEvent(). So my work around is far from ideal.

Thank you for the help, it is much appreciated.

Hello,

I discovered a typo that was the cause of the issue. I think it relates back to the suggestion you provided in my other post. The ExecuteEvent function now can see everything in the TestMainFrame class.

Thank you for your time and help!

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