0
votes

I'm going to preface this question by saying that I am not entirely new to wxWidgets, though I would not consider myself all that experience either.

I'm having a problem which I have narrowed down through testing to be with the wxWidgets wxBoxSizer classes. From what I can tell, I don't think I'm doing anything wrong, and they work while the application is running; however, when the application closes and everything terminates, it seems that about a third of the time it will double-free something and seg fault. I'm writing and building on a linux system, and there doesn't seem to be any problems with anything but these sizers.

I did have many more windows within the HomeFrame class before I stripped it to search for bugs, but the code below still causes the double-free:

I've also looked around StackOverflow and other code forums to see if anyone was having a similar issue, but I couldn't find anything. I'm thinking that it may have something to do with the way I'm storing the sizer pointers in the class as members?

HomeFrame.h:

#pragma once

#include <wx/frame.h>
#include <wx/sizer.h>

namespace qzrgui
{
    class HomeFrame: public wxFrame
    {
    public:
        HomeFrame();
        ~HomeFrame();

    private:
    //  Sizers

        wxBoxSizer* _topSizer;
        wxBoxSizer* _leftSizer;
        wxBoxSizer* _rightSizer;

    //  Functions

        void _setup();
        void _createWindows();
    };
}

HomeFrame.cpp:

#include "HomeFrame.h"

namespace qzrgui
{
    HomeFrame::HomeFrame() :
    wxFrame(nullptr, wxID_ANY, "Quizzer")
    {
        _setup();
    }
    
    HomeFrame::~HomeFrame() 
    {
        
    }
    
    void HomeFrame::_createWindows() 
    {
    //  Create sizers.

        _topSizer = new wxBoxSizer(wxOrientation::wxHORIZONTAL);
        _leftSizer = new wxBoxSizer(wxOrientation::wxVERTICAL);
        _rightSizer = new wxBoxSizer(wxOrientation::wxVERTICAL);
    }
    
    void HomeFrame::_setup() 
    {
        _createWindows();
    }

}

Quizzer.h (wxApp base class):

#pragma once

#include <wx/app.h>


namespace qzrgui
{
    class Quizzer : public wxApp
    {
    public:
        virtual bool OnInit();
    };
};


Quizzer.cpp:

#include "Quizzer.h"

wxIMPLEMENT_APP(qzrgui::Quizzer);

#include "frames/HomeFrame.h"

namespace qzrgui
{
    bool Quizzer::OnInit()
    {
        wxFrame* frame = new HomeFrame();
        frame->Show(true);

        return true;
    }
}

1
do you need to store the sizers as members? Also, which wx version do you use and under which platform? - Igor
Your class violates the rule of 3/5/0: https://en.cppreference.com/w/cpp/language/rule_of_three - drescherjm
I assume you have a delete somewhere in code you are not showing. - drescherjm
@Igor I guess I don't need to, but I was doing so for organizational purposes while I was building the class. I didn't think it would change anything. I'm using wxWidgets 3.1.4 on Ubuntu Linux. - Andrew Huffman
@drescherjm I know, this class is just for demonstration purposes. I had the destructor defined because I was messing around with termination stuff in it to see if I could wrap my head around what's going on. - Andrew Huffman

1 Answers

0
votes

There is nothing in the code shown here which could result in a crash. You do definitely have (three) memory leak(s) in your code, as you create 3 sizers not associated with any window or another sizer and never delete them, but, while wrong, this can't result in a crash.

There is also not enough information to diagnose what you're seeing. As always with a crash, please build your code with debug information and run it under debugger to see what exactly happens. If you're under a Unix-like system (or even under MSW with the latest MSVS 2019 version), you should also compile with address sanitizer enabled to find the real problem before the crash happens.