wxWidgets Unsual Runtime Error

Hi guys, I have a very one unusual error with my wxWidgets program, here are the call stacks

Level Addresse    Function                                                   line                                            
0     0x00000000  ??                             ../../include/wx/list.h     1225
1     0x66f5e4cd  wxWindowBase::DestroyChildren  ../../src/common/wincmn.cpp  447
2     0x66e58040  ~wxWindow                      ../../src/msw/window.cpp     561
3     0x66f5662c  ~wxTopLevelWindowBase          ../../src/common/toplvcmn.cpp  75
4     0x66e52c0c  ~wxTopLevelWindowMSW           ../../src/msw/toplevel.cpp   638
5     0x67275952  ~wxTopLevelWindow              ../../include/wx/toplevel.h  353
6     0x66f0132a  ~wxFrameBase                   ../../src/common/framecmn.cpp  79
7     0x66e86b8c  ~wxFrame                       ../../src/msw/frame.cpp      214
8     0x00421668  ~MyFrame                       ../../Projects/Paint/main.cpp  33
9     0x66ecd12a  wxAppBase::CleanUp             ../../src/common/appcmn.cpp  146
10    0x66e13fc1  wxApp::CleanUp                 ../../src/msw/app.cpp          483
11    0x66d82fbb  wxEntryCleanup                 ../../src/common/init.cpp    399
12    0x67267367  ~wxCleanupOnExit               ../../src/common/init.cpp     118
13    0x66d831bf  wxEntryReal                    ../../src/common/init.cpp    462
14    0x66dfd4a0  wxEntry                        ../../src/msw/main.cpp       231
15    0x66dfd74a  wxEntry                        ../../src/msw/main.cpp         386
16    0x00401408  WinMain@16                     ../../Projects/Paint/main.cpp  64
17    0x00417596  main    



line 33 of main.cpp is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class MyFrame : public wxFrame
{
public:
	// Constructor
	MyFrame(const wxString& title, long style=wxDEFAULT_FRAME_STYLE);
	// Event handlers
	void OnMouseDown(wxMouseEvent& event);
	void OnMouseUp(wxMouseEvent& event);
	void OnUndo(wxCommandEvent& event);
	void OnClear(wxCommandEvent& event);
	void OnSetPenRed(wxCommandEvent& event);
	void OnSetPenBlue(wxCommandEvent& event);
	void OnSetPenGreen(wxCommandEvent& event);
	void OnSetPenCyan(wxCommandEvent& event);
	void OnSetPenBlack(wxCommandEvent& event);
	void OnOpenImage(wxCommandEvent& event);
private:
	int CurrentPen;
	bool assigned;
	bool FileLoaded;
	// This class handles events
	DECLARE_EVENT_TABLE()
};

class MyApp : public wxApp
{
    public:
		// function called at the application initialization
        virtual bool OnInit();
}; 

line 64 of main.cpp is
IMPLEMent_APP(MyApp);

please can someone help me with the errors
Last edited on
wxWidgets has its own forum and we would need to see more of the code than this little snippet. What I can see it might be related to deleting the window before trying to destroy it, hence a fault of some form. Or better stated chasing down an invalid pointer. The window shows no controls on it which most windows are comprised of layers of controls. From the call stack there is no internal control to the window, which would be most like the sub-window for the drawing area to the window.

for example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class MyFrame : public wxFrame
{
public:
        // constructor.
        MyFrame(.......)
        // events bla bla
        
private:
        wxSizerBox MyWindowSizer;  // this is for making all the controls fit in the window and I may have the name wrong.
        myDrawingSurface DrawingWindow;
        wxToolBar ColorsControl;
        wxStatusBar StatusBar;
}

MyFrame::MyFrame(...)
{
        // create the sizer
     
        // create the Drawing frame with this frame as its parent.
       

       // create the toolbar and all its links with this frame as its parent
     
       // create the statusbar.
       
       // add everything to the sizer
       // if all works out the list that the call stack is referring to would not have an invalid pointer. 
}

//////////////////////////////////////////////////////////////
class myDrawingSurface : public wxWindow
{
         // this would look like your previous attempt.
};


