Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 242731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:51:44+00:00 2026-05-11T20:51:44+00:00

I have read questions/559482/why-doesnt-an-iphone-apps-main-function-ever-get-a-chance-to-finish , which explains why NSApplicationMain never actually returns. The same

  • 0

I have read questions/559482/why-doesnt-an-iphone-apps-main-function-ever-get-a-chance-to-finish, which explains why NSApplicationMain never actually returns. The same thing happens (for the same reason) in a desktop cocoa application, which is what I am working on.

With that in mind, how would I go about using NSLog to output some final debugging messages when my application exits?

To be specific, I would like to do something like this:

int myDebugVariable = 0;

int main(int argc, char *argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    CMLog(@"application begin");

    int exitCode = NSApplicationMain(argc,  (const char **) argv);

    CMLog(@"application end. Debugging variable = %d", myDebugVariable);

    [pool release];
    return exitCode;
}

In this example, The “application begin” line is printed to the console, but the “application end.” line is not.

Note #1: In my actual code, I am using something more sophisticated than myDebugVariable. This is a simplified example that illustrates the effect I am trying to achieve.

Note #2: I am familiar with the ApplicationWillTerminate method, which gets called when the application is about to quit, but it does not suit my needs. My debugging code relies on the dealloc methods for some custom classes, so it does not come into play until after ApplicationWillTerminate is called.


update:

Adam Rosenfield’s answer did the trick. For the sake of completeness, here is a working solution:

int myDebugVariable = 0;

void my_exit_handler(void)
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    CMLog(@"application end: Debugging variable = %d", myDebugVariable);

    [pool release];
}

int main(int argc, char *argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    CMLog(@"application begin");
    atexit(my_exit_handler);

    int exitCode = NSApplicationMain(argc,  (const char **) argv);

    [pool release];
    return exitCode;
}
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:51:44+00:00Added an answer on May 11, 2026 at 8:51 pm

    Use atexit(3) to register an exit handler. It will automatically be invoked when your app exits, either by finishing main or by calling exit(3). For example:

    void my_exit_handler(void)
    {
        NSLog(@"about to exit, x = %d\n", x);
    }
    
    // at some point during app initialization...
    atexit(&my_exit_handler);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read Answers to C++ interview questions among which there is one that
I have read a few questions similar to this, but I can't get my
I have read similar questions but they talk of AI models. What I want
I have read several questions regarding this but I fear they may be out
I have read many questions about Android, J2ME and RecordStore , but I still
I have read many questions about the facebook login but until not I didnt
First of all let me tell you that i have read the following questions
I have read quite a few selcet+update questions in here but cannot understand how
I am using JMeter and have 2 questions (I have read the FAQ +
I have trouble with adding new row to my UITableView. I read similar questions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.