-4

What I'm trying to do is send an e-mail when exiting my program. I use atexit(); function to do this but... it's not working properly.

#include <windows.h>
#include <iostream>

void SendEmail()
{
    //lot of code here
}

void Print()
{
    std::cout << "Bye!";
    system("pause");
}

void Terminate()
{
    SendEmail(blahblah); // Doesn't work
    Print(); // Works
}

int main()
{
    atexit(Terminate);

    while (true) Sleep(1);
    return 0;
}

Does SendEmail(); function take too much time and doesn't do anything? It doesn't even print the server responses (it works fine when used in main();)

  • 3
    Well, for one the `SendEmail()` function in your example doesn't take a `blahblah` parameter, which is not even defined in your example. And then I'd daresay the problem is somewhere in `//lot of code here`... have you tried your *real* `SendEmail()` from within `main()` successfully before? – DevSolar Oct 15 '15 at 15:34
  • registered `atexit` handlers only run during a normal exit, not if the process is killed... and you leave no mechanism for normal exit. – Ben Voigt Oct 15 '15 at 15:35
  • 2
    Welcome to programming, where your first step with any problem is to break the problem down to smaller manageable tasks. For example, in this case, do you even know if `SendEmail()` works when you change it to not be part of `atexit()`? If you don't know this, you should before you ask for help. – mah Oct 15 '15 at 15:35
  • How are you exiting your program? CTRL+C? `kill -KILL pid` ? `TerminateProcess()` ? crashing? – Ben Voigt Oct 15 '15 at 15:36
  • Compile with all warnings and debug info (e.g. `g++ -Wall -Wextra -g` if using [GCC](http://gcc.gnu.org/)....) then **use the debugger** (e.g. `gdb`), in particular by setting a breakpoint on your `SendEmail` function – Basile Starynkevitch Oct 15 '15 at 15:37
  • Minus 4 is very harsh. This is not the meta site! – P45 Imminent Oct 15 '15 at 15:39
  • Make certain of two things: 1) you are using `std::atexit`, not `atexit` and 2) that everything you still need alive to execute `SendEmail` is still alive by the time it is called. – user4581301 Oct 15 '15 at 15:41
  • @P45Imminent Harsh, yes, but the question cannot be answered in it's current state without the use of a crystal ball, a Flux Capacitor, or a few grams of Unobtainium. Best we can do is give suggestions. – user4581301 Oct 15 '15 at 15:45

1 Answers1

3

You don't know that other resources have been shut down prior to the running of atexit. These might have been important for the successful running of SendMail.

P45 Imminent
  • 8,319
  • 4
  • 35
  • 78
  • The OS doesn't get involved until _after_ `atexit`. – MSalters Oct 15 '15 at 21:57
  • I'm confident the OS is inextricably linked with the running of any program on linux or Windows. But I think I know what you're trying to say. I've edited. – P45 Imminent Oct 16 '15 at 07:02
  • Actually, the OS is mostly not involved, as that would be too slow. Switching from program to OS and back is an expensive context switch. – MSalters Oct 16 '15 at 11:22