Program crashes on initialization

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
ManlishPotato
In need of some credit
In need of some credit
Posts: 3
Joined: Sat Nov 27, 2021 6:59 pm

Program crashes on initialization

Post by ManlishPotato »

Hi!
I have been trying to chase out this random bug, and not sure where to look now, so would be great with some more experienced eye's on it.
I have this wxWidget c++ VS app that will randomly throw an exception at initialization. In the "OnInit" function it will randomly throw an exception in either memcpy.asm or init.cpp. That said the throw is always the same:
"Exception thrown at 0x7B0232AE (vcruntime140.dll) in SerialViewer.exe: 0xC0000005: Access violation reading location 0xA7C28FDB."
Here is what i believe to be the relevant code, please tell me if there is anything else you'd like to see.

cApp.h:

Code: Select all

#pragma once

#include <wx/wxprec.h>
#include "cMain.h"

class cApp : public wxApp
{
	public:
		cMain* m_frame1=nullptr;

	public:
		virtual bool OnInit();
};
cApp.cpp:

Code: Select all

#include "cApp.h"

wxIMPLEMENT_APP(cApp);

bool cApp::OnInit()
{	
	m_frame1=new cMain();
	m_frame1->SetBackgroundColour(wxColour(33,37,43));
	m_frame1->Show(true);	

	return true;
}
cMain constructor:

Code: Select all

