Catching CTRL+C signal...
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Catching CTRL+C signal...

  1. #1
    Join Date
    Jan 2004
    Posts
    10

    Catching CTRL+C signal...

    I am using "select()" to select from a read list.
    the timeval is set to 1/2 second.

    I have many objects allocated in which a CTRL+C or CTRL+BREAK would leak if i dont free them up.

    So, what is a good strategy to catch CTRL+C so i may break out of the select() loop and free what needs be.

    example source would be handy, for i have tried using "signal()" and was not able to get it to work appropriatly.

    This source will be running on win32 as well as linux.

    thanks ahead of time.
    andy

  2. #2
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,019
    Have a look at atexit. You basically set up a callback which executes when the program terminates. Sorry - I don't have an example.
    Succinct is verbose for terse

  3. #3
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,019
    Actually, I do have an example. Have a look at memory leaks
    I use atexit to print out the memory leaks.
    Succinct is verbose for terse

  4. #4
    Join Date
    Jan 2004
    Posts
    10
    neat function, havnt seen that one before...
    but that doesnt solve the problem when waiting on select() and a signal happens.

    I thought of something while staring at the wall... would it be that im not handing the execpt read list in select(), so when a user tries to CTRL+C out, its trying to write the the except read list, but since its null, it is crashing?

    i will have to do some studying.
    Thanks again for the input, and any other input you may be able to give.
    andy

  5. #5
    Join Date
    Jan 2004
    Posts
    10
    Just to let you know...

    The atexit() function adds the function func to a list of functions to be called without parameters on normal termination of the program. Normal termination occurs by either a call to exit() or a return from main(). The functions are called in the reverse order of their registration.
    so it wont catch CTRL+C

  6. #6
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    I don't know if my Console Signals Handler sample helps but you can look at it in case it does.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  7. #7
    Join Date
    Jan 2004
    Posts
    10
    ok i feel like a doofus...
    It was catching the signals in release mode.. the only time it would "crash" was in debug. And the msg was telling me "1st chance exception - control-c"

    So, that problem is solved. but a new one has arrived. Does anyone know how to disable 1st chance exceptions in debug mode using microsoft visual C++ 6.0

    I have _CrtSetDbgFlag() enabled and would like to see the memory leaks, but if i hit ctrl+c, it gives me that message and quits. i need it to cleanly exit as it does in release mode.

    thanks
    andy

  8. #8
    Join Date
    Jan 2004
    Posts
    10
    boy, not my day! i am zero for three today... you can just hit "ok" the the exception and the compiler just asks if you want your program to handle it anyway.

    lol.. ok i am good to go. sometimes it just helps to type stuff out to other people

    thanks all
    andy

  9. #9
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,019
    On Unix, You could try catching sighup, sigint or sigterm. One of them might equate to ctrl-C. sigkill or kill -9 isn't catchable. If ctrl-c equates to that, then you cannot catch it. The OS has been made this way so it is possible to kill rogue processes and the rogue processes cannot trap that signal to stop themselves from being killed.

    On Windows, Ctrl-C is copy, Ctrl-X is cut, Ctrl-V is paste so you can probably catch it but it won't do what you want. In windows programming: you could try trapping the WM_KEYDOWN. The Ctrl-Break sequence is called VK_CANCEL.
    Succinct is verbose for terse

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center