Why does this code cause the process to hang around despite the app being shutdown? If I take out the Windowed Screen related code, the process dies as expected upon app ending.
srod, thanks for trying it. I'm on XP Pro. I just tried it again the following morning (after bootup) and the program still lingers in the task manager after app shutdown.. I'll try this a bit later again on another PC. Certainly is weird that happens with the screen stuff only. Maybe there is something strange happening with anti-virus/spyware software but I seriously doubt it.
I just built an exe & tried on my wife's PC (XP Home) & my PC (XP Pro). It always closed cleanly on her machine (disappears ok from task manager), but not on mine. I'd just noticed that very rarely I have seen the program close ok on my machine.. wierd.
There's nothing inherently wrong with the way you have this written and it is removing the process immediately here. But if you don't mind (and this is in no way criticism at all) I'd like to tweak the logic just slightly and show you how I'd probably write it:
Procedure Init()
; initialize DirectX
If InitKeyboard() = 0 Or InitMouse() = 0 Or InitSprite() = 0 Or InitSprite3D() = 0 Or InitSound() = 0
ProcedureReturn ?err_dx
EndIf
; open windowed screen
rv = OpenWindow(0, 0, 0, 800, 600, "test", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
If rv = 0
ProcedureReturn ?err_window
EndIf
rv = OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 0, 0, 0)
If rv = 0
ProcedureReturn ?err_screen
EndIf
ProcedureReturn 0
EndProcedure
Procedure MainLoop()
quit.l = 0
Repeat
eventID = WaitWindowEvent()
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Escape)
quit = 1
EndIf
Until quit = 1 Or eventID = #PB_Event_CloseWindow
EndProcedure
result = Init()
If result
MessageRequester("Error", PeekS(result),#MB_ICONERROR)
Else
MainLoop()
EndIf
End
DataSection
err_dx:
Data.s "Fatal Error: Could not initialize DirectX"
err_window:
Data.s "Fatal Error: Could not open main window"
err_screen:
Data.s "Fatal Error: Could not open a DirectX screen"
EndDataSection
This is of course bearing in mind that if your program isn't going on to do other things after CloseWindow and/or CloseScreen there is no need for these commands as the End command takes care of them.
Thanks netmaestro. Yes this code was culled from another larger program which introduced me to this (linger) issue. I thought I must have had a memory leak of my own doing until I whittled it down to the bare bones (original post), and I was surprised..
I tried your version just for the exercise, and it still lingers (not that I was expecting any different, except maybe hoping that the magical netmaestro touch would work its magic on my machine to purge this evil-doing ).
It might be worthwhile to start the CPU monitor from the Debugger menu and see what it is saying. There should be two lines shown until your program starts and then a third one shows up when your window opens. Close the window and see if that third line is disappearing, I'm betting it does. If that's the case, it could just be that your task manager is taking its time updating its window for some reason.
BTW, the "magical netmaestro touch" can crash a program pretty fast!
OK, one more test. Run this code, close the window and then immediately try to run it again. If it will run, the process is absolutely not existing, as it is the closing of the process that automatically removes the mutex.
Hi netmaestro, thanks for that - I'm at work now so I'll try that later. I've just tried the original code on my work XP Pro PC & it closes down fine (almost immediately leaving the task manager list). So you must be right - something strange about my machine, a serious delay for killing off processes for some reason. Just before, I watched & waited for the kill off for at least 20 seconds after quitting the app and it was still listed in the task manager (then I just killed it manually).
Maybe I am kicking in an open door:
Try if it works in safe mode when you're back home.
(or check in msconfig how much and which non- ms services are running)