cMain::cMain() : wxFrame(nullptr,wxID_ANY,"SerialViewer2.0",wxPoint(30,30),wxSize(850,800))
{	
	wxPanel *topBar=new wxPanel(this,wxID_ANY,wxDefaultPosition,wxDefaultSize);
	topBar->SetBackgroundColour(secondaryColour1);
	wxPanel *mainPanel=new wxPanel(this,wxID_ANY,wxDefaultPosition,wxDefaultSize);
	mainPanel->SetBackgroundColour(secondaryColour1);
	wxPanel *sideBar=new wxPanel(this,wxID_ANY,wxDefaultPosition,wxDefaultSize);
	sideBar->SetBackgroundColour(secondaryColour1);
	wxPanel *bottomBar=new wxPanel(this,wxID_ANY,wxDefaultPosition,wxDefaultSize);
	bottomBar->SetBackgroundColour(secondaryColour1);

	wxSizer *leftGroupSz=new wxBoxSizer(wxVERTICAL);
	leftGroupSz->Add(topBar,0,wxEXPAND | wxALL,5);
	leftGroupSz->Add(mainPanel,1,wxEXPAND | wxLEFT | wxBOTTOM | wxRIGHT,5);

	wxSizer *mainGroupSz=new wxBoxSizer(wxHORIZONTAL);
	mainGroupSz->Add(leftGroupSz,1,wxEXPAND,0);
	mainGroupSz->Add(sideBar,0,wxEXPAND | wxTOP | wxRIGHT | wxBOTTOM,5);

	wxBoxSizer *allSz=new wxBoxSizer(wxVERTICAL);
	allSz->Add(mainGroupSz,1,wxEXPAND,0);
	allSz->Add(bottomBar,0,wxEXPAND | wxLEFT | wxBOTTOM | wxRIGHT,5);
	
	txtWrite=new wxTextCtrl(topBar,wxID_ANY,"",wxDefaultPosition,wxDefaultSize,wxTE_PROCESS_ENTER);
	txtWrite->SetBackgroundColour(secondaryColour2);
	txtWrite->SetForegroundColour(txtColour1);
	txtWrite->SetFont(font1);
	wxSizer *txtWriteSz=new wxBoxSizer(wxVERTICAL);
	txtWriteSz->Add(txtWrite,0,wxEXPAND | wxTOP | wxBOTTOM,5);
	topBar->SetSizerAndFit(txtWriteSz);

	txtRead=new wxTextCtrl(mainPanel,wxID_ANY,"",wxDefaultPosition,wxSize(500,450),wxTE_MULTILINE | wxTE_READONLY);
	txtRead->SetFont(font2);
	txtRead->SetBackgroundColour(secondaryColour2);
	txtRead->SetForegroundColour(txtColour1);
	wxSizer *txtReadSz=new wxBoxSizer(wxVERTICAL);
	txtReadSz->Add(txtRead,1,wxEXPAND,0);
	mainPanel->SetSizer(txtReadSz);

	btnConnect=new wxButton(sideBar,wxID_ANY,"Connect",wxDefaultPosition,wxSize(100,50));
	cbxBaud=new wxComboBox(sideBar,wxID_ANY,defBaudRate,wxDefaultPosition,wxSize(130,25),cbxBaudNum,cbxBaudSel,wxCB_READONLY);
	cbxPort=new wxComboBox(sideBar,wxID_ANY,"",wxDefaultPosition,wxSize(130,25));
	chkReset=new wxCheckBox(sideBar,wxID_ANY,"Auto DTR Reset",wxDefaultPosition,wxSize(100,25));
	wxStaticText *tlaBaud=new wxStaticText(sideBar,wxID_ANY,"Baud Rate",wxDefaultPosition,wxDefaultSize);
	wxStaticText *tlaPort=new wxStaticText(sideBar,wxID_ANY,"Port Select",wxDefaultPosition,wxDefaultSize);
	tlaBaud->SetForegroundColour(txtColour1);
	tlaPort->SetForegroundColour(txtColour1);
	chkReset->SetForegroundColour(txtColour1);	
	wxSizer *mainSettSz=new wxBoxSizer(wxVERTICAL);	
	mainSettSz->Add(btnConnect,0,wxEXPAND | wxBOTTOM,10);
	mainSettSz->Add(tlaBaud,0,wxEXPAND,0);
	mainSettSz->Add(cbxBaud,0,wxEXPAND | wxBOTTOM,10);
	mainSettSz->Add(tlaPort,0,wxEXPAND,0);
	mainSettSz->Add(cbxPort,0,wxEXPAND | wxBOTTOM,10);
	mainSettSz->Add(chkReset,0,wxEXPAND | wxTOP | wxBOTTOM,10);

	tlaConnected=new wxStaticText(sideBar,wxID_ANY,"Unconnected",wxDefaultPosition,wxDefaultSize,wxALIGN_CENTER_HORIZONTAL | wxST_NO_AUTORESIZE);
	tlaConnected->SetForegroundColour(txtColour2);	
	wxSizer *tlaConnectedAlignSz=new wxBoxSizer(wxHORIZONTAL);
	tlaConnectedAlignSz->Add(tlaConnected,1,wxALIGN_BOTTOM,0);

	wxSizer *sideBarSz=new wxBoxSizer(wxVERTICAL);
	sideBarSz->Add(mainSettSz,1,wxEXPAND | wxLEFT | wxTOP | wxRIGHT,5);
	sideBarSz->Add(tlaConnectedAlignSz,0,wxEXPAND,0);
	sideBar->SetSizerAndFit(sideBarSz);

	btnClear=new wxButton(bottomBar,wxID_ANY,"Clear",wxDefaultPosition,wxDefaultSize);	
	chkScroll=new wxCheckBox(bottomBar,wxID_ANY,"Auto Scroll",wxDefaultPosition,wxDefaultSize);
	chkClear=new wxCheckBox(bottomBar,wxID_ANY,"Auto Clear",wxDefaultPosition,wxDefaultSize);
	chkScroll->SetForegroundColour(txtColour1);
	chkClear->SetForegroundColour(txtColour1);
	wxSizer *otherSettSz=new wxBoxSizer(wxHORIZONTAL);
	otherSettSz->Add(btnClear,0,wxEXPAND | wxALL,5);
	otherSettSz->Add(chkScroll,0,wxEXPAND | wxALL,5);
	otherSettSz->Add(chkClear,0,wxEXPAND | wxALL,5);

	wxSizer *bottomBarSz=new wxBoxSizer(wxHORIZONTAL);
	bottomBarSz->Add(otherSettSz,1,wxALIGN_LEFT,0);
	bottomBar->SetSizerAndFit(bottomBarSz);	

	this->SetSizer(allSz);
	this->SetMinSize(wxSize(350,360));

	chkReset->SetValue(true);
	chkScroll->SetValue(true);
	chkClear->SetValue(true);

	updateComPorts();

	menubar=new wxMenuBar;
	file=new wxMenu;
	file->Append(wxID_SETUP,wxT("&Port settings"));
	file->Append(wxID_ABOUT,wxT("&About"));
	file->Append(wxID_REFRESH,wxT("Refresh Ports"));
	file->Append(wxID_PRINT_SETUP,wxT("List Settings"));
	menubar->Append(file,wxT("&File"));
	SetMenuBar(menubar);	

	cbxBaud->Bind(wxEVT_COMBOBOX,&cMain::baudChange,this);
	cbxPort->Bind(wxEVT_COMBOBOX,&cMain::comPortChange,this);
	btnConnect->Bind(wxEVT_BUTTON,&cMain::serialConnect,this);
	btnClear->Bind(wxEVT_BUTTON,&cMain::clearReadTxt,this);
	txtWrite->Bind(wxEVT_TEXT_ENTER,&cMain::onTxtWriteSend,this);
	chkScroll->Bind(wxEVT_CHECKBOX,&cMain::chkScrollChange,this);

	Bind(wxEVT_MENU,&cMain::menuSettings,this,wxID_SETUP);
	Bind(wxEVT_MENU,&cMain::menuAbout,this,wxID_ABOUT);
	Bind(wxEVT_MENU,&cMain::menuUpdatePorts,this,wxID_REFRESH);
	Bind(wxEVT_MENU,&cMain::menuListSettings,this,wxID_PRINT_SETUP);
	
	Bind(PT_WRITE_EVT,&cMain::printReadDataEvt,this,ptWriteEvtId);
	Bind(SC_ERROR_EVT,&cMain::serialThreadErrorEvt,this,scErrorEvtId);
}
I built the text sample as well as a program with only the important bits and have not been able to reproduce the problem outside the original one.

