App hangs after wxApp::OnInit() returns false 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
Frank
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Jan 01, 2005 6:19 pm

App hangs after wxApp::OnInit() returns false

Post by Frank » Mon Aug 04, 2008 11:58 am

Hi wxForum,

I have a strange problem. My apps hangs, if wxApp::OnInit() returns false.

I have an wxSingleInstanceChecker, and my OnInit() returns false, when the process is already running, so that the new process ends itself. Trouble is, it does not.

It creates no windows, but it hangs somewhere, I can see the second process in the taskmanager.

After some testing, I can reproduce the error with this code:

Code: Select all

class MyApp : public wxApp
{
public:
    virtual bool OnInit () { return false; }
    virtual int OnExit () { return 0; }
};
The programm is just sitting there in the Taskmanager, consuming no cpu.

And now for the strange part: I use wxMSW 2.8.3 as monolithic DLL. If I link my little Test-Application against a static Monolithic wx Lib, it does not hang.

So, what can it be, that the DLL Version hangs my application, when the static version does not.

Any Ideas?

Thanks
Frank

Edit: After some debugging I found the exact Spot where it hangs. It hangs in the CRT, where it calls the WinAPI ExitProcess(). As soon as ExitProcess is called, the app hangs. The MSDN states that there is the possiblillity of dadlocks, but Process Explorer shows that this app has only one single thread.

mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Re: App hangs after wxApp::OnInit() returns false

Post by mc2r » Mon Aug 04, 2008 6:00 pm

This is a stab in the dark based on nothing other than how I do my OnInit() and it works. But you could try calling wxApp::OnInit() before returning false;

Code: Select all

class MyApp : public wxApp
{
public:
    virtual bool OnInit () { wxApp::OnInit(); return false; }
    virtual int OnExit () { return 0; }
};
Like i said though its just a guess. Also you don't show a DECLARE_APP, IMPLEMENT_APP etc... macros, are you using them? Not sure that would effect this either but worth checking.

-Max

User avatar
zitmen
Earned a small fee
Earned a small fee
Posts: 19
Joined: Mon Aug 04, 2008 11:23 am
Location: Prague, Czech Republic
Contact:

Re: App hangs after wxApp::OnInit() returns false

Post by zitmen » Mon Aug 04, 2008 11:13 pm

mc2r wrote:Also you don't show a DECLARE_APP, IMPLEMENT_APP etc... macros, are you using them?
It wouldn't work without IMPLEMENT_APP, becouse of missing entrypoint (main / WinMain).

Following forks fine for me:

Code: Select all

#include <wx/wx.h>
#include <windows.h>

class MyFrame : public wxFrame
{
	public:
		MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
			: wxFrame((wxFrame *)NULL, -1, title, pos, size)
		{
			wxMenu *menuFile = new wxMenu;
			menuFile->Append( 1, _T("E&xit") );

			wxMenuBar *menuBar = new wxMenuBar;
			menuBar->Append( menuFile, _T("&File") );

			SetMenuBar( menuBar );

			CreateStatusBar();
			SetStatusText( _T("Welcome to wxWidgets!") );
		}

		void OnQuit(wxCommandEvent& event)
		{
			Close();
		}

	DECLARE_EVENT_TABLE()
};

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
	EVT_MENU(1, MyFrame::OnQuit)
END_EVENT_TABLE() 

class MyApp : public wxApp
{
	public:
		bool OnInit()
		{
			if(FindWindow(NULL, _T("Hello World")))
			{
				wxMessageBox(_T("Already exists!"), _T("WndChecker"));
				return false;
			}
			//
			MyFrame *frame = new MyFrame(_T("Hello World"), wxPoint(50,50), wxSize(450,340));
			frame->Show(); 
			return true;
		}
};

IMPLEMENT_APP(MyApp)
Checking is really primitive here, but that is enough for this example.

It works with Monolithic DLL too, but I have 2.8.7 version.

Try execute my example (if you want), then it will obvious if the problem is in version or in your code.

Frank
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Jan 01, 2005 6:19 pm

Post by Frank » Tue Aug 05, 2008 9:11 am

Hi,

your code hangs too.

I think it is some timing problem, because when I end the application the normal way, by closing the MainFrame, or by putting an exit() somewhere outside of wxApp it works as expected.

Dont know, maybe there is some Runtime-Thingy in one of the DLLs (Process Explorer shows some dozenz DLLs linked to the exe, most of wich I never heard of). I believe one of those is hanging on the Detaching-Code. The Call-Stack in Process-Explorer shows some reference to WaitForSingleObject(), so there seems to be a deadlock somewhere. But how can this be, if there is only one thread?

Anyway, my workaround for now is to hard kill the Process via WinAPI TerminateProcess(). This one skips the DLL-Detaching, and the Process is killed properly.

Greets
Frank

User avatar
zitmen
Earned a small fee
Earned a small fee
Posts: 19
Joined: Mon Aug 04, 2008 11:23 am
Location: Prague, Czech Republic
Contact:

Post by zitmen » Tue Aug 05, 2008 9:28 am

Frank wrote:Hi,

your code hangs too.
Ok, I have to ask you now: Have you any reason for using v2.8.3? May be, it's bug in older wx version, so upgrade could solve that.

Frank
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Jan 01, 2005 6:19 pm

Post by Frank » Tue Aug 05, 2008 11:19 pm

Hi,

No I have no real reason to stay with 2.8.3. Just Time-Constraints.

After the next release of our proejct I will update. Okay, I planned that before the last release too. And the release before that. Well, next time I'll do it ;-)

Greetings
Frank

Post Reply