# Get seg fault when trying to call TGMainFrame::Cleanup() then reuse the frame

**URL:** <https://root-forum.cern.ch/t/get-seg-fault-when-trying-to-call-tgmainframe-cleanup-then-reuse-the-frame/59746>\
**Category:** Newbie\
**Tags:** root\
**Created:** [June 22, 2024, 11:23am UTC](https://root-forum.cern.ch/t/get-seg-fault-when-trying-to-call-tgmainframe-cleanup-then-reuse-the-frame/59746 "2024-06-22T11:23:19Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 22, 2024, 11:23am UTC](https://root-forum.cern.ch/t/get-seg-fault-when-trying-to-call-tgmainframe-cleanup-then-reuse-the-frame/59746/1 "2024-06-22T11:23:20Z")

</div>

Dear experts

I want to make a GUI which after I click a button it process something and then clean up everything on the main frame, then add new things on it.  
Here is part of code:

```auto
EventDisplay::EventDisplay() {
        // Create main frame
        mainFrame = new TGMainFrame(gClient->GetRoot(), 800, 600);

        // Add label
        TGLabel* label = new TGLabel(mainFrame, "Please select number of entries that you want to preprocess for T0 and ADC pedestal value");
        mainFrame->AddFrame(label, new TGLayoutHints(kLHintsCenterX, 5, 5, 10, 10));

        // Add number entry
        preNumEventEntry = new TGNumberEntry(mainFrame, 0, 9, -1, TGNumberFormat::kNESInteger);
        mainFrame->AddFrame(preNumEventEntry, new TGLayoutHints(kLHintsCenterX, 5, 5, 10, 10));

        // Add process button
        processButton = new TGTextButton(mainFrame, "&Process");
        processButton->Connect("Clicked()", "EventDisplay", this, "Preprocess()");
        mainFrame->AddFrame(processButton, new TGLayoutHints(kLHintsCenterX, 5, 5, 10, 10));

        mainFrame->SetWindowName("Preprocess Entries");
        mainFrame->MapSubwindows();
        mainFrame->Resize(mainFrame->GetDefaultSize());
        mainFrame->MapWindow();
}

void EventDisplay::Preprocess() {
        int number = preNumEventEntry->GetIntNumber();
        std::cout << "Processing " << number << " entries..." << std::endl;
        
        mainFrame->Cleanup();

        // Call SetupDisplay to create new elements
        SetupDisplay();
}

void EventDisplay::SetupDisplay() {
        // Create embedded canvas
        embeddedCanvas = new TRootEmbeddedCanvas("embeddedCanvas", mainFrame, 800, 400);
        mainFrame->AddFrame(embeddedCanvas, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
        canvas = embeddedCanvas->GetCanvas();

        // Set new canvas size
        canvas->SetCanvasSize(800, 600);

 // Set window name and map subwindows, main window
        mainFrame->SetWindowName("Event Display");
        mainFrame->MapSubwindows();
        mainFrame->Resize(mainFrame->GetDefaultSize());
        mainFrame->MapWindow();
}

```

I found that when I add the line `mainFrame->Cleanup();` it give the seg fault when I click the button:

```auto
 ***Break*** segmentation violation
[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)
[/Users/siyuan/Physics/root/install/lib/libGui.so] TGButton::EmitSignals(bool) (no debug info)
[/Users/siyuan/Physics/root/install/lib/libGui.so] TGButton::HandleButton(Event_t*) (no debug info)
[/Users/siyuan/Physics/root/install/lib/libGui.so] TGFrame::HandleEvent(Event_t*) (no debug info)
[/Users/siyuan/Physics/root/install/lib/libGui.so] TGClient::HandleEvent(Event_t*) (no debug info)
[/Users/siyuan/Physics/root/install/lib/libGui.so] TGClient::ProcessOneEvent() (no debug info)
[/Users/siyuan/Physics/root/install/lib/libGui.so] TGInputHandler::Notify() (no debug info)
[/Users/siyuan/Physics/root/install/lib/libCore.so] TMacOSXSystem::DispatchOneEvent(bool) (no debug info)
[/Users/siyuan/Physics/root/install/lib/libCore.so] TSystem::InnerLoop() (no debug info)
[/Users/siyuan/Physics/root/install/lib/libCore.so] TSystem::Run() (no debug info)
[/Users/siyuan/Physics/root/install/lib/libCore.so] TApplication::Run(bool) (no debug info)
[/Users/siyuan/Physics/comet/crt/ana/EventDisplay/build/EventDisplay] main (no debug info)
[/usr/lib/dyld] start (no debug info)

```

What did I do wrong?

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 22, 2024, 11:47am UTC](https://root-forum.cern.ch/t/get-seg-fault-when-trying-to-call-tgmainframe-cleanup-then-reuse-the-frame/59746/2 "2024-06-22T11:47:13Z")

</div>

Here is a little update:  
I add one line:

```auto
preNumEventEntry->GetNumberEntry()->Connect("ReturnPressed()", "TGTextButton", processButton, "Clicked()");

```

And I found that when I use Enter key to trigger `Preprocess()` there is no seg fault, only if I press the button there will be seg fault.

But this makes no sense because when I press the enter key it should be the same as I click the “process” button…

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [June 22, 2024, 2:46pm UTC](https://root-forum.cern.ch/t/get-seg-fault-when-trying-to-call-tgmainframe-cleanup-then-reuse-the-frame/59746/3 "2024-06-22T14:46:43Z")

</div>

Hi,

Adding in the loop @linev and @couet .

Best,  
D

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 24, 2024, 5:52am UTC](https://root-forum.cern.ch/t/get-seg-fault-when-trying-to-call-tgmainframe-cleanup-then-reuse-the-frame/59746/4 "2024-06-24T05:52:07Z")

</div>

The problem you’re encountering is related to the GUI. @bellenot is the expert in this area.

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [June 25, 2024, 10:35am UTC](https://root-forum.cern.ch/t/get-seg-fault-when-trying-to-call-tgmainframe-cleanup-then-reuse-the-frame/59746/5 "2024-06-25T10:35:29Z")

</div>

Thank you, @bellenot could you please give some suggestions?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 25, 2024, 10:54am UTC](https://root-forum.cern.ch/t/get-seg-fault-when-trying-to-call-tgmainframe-cleanup-then-reuse-the-frame/59746/6 "2024-06-25T10:54:46Z")

</div>

@bellenot is on holidays right now. He will reply when he will be back.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 3, 2024, 8:22am UTC](https://root-forum.cern.ch/t/get-seg-fault-when-trying-to-call-tgmainframe-cleanup-then-reuse-the-frame/59746/7 "2024-07-03T08:22:42Z")

</div>

You cannot call `mainFrame->Cleanup();` in the slot method (`EventDisplay::Preprocess()`), it will delete the widget being currently in use (the signal emitter). Here is a possible solution:

```cpp
void EventDisplay::Preprocess()
{
   TTimer::SingleShot(300, "EventDisplay", this, "Process()");
}

void EventDisplay::Process()
{
   int number = preNumEventEntry->GetIntNumber();
   std::cout << "Processing " << number << " entries..." << std::endl;
   mainFrame->Cleanup();
   // Call SetupDisplay to create new elements
   SetupDisplay();
}

```

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [July 3, 2024, 11:16am UTC](https://root-forum.cern.ch/t/get-seg-fault-when-trying-to-call-tgmainframe-cleanup-then-reuse-the-frame/59746/8 "2024-07-03T11:16:53Z")

</div>

Thank you, can I move the `mainFrame->Cleanup()` to the function `SetupDisplay()` if I don’t want a new function?

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 3, 2024, 11:18am UTC](https://root-forum.cern.ch/t/get-seg-fault-when-trying-to-call-tgmainframe-cleanup-then-reuse-the-frame/59746/9 "2024-07-03T11:18:24Z")

</div>

Not if `EventDisplay::SetupDisplay()` is called by `EventDisplay::Preprocess()`

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [July 3, 2024, 11:19am UTC](https://root-forum.cern.ch/t/get-seg-fault-when-trying-to-call-tgmainframe-cleanup-then-reuse-the-frame/59746/10 "2024-07-03T11:19:39Z")

</div>

Ah, I see, that means I need to wait until `EventDisplay::Preprocess()` return then I can call `mainFrame->Cleanup()`

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [July 3, 2024, 11:21am UTC](https://root-forum.cern.ch/t/get-seg-fault-when-trying-to-call-tgmainframe-cleanup-then-reuse-the-frame/59746/11 "2024-07-03T11:21:50Z")

</div>

Sorry but why return key does not trigger seg fault? (refer to my 2nd post)

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 3, 2024, 11:24am UTC](https://root-forum.cern.ch/t/get-seg-fault-when-trying-to-call-tgmainframe-cleanup-then-reuse-the-frame/59746/12 "2024-07-03T11:24:52Z")

</div>

I don’t know, I’ll check whenever I have some time

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 3, 2024, 1:19pm UTC](https://root-forum.cern.ch/t/get-seg-fault-when-trying-to-call-tgmainframe-cleanup-then-reuse-the-frame/59746/13 "2024-07-03T13:19:38Z")

</div>

So I don’t know why it works with the number entry… But anyway, now you know how to fix the issue, right?

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 3, 2024, 1:42pm UTC](https://root-forum.cern.ch/t/get-seg-fault-when-trying-to-call-tgmainframe-cleanup-then-reuse-the-frame/59746/14 "2024-07-03T13:42:02Z")

</div>

If you don’t want to create a new method, just do the following:

```auto
void EventDisplay::Preprocess()
{
   int number = preNumEventEntry->GetIntNumber();
   std::cout << "Processing " << number << " entries..." << std::endl;
   TTimer::SingleShot(300, "EventDisplay", this, "SetupDisplay()");
}

void EventDisplay::SetupDisplay()
{
   mainFrame->Cleanup();
   // Create embedded canvas
   embeddedCanvas = new TRootEmbeddedCanvas("embeddedCanvas", mainFrame, 800, 400);
   mainFrame->AddFrame(embeddedCanvas, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
   canvas = embeddedCanvas->GetCanvas();
   ...

```
