Access violation upon showing frame 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
vian
In need of some credit
In need of some credit
Posts: 4
Joined: Mon Oct 18, 2021 3:33 am

Access violation upon showing frame

Post by vian »

When using the hello world sample code (https://docs.wxwidgets.org/stable/overv ... world.html) with the following code in a main class:

Code: Select all

MyApp* app = new MyApp();
app->OnInit();
the program crashes with the following error:

Exception thrown: read access violation.
**this** was nullptr.

Unhandled exception at 0x00007FFF20E0B963 (wxmsw315u_core_vc_custom.dll) in test.exe: 0xC000041D: An unhandled exception was encountered during a user callback.

From further debugging I suspect that this is due to wxWindow parent being set to NULL in the Myframe constructor:

Code: Select all

MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
    : wxFrame(NULL, wxID_ANY, title, pos, size)

However, I am unsure as to how I can resolve this error.
Any support would be greatly appreciated.
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Access violation upon showing frame

Post by doublemax »

Code: Select all

MyApp* app = new MyApp();
app->OnInit();
You're not supposed to create the wxApp instance yourself, the wx macros handle that (the wxIMPLEMENT_APP(MyApp) line in the page you used). Please check the "minimal" sample that comes with wxWidgets (in <wxdir>/samples/minimal).
Use the source, Luke!
vian
In need of some credit
In need of some credit
Posts: 4
Joined: Mon Oct 18, 2021 3:33 am

Re: Access violation upon showing frame

Post by vian »

This may be more of a compiler question, but if I remove the main method from my project how can I have it default to use wxWidget's main?

The compiler is assuming that the main method is inside my code and not wxWidget's code.

I am getting this error upon compilation:
LNK2019 unresolved external symbol main referenced in function "int __cdecl invoke_main(void)"

My CMakeLists.txt matches the list under the find_package subtitle in https://docs.wxwidgets.org/trunk/overview_cmake.html

I am using CMake version 3.8 with Visual Studio and the MSVC Compiler on windows.
WxWidgets was installed via vcpkg.
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Access violation upon showing frame

Post by doublemax »

vian wrote: Mon Oct 18, 2021 5:51 am This may be more of a compiler question, but if I remove the main method from my project how can I have it default to use wxWidget's main?
The wxIMPLEMENT_APP macro does that, too.

However, if you're under Windows and the compiler complains about missing main() (instead of WinMain()), it means you're building a console application. You need to switch /SUBSYSTEM to "Windows" in the project settings.

To make things easier, start by building the "minimal" sample using the provided make/project files.
Use the source, Luke!
vian
In need of some credit
In need of some credit
Posts: 4
Joined: Mon Oct 18, 2021 3:33 am

Re: Access violation upon showing frame

Post by vian »

Thank you!
You pointed me in the right direction.

Adding the WIN32 flag to the add_executable function in my CMakeLists.txt solved the problem.

Code: Select all

add_executable(${PROJECT_NAME} WIN32 app.cpp)
Post Reply