Read access violation when closing my app Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
User avatar
lionking
Earned a small fee
Earned a small fee
Posts: 17
Joined: Sun Apr 07, 2019 6:07 pm

Read access violation when closing my app

Post by lionking » Fri Apr 19, 2019 1:08 am

When closing my application a runtime error occurs.
After debugging I found "Read access violation".

Image

myCode:

Code: Select all

class myApp: public wxApp {
public:
    virtual bool OnInit();
    virtual int OnExit();
    wxSingleInstanceChecker *AppSingleInstance;
    MainFrm *mainWindow;
};

bool myApp::OnInit() {
    AppSingleInstance = new wxSingleInstanceChecker();
    if (AppSingleInstance->IsAnotherRunning()) {
        wxLogError("Another instance is already running, aborting.");
        delete AppSingleInstance;
        AppSingleInstance = NULL;
        return false;
    }
    mainWindow = new MainFrm(NULL, "myApp");
    SetTopWindow(mainWindow);
    mainWindow->Show();
    return true;
}

int myApp::OnExit() {
    delete AppSingleInstance;
    delete mainWindow;
    return 0;
}

IMPLEMENT_APP(myApp);
Why did that access violation happen?

New Pagodi
Super wx Problem Solver
Super wx Problem Solver
Posts: 327
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

Re: Read access violation when closing my app

Post by New Pagodi » Fri Apr 19, 2019 1:34 am

You don't need to delete mainWindow. As part of application termination, wxWidgets destroys all top level widows.

User avatar
lionking
Earned a small fee
Earned a small fee
Posts: 17
Joined: Sun Apr 07, 2019 6:07 pm

Re: Read access violation when closing my app

Post by lionking » Sat Apr 20, 2019 1:18 pm

Thank you.
I hope you give me a page from the documentation which confirms your talking.

New Pagodi
Super wx Problem Solver
Super wx Problem Solver
Posts: 327
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

Re: Read access violation when closing my app

Post by New Pagodi » Sat Apr 20, 2019 4:09 pm

On the page for wxTopLevelWindow, they state
Note that the instances of wxTopLevelWindow are managed by wxWidgets in the internal top level window list.

User avatar
doublemax
Moderator
Moderator
Posts: 14597
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Read access violation when closing my app

Post by doublemax » Sat Apr 20, 2019 5:17 pm

lionking wrote:
Sat Apr 20, 2019 1:18 pm
Thank you.
I hope you give me a page from the documentation which confirms your talking.
I don't think this is explicitly written anywhere. It's only stated implicitly by the fact that the default close behavior of a wxFrame is to destroy itself.
https://docs.wxwidgets.org/trunk/overvi ... on_default
Use the source, Luke!

Kvaz1r
I live to help wx-kind
I live to help wx-kind
Posts: 181
Joined: Tue Jun 07, 2016 1:07 pm

Re: Read access violation when closing my app

Post by Kvaz1r » Sat Apr 20, 2019 6:36 pm

lionking wrote:
Sat Apr 20, 2019 1:18 pm
Thank you.
I hope you give me a page from the documentation which confirms your talking.
It's popular question so wxWidgets developers puted answer into special FAQ.
Who deletes all the windows I create?
All windows and controls in wxWidgets programs are created using new but you shouldn’t use delete to free them. This doesn’t result in memory leaks because wxWidgets takes care of this: all objects derived from wxWindow will be deleted automatically by the library when the corresponding real, on screen, window is destroyed. Thus, the top level window objects are deleted when you call Close() or Destroy() and all the child windows are deleted just before the parent window is. More details about the top level windows can be found in the “Window deletion overview” in the manual.

Post Reply