iOS termination function exit

Source: Internet
Author: User
Tags terminates

1. Exit Function

The C,c++ function exit is used to terminate the current program, and the function is defined as follows:


    1. void exit (int status);


The official notes are as follows:

Terminates the process normally, performing the regular cleanup for terminating programs.
Normal program termination performs the following (in the same order):

    • Objects associated with the current thread with thread storage duration is destroyed (c++11 only).
    • Objects with static storage duration is destroyed (c + +) and functions registered withatexit is called.
    • All C streams (open with functions in <cstudio>) is closed (and flushed, if buffered), and all files created with T Mpfile is removed.
    • Control is returned to the host environment.
The third 4th is easier to understand than to start a discussion .


1th:

The thread storage here refers to threads that are locally stored, valid for the threaded scope, and c++11 define the thread storage as follows:

[HTML]View Plaincopy
    1. Thread_local Object obj; object is a C + + class
    2. thread_local int value = 1;


2nd:static storage refers to a variable defined in the global or static mode of a function that is stored in a static store (as distinct from the heap and stack), such as: [HTML]View Plaincopy
    1. Object GOBJ;
    2. void Fun () {
    3. Static Object sobj;
    4. }
The function set by atexit is called during exit, and the ATEXIT function is defined as follows: [HTML]View Plaincopy
    1. int atexit (void (*func) (void));

Atexit examples are as follows: [CPP]View Plaincopy
  1. / * Atexit Example * /   
  2. #include <stdio.h>/* puts * /   
  3. #include <stdlib.h>/* atexit * /   
  4. void fnExit1 (void)
  5. {
  6. Puts ("Exit function 1." )  );
  7. }
  8. void fnExit2 (void)
  9. {
  10. Puts ("Exit function 2." )  );
  11. }
  12. int Main ()
  13. {
  14. Atexit (FNEXIT1);
  15. Atexit (FNEXIT2);
  16. Puts ("Main function." )  );
  17. Exit (0);
  18. }

the output is: [HTML]View Plaincopy
    1. Main function.
    2. Exit function 2.
    3. Exit function 1.


for C + + object destruction Order, you can see the following example: [CPP]View Plaincopy
  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. #include <unistd.h>   
  4. #include <thread>   
  5. #include <string>   
  6. #include <iostream>   
  7. class MyObject
  8. {
  9. Public :  
  10. std::string str;
  11. Public :  
  12. MyObject (std::string str) {
  13. This  ->str = str;
  14. }
  15. ~myobject () {
  16. printf ("%s--%s\n", __function__, Str.c_str ());
  17. }
  18. };
  19. void thread_fun (int N)
  20. {
  21. Thread_local MyObject threadobj ("thread_local subthread obj");
  22. //do something   
  23. Sleep (2);
  24. }
  25. void Exit_fun () {
  26. MyObject threadobj ("atexit obj");
  27. //do something   
  28. Sleep (1);
  29. }
  30. MyObject obj ("Global obj");
  31. int Main (int argc, const char * argv[])
  32. {
  33. Thread_local MyObject threadobj ("thread_local mainthread obj");
  34. Static  MyObject staticobj ("fun static obj");
  35. std::thread t (thread_fun, 1);
  36. Atexit (Exit_fun);
  37. Sleep (2);
  38. Exit (0);
  39. }

Output: [CPP]View Plaincopy
    1. ~myobject--thread_local subthread obj
    2. ~myobject--thread_local mainthread obj
    3. ~myobject--Atexit obj
    4. ~myobject--Fun static obj
    5. ~myobject--Global obj
    6. Program ended with exit code:0

As can be seen from the above, for the 2nd is to call Atexit first, and then clean up the static storage

2. Exit vs Abort
void abort (void);

The abort function is actually sending SIGABRT signal, which terminates the program directly if it is not processed, and does not do cleanup operations at this time, which is equivalent to terminating the program directly.

3. IOS Exit Function
double-clicking the Home Manual Kill program will call the Exit function to close the program, the __cxa_finalize function will perform the above mentioned cleanup work.

if the appdelegate applicationwillterminate function is overloaded, the function is called before exit is executed, and the developer program processing opportunity is provided before terminating .


If the program has retreated to the background and is in the suspend state, then manual kill does not call exit as above, but sends Sigkill signal to terminate the program directly.

4. Exit and multithreadingWhen exit is invoked, if other threads are executing and accessing the global or Staic C + + object, it is possible that an unexpected memory error has occurred because these objects have been destroyed, such as: SIGBUS,SIGSEGV. To avoid this problem you can have some of the following processing scenarios:
1). Move the object from the static storage on the heap to avoid the exit process destroying the object with unintended resultsFor example: [HTML]View Plaincopy
    1. MyObject gObj ("Global obj");
    2. ------>
    3. myobject* pObj = new MyObject ("Global obj ptr");

2). Use Atexit to register callbacks, or end child threads in Applicationwillterminate (IOS) to avoid referencing objects that might be destroyed.


5. Terminate the program on iOS
1). Active call exit or user termination (program does not enter suspend state, ApplicationdidenterbackgroundNo callback )

You can call exit (0) to terminate the program directly on iOS, as well as clean up C + + object as mentioned above cleanup. (Note: IOS 7.0 simulator is not cleaned, iphone and 7.1 simulator are OK, estimated to be simulator bug)
2). If the program has entered the suspend state Applicationdidenterbackground has been callback, the system will send the Sigkill directly to the end of the Force program

iOS termination function exit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.