process still running after quitting the app

Just starting out? Need help? Post your questions and find answers here.
mskuma
Enthusiast
Enthusiast
Posts: 573
Joined: Sat Dec 03, 2005 1:31 am
Location: Australia

process still running after quitting the app

Post 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.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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!
I may look like a mule, but I'm not a complete ass.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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. :)
I may look like a mule, but I'm not a complete ass.
mskuma
Enthusiast
Enthusiast
Posts: 573
Joined: Sat Dec 03, 2005 1:31 am
Location: Australia

Post 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.
mskuma
Enthusiast
Enthusiast
Posts: 573
Joined: Sat Dec 03, 2005 1:31 am
Location: Australia

Post 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.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
mskuma
Enthusiast
Enthusiast
Posts: 573
Joined: Sat Dec 03, 2005 1:31 am
Location: Australia

Post 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: ).
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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!
BERESHEIT
mskuma
Enthusiast
Enthusiast
Posts: 573
Joined: Sat Dec 03, 2005 1:31 am
Location: Australia

Post by mskuma »

Yes the third line comes & goes (upon quit) but the process is hanging around for some reason (wonder why). Thanks for that.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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
BERESHEIT
mskuma
Enthusiast
Enthusiast
Posts: 573
Joined: Sat Dec 03, 2005 1:31 am
Location: Australia

Post 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).
Character
Enthusiast
Enthusiast
Posts: 337
Joined: Mon Aug 07, 2006 3:51 pm
Location: Netherlands

Safe mode

Post 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)
Cessante causa cessat effectus
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> Anywhere? Nope, doesn't work a text area in Opera

Huh? What's Opera got to do with PureBasic's "End" command?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply