About Store Forum Documentation Contact



Post Reply 
APP_EXIT_IMMEDIATELY usage
Author Message
Houge Offline
Member

Post: #1
APP_EXIT_IMMEDIATELY usage
Hi!

I'm trying to use APP_EXIT_IMMEDIATELY flag. But i faced a problem that void Shut() is not called and global variables destructors are not called too. Here is a small example:

Code:
/******************************************************************************/
class baba
{
   baba()   {   Init();}
  ~baba()   {   Shut();}
}
baba bab;
Flt deltaTime, last_time;
/******************************************************************************/
void InitPre() // init before engine inits
{
   EE_INIT(); // call auto-generated function that will setup application name, load engine and project data
   App.flag |= APP_WORK_IN_BACKGROUND|APP_NO_PAUSE_ON_WINDOW_MOVE_SIZE|APP_ALLOW_NO_GPU|APP_MIN​IMIZABLE|APP_EXIT_IMMEDIATELY;
   LogConsole();
   App.quit = Shut;
   while(Update()){};
   Shut();
}
/******************************************************************************/
bool Init() // initialize after engine is ready
{
   FileText f;
   f.write("C:\\enter.txt");
   f.putLine("Aaaaaaaaaaaaaaaaaaaaaaa");
   return true;
}
/******************************************************************************/
void Shut() // shut down at exit
{
   FileText f;
   f.write("C:\\exit.txt");
   f.putLine("Aaaaaaaaaaaaaaaaaaaaaaa");
}
/******************************************************************************/
bool Update() // main updating
{
   deltaTime = Time.curTime() - last_time;
   return true;// continue
}
/******************************************************************************/
void Draw() // main drawing
{}
/******************************************************************************/

File C:\exit.txt is never created (neither in bab destructor, nor in Shut function), so i'm curious if i can put server loop inside IntPre, will it call all clients destructors when i close the app with X button?
(This post was last modified: 03-29-2015 10:11 AM by Houge.)
03-27-2015 09:30 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #2
RE: APP_EXIT_IMMEDIATELY usage
Hi,

Shut does get called with APP_EXIT_IMMEDIATELY enabled.
The file is not created because most likely you need admin permission to write in root C folder.
Please do not call any of the Init Shut Update Draw manually, as these are callbacks called by the engine.
Handle your game server loop in 'Update'
If you want to run it on Linux server, use APP_ALLOW_NO_GPU
03-31-2015 12:50 AM
Find all posts by this user Quote this message in a reply
Houge Offline
Member

Post: #3
RE: APP_EXIT_IMMEDIATELY usage
(03-31-2015 12:50 AM)Esenthel Wrote:  The file is not created because most likely you need admin permission to write in root C folder.

That is not so simple as you think lol
File "enter.txt" is created, but "exit.txt" doesn't?

Ok to remove all misunderstandings let's use the following code:

Code:
/******************************************************************************/
void writeToLog(Str s)
{
   C Str fname = "C:\\appLog.txt";
   FileText f; f.append(fname); f.putLine(s);
}
/******************************************************************************/
class baba
{
   baba(){   writeToLog("Constructor Called");   }
  ~baba(){   writeToLog("Destructor Called" );   }
}
baba bab;
Flt deltaTime, last_time;
/******************************************************************************/
void InitPre() // init before engine inits
{
   EE_INIT();
   App.flag = APP_WORK_IN_BACKGROUND|APP_NO_PAUSE_ON_WINDOW_MOVE_SIZE|APP_ALLOW_NO_GPU|APP_MIN​IMIZABLE|APP_EXIT_IMMEDIATELY;
   writeToLog("IntPre Called");
   LogConsole();
   while(Update()){};
}
/******************************************************************************/
bool Init() // initialize after engine is ready
{
   writeToLog("Init Called");
   return true;
}
/******************************************************************************/
void Shut() // shut down at exit
{
   writeToLog("Shut Called");
}
/******************************************************************************/
bool Update() // main updating
{
   deltaTime = Time.curTime() - last_time;
  
   if(Time.curTime() > 10)
      return false;
  
   return true;// continue
}
/******************************************************************************/
void Draw(){}
/******************************************************************************/

