program still running in memory after completion?

Started by Chris Chancellor, May 05, 2018, 06:20:06 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Chris Chancellor

Hello Charles

i have a program compiled in O2 , which upon completion still did not exit completely
and still remains running in the memory.  please look at the attechment picture of the TaskManager
showing that the Saik.exe still running in memory.

What would be the problem, is there an O2 command or statement to exit completely so that it won't
show up on the TaskManager  upon program completion?
  •  

Charles Pegge

Hi Chris,
It often means that your program has ended, yet something prevents it from exiting the message loop. Are you using MainWindow in WinUtil.inc?

Chris Chancellor

#2
Hello Charles

No, with my own program as shown below (partial code shown as the other includes were written by another person
which he didn't give permission to publish)

after successful execution of sMain() , it just did not exit from memory

there is no windows involved, no message loop but just a simple  input --> process --> output

maybe can you please give me an O2 command that can help it to exitcompletely from memory when the smain()
has completed its process?  Thanxx


' Saik.o2bas


'============================================================
'     Saik encryption
'     
$ filename "Saik.exe"
use rtl64
use corewin



' for random numbers and strings
   uses RandGenSaik
' for the Saik encryption routines
  uses SaikRtn


===========================
Function SMain()
'     Encrypt with Saik
   DIM    SEncySt  AS STRING
   DIM   givenSt   ,  decst   AS STRING

  ' Test given strings
 

'   41 chars   
   givenSt  ="Meganova 103 is the new thing in space ??"





' ------------------------------------------------------------------------
'         Encryption
          Long Lgstr
  '       Note that  givenSt is any given string to be encrypted
          SEncySt = EncryptSaik(givenSt )
           LgStr = Len( SEncySt)
   
         ' Encrypted  string
          print     " Saik encrypted string  :  " +     chr(13,10) + SEncySt  +     chr(13,10) +
                         "  Length : " +  LgStr


  ' ------------------------------------------------------------------
  '          Decryption
  '          Decrypt the encryted string
            decst = DecryptSaik(  SEncySt)
             LgStr = Len(decst)
             print  " decrypted  string :  "  +     chr(13,10) + decst +     chr(13,10) +
                    "  Length : " +  LgStr

    End Function




SMain()

  •  

Mike Lobanovsky

Chris,

Any tight and infinite loop, not only the blocking While GetMessage()/Wend, can prevent your executable from shutting down gracefully. Check your other includes if they break their Saik encryption/decryption loops as, and when, planned.

ExitProcess() is your friend if you aren't able to determine the true cause of malfunction. But it always looks more like some kind of programmatic "crutch" rather than an elegant solution.
Mike
(3.6GHz Intel Core i5 w/ 16GB RAM, 2 x GTX 650Ti w/ 2GB VRAM, Windows 7 Ultimate Sp1)
  •  

Charles Pegge

#4
Consoles are very useful for monitoring your program during development and debugging, with the added advantage of shutting down the prog when the console is closed.


$filename "t.exe"
'uses rtl64
uses console
int a
do
  a++
  printl a
loop



Chris Chancellor

Thanxx a lot Mike,

i placed in the following codes and it works now by calling  ExitProgram after the Smain()

the program is no longer lurking in memory upon exiting

but there must be something wrong with those other  includes -- i have to check with its author


use corewin


%PROCESS_TERMINATE= 0x0001



'=========================
'  to terminate the program immediately
SUB ExitProgram
  LOCAL hProc  ,ProcId ,ExitAddress AS sys
  Local  lResult AS LONG
  ProcId = GetCurrentProcessId
  hProc = OpenProcess(BYVAL %PROCESS_TERMINATE, BYVAL 0, BYVAL ProcID)
  IF hProc <> %NULL THEN
    lresult = GetExitCodeProcess(hProc, ExitAddress)
    IF lresult <> 0 THEN
      CALL ExitProcess(ExitAddress)
    ELSE
      CloseHandle hProc
    END IF

  END IF
  END SUB
  •  

Mike Lobanovsky

Chris,

I wouldn't TerminateProcess() or OpenProcess(PROCESS_TERMINATE) to end my own process. They are too rough for that; they compromise a few stages of graceful shut down. They are rather the last resort to enforce termination upon some 3rd party process you've been working with.

Prefer to use ExitProcess() unconditionally. From the OS perspective, it ensures clean exit from any process you create with your code and unmaps all its dependencies gracefully.

Yet ExitProcess() would bypass certain stages that your compiler may optionally utilize as an epilogue to cleanly end the processes its executables create. Such stages are usually coded via the compiler's atexit() facility or its equivalent. So,  ExitProcess() can also be incomplete but only from the compiler's own perspective. I am not however sure if OxygenBasic supports this facility at all or not.

That's why ExitProcess() is usually considered a palliation which indicates that the programmer is not quite sure why his or her application faulters at its shutdown.
Mike
(3.6GHz Intel Core i5 w/ 16GB RAM, 2 x GTX 650Ti w/ 2GB VRAM, Windows 7 Ultimate Sp1)
  •  

Charles Pegge

An O2 EXE frees all its libraries and global buffers, then performs ExitProcess, as Mike describes. DLL and JIT binaries release any stack allocation, restore registers, and return to the caller, instead of calling ExitProcess.
As such, the latter are more vulnerable to corruption of the rbp register value

Mike Lobanovsky

Quote from: Charles Pegge on May 05, 2018, 07:30:34 PMDLL and JIT binaries release any stack allocation, restore registers, and return to the caller, instead of calling ExitProcess.

Oh yes, that's a very important note I overlooked to mention!

ExitProcess() shouldn't be used in a DLL because not only will it free the DLL it is called from but it will also kill the entire process, which isn't always what the programmer intends when freeing the library that isn't going to be used any more. We've seen it in the recent splash screen example, haven't we?
Mike
(3.6GHz Intel Core i5 w/ 16GB RAM, 2 x GTX 650Ti w/ 2GB VRAM, Windows 7 Ultimate Sp1)
  •  

Chris Chancellor

Thanxx a lot  Charles and Mike

very good learning here
  •