Page 1 of 3

process still running after quitting the app

Posted: Thu Aug 24, 2006 1:54 pm
by mskuma
I must be tired/crazy..

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.

Code: Select all

Procedure Init()

  ; initialize DirectX

  If InitKeyboard() = 0 Or InitMouse() = 0 Or InitSprite() = 0 Or InitSprite3D() = 0 Or InitSound() = 0
    ProcedureReturn #False
  EndIf    
  
  ; open windowed screen

  rv = OpenWindow(0, 0, 0, 800, 600, "test", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
  
  If rv = 0
    ProcedureReturn #False
  EndIf
      
  rv = OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 0, 0, 0)
  
  If rv = 0
    ProcedureReturn #False
  EndIf
          
  ProcedureReturn #True

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
  
  CloseScreen()
  CloseWindow(0)
  
  End

EndProcedure

Init()

MainLoop()
Thanks.

Posted: Thu Aug 24, 2006 2:03 pm
by srod
Shuts down okay here, according to task manager the process quits immediately when I close it! xp home.

A bit strange though that you place an End statement within a procedure!

Posted: Thu Aug 24, 2006 9:16 pm
by PB
> A bit strange though that you place an End statement within a procedure!

Why? It can be used anywhere to end an app.

Posted: Thu Aug 24, 2006 9:23 pm
by Trond
PB wrote:> A bit strange though that you place an End statement within a procedure!

Why? It can be used anywhere to end an app.
Anywhere? Nope, doesn't work a text area in Opera.

Posted: Thu Aug 24, 2006 9:30 pm
by srod
PB wrote:> A bit strange though that you place an End statement within a procedure!

Why? It can be used anywhere to end an app.
I didn't say you couldn't, just said it was strange - at least imho. :)

Posted: Thu Aug 24, 2006 9:37 pm
by mskuma
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.

Posted: Thu Aug 24, 2006 9:53 pm
by mskuma
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.

Posted: Thu Aug 24, 2006 10:06 pm
by netmaestro
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:

Code: Select all

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.

Posted: Thu Aug 24, 2006 10:12 pm
by mskuma
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 :wink: ).

Posted: Thu Aug 24, 2006 10:21 pm
by netmaestro
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!

Posted: Thu Aug 24, 2006 10:41 pm
by mskuma
Yes the third line comes & goes (upon quit) but the process is hanging around for some reason (wonder why). Thanks for that.

Posted: Thu Aug 24, 2006 10:48 pm
by netmaestro
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.

Code: Select all

ProcedureDLL.l Instance_Running(LockStr$)
  *MyMutex = CreateMutex_(#Null, 1, LockStr$)
  If *MyMutex <> 0 And GetLastError_() = #ERROR_ALREADY_EXISTS
    CloseHandle_(*MyMutex)
    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf
EndProcedure

Procedure Init() 

  ; initialize DirectX 

  If InitKeyboard() = 0 Or InitMouse() = 0 Or InitSprite() = 0 Or InitSprite3D() = 0 Or InitSound() = 0 
    ProcedureReturn #False 
  EndIf    
  
  ; open windowed screen 

  rv = OpenWindow(0, 0, 0, 800, 600, "test", #PB_Window_ScreenCentered | #PB_Window_SystemMenu) 
  
  If rv = 0 
    ProcedureReturn #False 
  EndIf 
      
  rv = OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 0, 0, 0) 
  
  If rv = 0 
    ProcedureReturn #False 
  EndIf 
          
  ProcedureReturn #True 

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 
  
  CloseScreen() 
  CloseWindow(0) 
  
  End 

EndProcedure 

If Instance_Running("MyTestMutexString") = 0
  Init() 
  MainLoop()
Else
  MessageRequester("Error","Already Running!")
EndIf

Posted: Thu Aug 24, 2006 11:02 pm
by mskuma
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).

Safe mode

Posted: Thu Aug 24, 2006 11:17 pm
by Character
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)

Posted: Fri Aug 25, 2006 8:03 am
by PB
> Anywhere? Nope, doesn't work a text area in Opera

Huh? What's Opera got to do with PureBasic's "End" command?