Now i answer your questions:

(03-31-2015 12:50 AM)Esenthel Wrote:  Shut does get called with APP_EXIT_IMMEDIATELY enabled.
Yes, but only when IntPre exits itself, without X button.

(03-31-2015 12:50 AM)Esenthel Wrote:  Please do not call any of the Init Shut Update Draw manually, as these are callbacks called by the engine.
Ok, the new example doesn't do that.

(03-31-2015 12:50 AM)Esenthel Wrote:  Handle your game server loop in 'Update'
How can i do that if application exits after IntPre function? smile

Let's do 3 tests together.
You can change C Str fname = "C:\\appLog.txt"; to any file name you like.

Test 1. Run the code above and wait 10 seconds. Application will close itself.
What you expect in log:
Constructor Called
IntPre Called
Init Called
Shut Called
Destructor Called

What you get:
Constructor Called
IntPre Called
Shut Called
Destructor Called

What's wrong:
Init is NOT called smile

Test 2. Run the code above and close it with X button.
What you expect in log:
Constructor Called
IntPre Called
Shut Called
Destructor Called

What you get:
Constructor Called
IntPre Called

What's wrong:
No way, no Shut and no Destructor! A memory leak? I think the problem is that the engine calls Shut etc when the window recieves a message like WM_CLOSE or WM_QUIT or whatever. But with APP_EXIT_IMMEDIATELY there is NO window smile

Test 3. Remove while(Update()){}; from IntPre from the code above and run application.
What you expect in log:
Constructor Called
IntPre Called
Shut Called
Destructor Called

What you get:
Constructor Called
IntPre Called
Shut Called
Destructor Called

What's wrong:
In this case application is closed right after it is opened and Update is not handled by the engine so i can't do the following:

(03-31-2015 12:50 AM)Esenthel Wrote:  Handle your game server loop in 'Update'

So what shall i do to get all destructors calls and Shut when i use X button to close the application? smile
(This post was last modified: 03-31-2015 06:59 AM by Houge.)
03-31-2015 06:56 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #4
RE: APP_EXIT_IMMEDIATELY usage
Hi,

It appears that closing the console terminates the app immediately - that's a limitation of the OS:
http://stackoverflow.com/questions/11959...-whole-app

This commit disables the option to close the console completely:
https://github.com/Esenthel/EsenthelEngi...376d43876e

Quote:How can i do that if application exits after IntPre function?
Do not use APP_EXIT_IMMEDIATELY
03-31-2015 10:59 PM
Find all posts by this user Quote this message in a reply
Houge Offline
Member

Post: #5
RE: APP_EXIT_IMMEDIATELY usage
(03-31-2015 10:59 PM)Esenthel Wrote:  It appears that closing the console terminates the app immediately - that's a limitation of the OS:
http://stackoverflow.com/questions/11959...-whole-app
I suspected this, just wanted to hear it from another person. Ok.

(03-31-2015 10:59 PM)Esenthel Wrote:  This commit disables the option to close the console completely:
https://github.com/Esenthel/EsenthelEngi...376d43876e

Thanks a lot, i think it's more than enough, to prevent console window from closing accidentally.

(03-31-2015 10:59 PM)Esenthel Wrote:  
Quote:How can i do that if application exits after IntPre function?
Do not use APP_EXIT_IMMEDIATELY
Fair enough smile

Thank you for your answers!
04-01-2015 05:41 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #6
RE: APP_EXIT_IMMEDIATELY usage
Hope that helped! smile
04-01-2015 09:23 AM
Find all posts by this user Quote this message in a reply
Houge Offline
Member

Post: #7
RE: APP_EXIT_IMMEDIATELY usage
(04-01-2015 09:23 AM)Esenthel Wrote:  Hope that helped! smile

I will test it as soon as beta branch merges to master, but i'm fully satisfied with the answers and explanations you gave me, so that's clear smile
04-01-2015 08:56 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply