When I enter a wxDialog and I focus one wxTextCtrl nothing bad happens but as soon as I modify something in that text, like deleting one character, the application crashes with this message:
"debug assertion failed: map/set iterator not dereferencable"
The last method I found out is called before crashing is this one:
bool wxWindowMSW::HandleKillFocus(WXHWND hwnd)
{
#if wxUSE_CARET
// Deal with caret
if ( m_caret )
{
m_caret->OnKillFocus();
}
#endif // wxUSE_CARET
#if wxUSE_TEXTCTRL
// If it's a wxTextCtrl don't send the event as it will be done
// after the control gets to process it.
wxTextCtrl *ctrl = wxDynamicCastThis(wxTextCtrl);
if ( ctrl )
{
return false;
}
#endif
// Don't send the event when in the process of being deleted. This can
// only cause problems if the event handler tries to access the object.
if ( m_isBeingDeleted )
{
return false;
}
wxFocusEvent event(wxEVT_KILL_FOCUS, m_windowId);
event.SetEventObject(this);
// wxFindWinFromHandle() may return NULL, it is ok
event.SetWindow(wxFindWinFromHandle(hwnd));
return GetEventHandler()->ProcessEvent(event);
}
While running the application, this method is called many times; ctrl having a value of 0x00000000 all the time and therefor not returning false in the first IF clause.
While being inside the dialog and modifying the text, the value of ctrl changes to a real value 0x031194b0; then it enters the IF clause, returns false, and crashes.
The problem came from another code modification that I still do not find how it could have this effect. Internal behaviour of wxWidgets library maybe?
Related
I'm writing a reparenting window manager in XCB and C++:http://ix.io/3yNo
At the moment it works pretty well, but occasionally when I close a window, all the windows of that application close because the process exits with a BadWindow. For example if I have a couple xfce4-terminal windows open, all managed by one process,and I close one, occasionally the application will close and I will get a BadWindow (invalid window parameter) error (in the app, not my wm). The very interesting thing is that this is not reproducible but kind of rare, probably a race condition between reporting the error and closing the window due to X11's asynchoronous nature.I have no clue where to begin debugging this, any tips?I kind of suspect it might be something in the Unmap O
Your link contains almost 500 lines of code. I am not going to try to fully understand that. Instead, I'll just randomly guess.
auto window_manager::handle_unmap_notify(xcb_unmap_notify_event_t *ev) -> void {
if (unmap_ignore > 0) {
unmap_ignore--;
return;
}
client *cl = nullptr;
size_t idx = 0;
for (client &c : clients) {
if (c.window == ev->window) {
cl = &c;
break;
}
idx++;
}
if (not cl)
return;
xcb_destroy_window(conn, cl->frame);
clients.erase(clients.begin() + idx);
}
You are destroying windows that are not yours. When the owner of the window accesses it the next time, it will get a BadWindow error.
Instead, you should check a window's WM_PROTOCOLS property and check for WM_DELETE_WINDOW. If that is present, you are supposed to send a WM_DELETE_WINDOW message to the window. See ICCCM ยง 4.2.8.1: https://tronche.com/gui/x/icccm/sec-4.html#s-4.2.8.1
I'm writing a Game Engine just for practice but I still stuck with the first chellange. The Window Manager.
https://github.com/thebenius/SDL
I've created a GitHub Repo to show you the code but don't worry. Its not much. But I absolutely don't know where is my mistake.
In the Code I create three Windows and I Manage the input for SDL_QUIT to stop the game loop and for SDL_WINDOWEVENT_CLOSE to close the windows.
Everything works fine until the last window is closed. As far as I know, now the SDL_QUIT Event must be emitted by SDL. But the Gameloop goes on.
I think I maybe have a kind of memory leak and there is still a windows saved. But I checked the window stack (Window::windows hashmap) it is empty. And also the variables in main are cleared.
I also tried to additionally clear the window and renderer variable in the hash map
Window::~Window() {
// Delete Window and Renderer
SDL_DestroyRenderer(Window::windows[this->windowID]->renderer);
SDL_DestroyWindow(Window::windows[this->windowID]->window);
Window::windows[this->windowID]->renderer = nullptr;
Window::windows[this->windowID]->window = nullptr;
// Delete Window from map
Window::windows.erase(this->windowID);
// Delete Window and Renderer
SDL_DestroyRenderer(this->renderer);
SDL_DestroyWindow(this->window);
// Reset Pointer
this->renderer = nullptr;
this->window = nullptr;
Nothing worked.
I'm new in C++ and SDL. I hope you can help me out.
Thank you o11c,
Your answer was the riddles solution.
I just put SDL_Quit() out of the Destructor. This obviously blocked the Event Handler to catch SDL_QUIT. So I put it to the constructor in atexit()
After that (don't know why before not) I got an Segfault when deleting the window pointer in main. I deleted that and just set them all to nullptr.
Now the WindowManager works properly. Thank you for your help
I think SDL_QUIT is only an hook called if you call SDL_Quit(), to give the user the opportunity to do some quit stuff, the manual:
You should call this function even if you have already shutdown each initialized subsystem with SDL_QuitSubSystem(). It is safe to call this function even in the case of errors in initialization
You can use this function with atexit() to ensure that it is run when your application is shutdown, but it is not wise to do this from a library or other dynamically loaded code
To catch a window close event see SDL_WindowEvent, SDL_WINDOWEVENT and SDL_WINDOWEVENT_CLOSE, the id of the closing window is given as argument.
* \file SDL_quit.h
*
* An ::SDL_QUIT event is generated when the user tries to close the application
* window. If it is ignored or filtered out, the window will remain open.
* If it is not ignored or filtered, it is queued normally and the window
* is allowed to close. When the window is closed, screen updates will
* complete, but have no effect.
*
* SDL_Init() installs signal handlers for SIGINT (keyboard interrupt)
* and SIGTERM (system termination request), if handlers do not already
* exist, that generate ::SDL_QUIT events as well. There is no way
* to determine the cause of an ::SDL_QUIT event, but setting a signal
* handler in your application will override the default generation of
* quit events for that signal.
*
* \sa SDL_Quit()
SDL_EventType#SDL_QUIT
An SDL_QUIT event is generated when the user clicks on the close button of the last existing window.
You shouldn't call SDL_Quit() in the destructor, but only once before leaving the application (is suggested to use it with atexit())
--- a/main.cpp
+++ b/main.cpp
## -32,17 +32,18 ## int main() {
}
-
// Delete Windows
- delete window;
- delete window2;
- delete window3;
+ // delete window;
+ // delete window2;
+ // delete window3;
// reset pointer
window = nullptr;
window2 = nullptr;
window3 = nullptr;
+ SDL_Quit();
+
// Close Program properly
return 0;
}
--- a/video/window.cpp
+++ b/video/window.cpp
## -51,7 +51,7 ## Window::~Window() {
// Shutdown if last window closed
if(this->windows.empty()) {
// Shutdown Video System
- SDL_Quit();
+ // SDL_Quit();
std::cout << "shuted down Video" << std::endl;
}
}
I have developed a simple program with MFC. It's responsible for reading and writing geotiff files using GDAL library. For this purpose, I have derived two classes from CFileDialog class named ManageOpenGeoTiffFiles and ManageSaveGeoTiffFiles each have 3 functions to support reading and writing geotiffs.
this is the header of the constructor and destructor for one of them:
ManageOpenGeoTiffFiles::ManageOpenGeoTiffFiles(void):CFileDialog(true,0,0,OFN_ENABLESIZING | OFN_HIDEREADONLY,_T("Tiff Files (*.tif)|*.tif|"),0,0,true)
ManageOpenGeoTiffFiles::~ManageOpenGeoTiffFiles(void)
{
}
and this is how I use it in my code:
void CInitialJobProject2FinalDlg::OnBnClickedBtnopen()
{
// TODO: Add your control notification handler code here
m_oglWindow1.WantToPan = false;
m_oglWindow1.WantToUseZoomTool = false;
CString fullpath;
if ( m_openFiles.DoModal() == IDOK )
{
fullpath = m_openFiles.GetPathName();
try{
m_openFiles.OpenGeoTiffAsReadonly(fullpath);
}
catch(CFileException *e){
MessageBox(_T("the file could not be opened"),_T("error"),MB_OK);
this ->ExitMFCApp();
}
m_openFiles.ReadRasterData();
}
else
MessageBox(_T("you pressed cancel and can not proceed."),_T("error"),MB_ICONERROR);
}
everythings ok when I use the Open or Save button for the first time in my program but when it comes to second use I get the error:
and if I click ignore:
this error occurs in the line:
if ( m_openFiles.DoModal() == IDOK )
of each dialog and even if I click cancel the first time,the error occures in the second use of dialog.
Line 398 of dlgFile.cpp is as follows:
hr = (static_cast<IFileDialog*>(m_pIFileDialog))->SetFileTypes(nFilterCount, pFilter);
ENSURE(SUCCEEDED(hr));
Edited section:
Answering one of the comments and providing information for others too:
When I set a breakpoint there saw these results when the assertion fails:
pFilter 0x00fc3660 {pszName=0x00fc36a8 "Tiff Files (*.tif)" pszSpec=0x00fc3788 "*.tif" }
hr E_UNEXPECTED
and the results for the first time when the assertion does not fail is as follows:
pFilter 0x004cfca0 {pszName=0x004cfce8 "Tiff Files (*.tif)" pszSpec=0x004cfdc8 "*.tif" }
hr S_OK
You are passing a malformed filter string to CFileDialog::CFileDialog. The Remarks sections states the following condition:
The lpszFilter parameter [...] ends with two '|' characters.
I am using FLTK. I have a window with a variety of buttons the user can click to perform some action. In my int main() i have a switch statement to handle all of these. When the user clicks exit the switch statement is setup like so:
case Exit_program:
cout << "save files and exit\n";
do_save_exit(sw);
This goes to the do_save_exit function that creates a exit confirmation window with two buttons yes (exit) and no (don't exit). I got the yes button to work, exits the program, but the no button mean i should just hide the confirmation window. This is the follow functions:
void yes(Address addr, Address)
{
exit(0);
}
void no(Address addr, Address)
{
}
void do_save_exit(Window& w)
{
Window quit(Point(w.x()+100, w.y()+100), 250, 55, "Exit confirmation");
Text conf(Point(15,15),"Do you really want to save and exit?");
Button yes(Point(60, 20),35,30,"Yes",yes);
Button no(Point(140, 20),35,30,"No",no);
quit.attach(conf);
quit.attach(yes);
quit.attach(no);
wait_for_main_window_click();
}
The problem is, when i click the no button it goes to void no, but I can't go anywhere from there. I just want to do quit.hide() but the no function doesn't have sight of the quit window (out of scope). How should I proceed? Thank you
P.S: I have thought about using a pointer to the quit window and then using the pointer to quit the window in the no function but am not sure how to do that exactly.
The Fl_Window callback is called when an attempt is made to close the window. The default callback hides the window (and if all windows are hidden, your application ends). If you set your own window callback, you can override this behaviour, so as not to hide the window:
// This window callback allows the user to save & exit, don't save, or cancel.
static void window_cb (Fl_Widget *widget, void *)
{
Fl_Window *window = (Fl_Window *)widget;
// fl_choice presents a modal dialog window with up to three choices.
int result = fl_choice("Do you want to save before quitting?",
"Don't Save", // 0
"Save", // 1
"Cancel" // 2
);
if (result == 0) { // Close without saving
window->hide();
} else if (result == 1) { // Save and close
save();
window->hide();
} else if (result == 2) { // Cancel / don't close
// don't do anything
}
}
Set your window's callback wherever you set up your Fl_Window, e.g. in your main function:
window->callback( win_cb );
You probably need to look at using a modal (i.e., dialog) window. Look at <FL/fl_ask.h>
if (fl_ask("Do you really want to save and exit?"))
save_and_exit();
The header also has functions for the popup's font, title, etc.
When you build you don't get an error or warning? The problem is probably that you have both global functions names yes and no and also local variables called just the same. Rename either the functions of the variables.
No need to use hide().
You can simply use exit(0); in a callback.
I'm really having trouble closing my console application with FreeGLUT.
I would like to know what the best way is to take every possible closing, because I don't want any memory leaks (I'm pretty afraid of those).
So I already tried the following, which is giving me an exception like this:
First-chance exception at 0x754e6a6f in myProject.exe: 0x40010005: Control-C.
int main(int argc, char **argv)
{
if( SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, true) )
{
// more code here as well ....
glutCloseFunc(close); // set the window closing function of opengl
glutMainLoop();
close(); // close function if coming here somehow
}
else
{
return 1;
}
return 0;
}
void close()
{
// keyboardManager is a pointer to a class
// which I want to delete, so no memory will leak.
if(keyboardManager) // do I need this check?
delete keyboardManager;
}
bool CtrlHandler(DWORD fdwCtrlType)
{
switch(fdwCtrlType)
{
// Handle the CTRL-C signal.
case CTRL_C_EVENT:
// and the close button
case CTRL_CLOSE_EVENT:
close();
return true;
// Pass other signals to the next handler.
case CTRL_BREAK_EVENT:
return false;
// delete the pointer anyway
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
default:
close();
return false;
}
}
So what goes right is:
Closing the window of glut
Closing the console application with the x
Closing my window of glut with my keyboardmanager if(keyboardManager->isKeyDown[27]) glutExit();
What goes wrong is:
Closing the console application with CTRL+C, it gives the exception from above.
This is in Visual Studio 2008 C++.
UPDATE
I found that the exception is thrown, because I'm in debug. So that won't be a problem. But the question is still open: What is the most elegant way to actually close glut?
atexit() seems to work as well, so maybe I can use this?
I use this function:
void glutLeaveMainLoop ( void );
There is more information on their sourceforge page but I never used that functionality:
The glutLeaveMainLoop function causes freeglut to stop the event loop. If the GLUT_ACTION_ON_WINDOW_CLOSE option has been set to GLUT_ACTION_CONTINUE_EXECUTION, control will return to the function which called glutMainLoop; otherwise the application will exit.
http://freeglut.sourceforge.net/docs/api.php#EventProcessing
It is safe to use delete on a null pointer, no need to check.
Thanks to Maarten's post, this works to me:
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION);
whenever you want to leave the mainloop without termiante the application use:
glutLeaveMainLoop();
don't forget to include "freeglut.h"
I use glutDestroyWindow(int handle);
or
According to ID: RigidBody at OpenGL forum
void destroy_window()
{
window_valid = -1;
}
void display_func()
{
if(window_valid == -1)
return;
// draw things
}
Try this method:
glutDestroyWindow(glutGetWindow());