I hope that points you in a direction.
Last edited on
Here is all of the code in my program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*********************************************************************
 * Name:      	Paint.cpp
 * Purpose:   	Implements simple wxWidgets application with GUI.
 * Author:    	Hengjian Jia
 * Created:   	Hengjian Jia
 * Copyright: 	Hengjian Jia
 * License:   	wxWidgets license (www.wxwidgets.org)
 * 
 * Notes:		
 *********************************************************************/
 
#include <wx/wx.h>
#include <iostream>
#include <vector>


#define wxID_SET_RED 1
#define wxID_SET_BLACK 0
#define wxID_SET_BLUE 2
#define wxID_SET_CYAN 4
#define wxID_SET_GREEN 3
wxBitmap *Image;
wxClientDC *Canvas;
wxMemoryDC *Memory;
wxPen wxBLUE_PEN(*wxBLUE);
int element=-1;
using namespace std;
vector<wxPoint> Start;
vector<wxPoint> End;
vector<wxPen> Pen;
// application class
class MyFrame : public wxFrame
{
public:
	// Constructor
	MyFrame(const wxString& title, long style=wxDEFAULT_FRAME_STYLE);
	// Event handlers
	void OnMouseDown(wxMouseEvent& event);
	void OnMouseUp(wxMouseEvent& event);
	void OnUndo(wxCommandEvent& event);
	void OnClear(wxCommandEvent& event);
	void OnSetPenRed(wxCommandEvent& event);
	void OnSetPenBlue(wxCommandEvent& event);
	void OnSetPenGreen(wxCommandEvent& event);
	void OnSetPenCyan(wxCommandEvent& event);
	void OnSetPenBlack(wxCommandEvent& event);
	void OnOpenImage(wxCommandEvent& event);
private:
	int CurrentPen;
	bool assigned;
	bool FileLoaded;
};

class MyApp : public wxApp
{
    public:
		// function called at the application initialization
        virtual bool OnInit();
};

DECLARE_APP(MyApp)
IMPLEMENT_APP(MyApp)