Windows 10
Visual studio 2019
wxWidgets 3.1.5

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

Re: Program crashes on initialization

Post by doublemax »

This is far too much code to be debugged by just looking at it. Create a debug build, so you can see the exact line where it crashes.
Use the source, Luke!
ManlishPotato
In need of some credit
In need of some credit
Posts: 3
Joined: Sat Nov 27, 2021 6:59 pm

Re: Program crashes on initialization

Post by ManlishPotato »

I haven't got it to crash in a debug build, only in release. I assume because of optimizations. When debugging in release it will throw exception after:
m_frame1=new cMain(); in OnInit in which case the exception will be thrown in memcpy.asm
Or after it returns true in OnInit, then it will be thrown in init.cpp
Which one of these is thrown is random, but there is usually a string of one or the other after each other.

I release that this might be a pretty general bug and is like looking for a needle in a haystack, so bare with me.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Program crashes on initialization

Post by doublemax »

Try enabling debug info for the release build. When a program crashes in release, but not in debug, in most cases it's caused by an uninitialized variable (which will be NULL in debug, but contain random stuff in release). If that doesn't help, try commenting out parts of the code until you've narrowed it down.
Use the source, Luke!
ManlishPotato
In need of some credit
In need of some credit
Posts: 3
Joined: Sat Nov 27, 2021 6:59 pm

Re: Program crashes on initialization

Post by ManlishPotato »

Ok I will try that when I get a chance, but i mean with the way it's crashing it makes me think that the issue lies in the cApp class, but I'm practically doing nothing there other than opening the window. I don't remember exactly where it fails in init.cpp of my hand, but I'll post that later once I have some time since that might be important.
Post Reply