void MyFrame::OnSetPenRed(wxCommandEvent& event)
{
	CurrentPen=wxID_SET_RED;
	assigned=true;
}
void MyFrame::OnSetPenBlue(wxCommandEvent& event)
{
	CurrentPen=wxID_SET_BLUE;
	assigned=true;
}
void MyFrame::OnSetPenGreen(wxCommandEvent& event)
{
	CurrentPen=wxID_SET_GREEN;
	assigned=true;
}
void MyFrame::OnSetPenCyan(wxCommandEvent& event)
{
	CurrentPen=wxID_SET_CYAN;
	assigned=true;
}
void MyFrame::OnSetPenBlack(wxCommandEvent& event)
{
	CurrentPen=wxID_SET_BLACK;
	assigned=true;
}
void MyFrame::OnMouseDown(wxMouseEvent& event)
{
	if(assigned==false)
		Canvas=new wxClientDC(this);
	wxPoint position=event.GetLogicalPosition(wxClientDC(this));
	Start.push_back(position);
	assigned=true;
}
void MyFrame::OnMouseUp(wxMouseEvent& event)
{
	wxPoint position=event.GetLogicalPosition(*Canvas);
	End.push_back(position);
	element++;
	if(CurrentPen==wxID_SET_RED)
		Canvas->SetPen(*wxRED_PEN);
	else if(CurrentPen==wxID_SET_BLUE)
		Canvas->SetPen(wxBLUE_PEN);
	else if(CurrentPen==wxID_SET_GREEN)
		Canvas->SetPen(*wxGREEN_PEN);
	else if(CurrentPen==wxID_SET_CYAN)
		Canvas->SetPen(*wxCYAN_PEN);
	else
		Canvas->SetPen(*wxBLACK_PEN);
	Canvas->DrawLine(Start.at(element),End.at(element));
	Pen.push_back(Canvas->GetPen());
}
void MyFrame::OnUndo(wxCommandEvent& event)
{
	if(element<=-1)
		return;
	Canvas->Clear();
	for(int i=0;i<element;i++)
	{
		Canvas->SetPen(Pen.at(i));
		Canvas->DrawLine(Start.at(i),End.at(i));
	}
	Start.pop_back();
	End.pop_back();
	element--;
}
void MyFrame::OnOpenImage(wxCommandEvent& event)
{
	if(assigned==false)
		Canvas=new wxClientDC(this);
	Memory=new wxMemoryDC;
	wxFileDialog * FileDialog = new wxFileDialog(this);
	if (FileDialog->ShowModal() == wxID_OK)
	{
		wxString FileName = FileDialog->GetPath();
		if(FileLoaded==false)
			Image=new wxBitmap;
		if(Image->LoadFile(FileName,wxBITMAP_TYPE_ANY)==false)
		{
			wxMessageDialog *Error = new wxMessageDialog(NULL, wxT("Error loading file"), wxT("Error"), wxOK | wxICON_ERROR);
			Error->ShowModal();
			delete Error;
			delete FileDialog;
			return;
		}
	}
	Canvas->Clear();
	for(;element>-1;element--)
	{
		Pen.pop_back();
		Start.pop_back();
		End.pop_back();
	}
	Memory->SelectObject(*Image);
	Canvas->Blit(50, 0, Image->GetWidth(), Image->GetHeight(), Memory, 0, 0, wxCOPY, true);
	FileLoaded=true;
	delete FileDialog;
}
void MyFrame::OnClear(wxCommandEvent& event)
{
	Canvas->Clear();
	for(;element>-1;element--)
	{
		Pen.pop_back();
		Start.pop_back();
		End.pop_back();
	}
}
bool MyApp::OnInit()
{
	// create a new frame and set it as the top most application window
	MyFrame *frame = new MyFrame(wxT("Paint"));
	// show main frame
    frame->Show(true);

	// enter the application's main loop
    return true;
}
MyFrame::MyFrame(const wxString& title, long style): 
wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(1024,800))
{
	CurrentPen=wxID_SET_BLACK;
	assigned=false;
	FileLoaded=false;
	wxInitAllImageHandlers();
	wxPanel *panel = new wxPanel(this);
	new wxButton(panel,wxID_UNDO,wxT("Undo"),wxPoint(0,0),wxSize(50,20));
	new wxButton(panel,wxID_OPEN,wxT("Open"),wxPoint(0,20),wxSize(50,20));
	new wxButton(panel,wxID_CLEAR,wxT("Clear"),wxPoint(0,40),wxSize(50,20));
	new wxButton(panel,wxID_SET_BLACK,wxT("Black"),wxPoint(0,60),wxSize(50,20));
	new wxButton(panel,wxID_SET_RED,wxT("Red"),wxPoint(0,80),wxSize(50,20));
	new wxButton(panel,wxID_SET_BLUE,wxT("Blue"),wxPoint(0,100),wxSize(50,20));
	new wxButton(panel,wxID_SET_GREEN,wxT("Green"),wxPoint(0,120),wxSize(50,20));
	new wxButton(panel,wxID_SET_CYAN,wxT("Cyan"),wxPoint(0,140),wxSize(50,20));
	panel->Connect(wxEVT_LEFT_DOWN,wxMouseEventHandler(MyFrame::OnMouseDown));
	panel->Connect(wxEVT_LEFT_UP,wxMouseEventHandler(MyFrame::OnMouseUp));
	panel->Connect(wxID_SET_RED,wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(MyFrame::OnSetPenRed));
	panel->Connect(wxID_SET_BLUE,wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(MyFrame::OnSetPenBlue));
	panel->Connect(wxID_SET_GREEN,wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(MyFrame::OnSetPenGreen));
	panel->Connect(wxID_SET_CYAN,wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(MyFrame::OnSetPenCyan));
	panel->Connect(wxID_SET_BLACK,wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(MyFrame::OnSetPenBlack));
	panel->Connect(wxID_UNDO,wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(MyFrame::OnUndo));
	panel->Connect(wxID_OPEN,wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(MyFrame::OnOpenImage));
	panel->Connect(wxID_CLEAR,wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(MyFrame::OnClear));
}



It is a paint program like the one that comes in windows, the error happens when I try to open a picture
Last edited on
Topic archived. No new replies allowed.