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 8230695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:13:14+00:00 2026-06-07T17:13:14+00:00

I have a simple program which uses select and stuff like that for multiplexing

  • 0

I have a simple program which uses select and stuff like that for multiplexing IO.
To interrupt the “server” process, I integrated a sig_handler which reacts on SIGINT.

Each time when memory is allocated, the containing method does the free itself or the calling method.

Using valgrind shows up, that there are some allocation aren’t freed up.
Perhaps there is no need for it, but I want to learn what would be the best way to handle the signal.
It seems that the free calls aren’t invoked when pressing STRG + C.
So it would be senseless to quit loops with a break condition, which was my first approach.

Is there any possibility to clean every thing up, before closing the whole program?

Thanks for any hints and advices.

  • 1 1 Answer
  • 0 Views
  • 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-06-07T17:13:16+00:00Added an answer on June 7, 2026 at 5:13 pm

    Valgrind is just a tool for finding memory leaks, not an oracle whose advice must be heeded. Making a program “Valgrind-clean” is a worthy goal but don’t let it get out of hand. Ask yourself some questions about the program.

    1. Does the program need to do anything when it receives a SIGINT or SIGQUIT or whatever? Does it need to do some kind of clean shutdown? For example, a server might decide to finish handling all open requests, or at least send a shutdown message to connected clients.

    2. Does a sudden termination always leave behind certain blocks? Then you can just quash those reports from Valgrind, without having to spend extra time freeing memory that’s already going to be freed.

    In simple terms, there are only two reasons to call free in a program that is about to exit.

    1. If it’s the easiest way to quash Valgrind messages (that is, without reading the Valgrind manual)

    2. If it makes your code simpler.

    Otherwise, just don’t call free during program exit, since all it will do is burn CPU cycles.

    Handling SIGINT: I can think of four common ways to handle a SIGINT:

    1. Use the default handler. Highly recommended, this requires the least amount of code and is unlikely to result in any abnormal program behavior. Your program will simply exit.

    2. Use longjmp to exit immediately. This is for folk who like to ride fast motorcycles without helmets. It’s like playing Russian roulette with library calls. Not recommended.

    3. Set a flag, and interrupt the main loop’s pselect/ppoll. This is a pain to get right, since you have to twiddle with signal masks. You want to interrupt pselect/ppoll only, and not non-reentrant functions like malloc or free, so you have to be very careful with things like signal masks. Not recommended. You have to use pselect/ppoll instead of select/poll, because the “p” versions can set the signal mask atomically. If you use select or poll, a signal can arrive after you check the flag but before you call select/poll, this is bad.

    4. Create a pipe to communicate between the main thread and the signal handler. Always include this pipe in the call to select/poll. The signal handler simply writes one byte to the pipe, and if the main loop successfully reads one byte from the other end, then it exits cleanly. Highly recommended. You can also have the signal handler uninstall itself, so an impatient user can hit CTRL+C twice to get an immediate exit.

    The two simplest and most fool-proof methods are #1 and #4.

    A program that exits does not have any leaks. Only a running program can have leaks. Once the program exits, all memory is freed (so there is no leak any more).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have avery simple program which uses MediaPLayer. I am new to java and
Below, I have a simple program which uses the CImg library (http://cimg.sourceforge.net/) which iterates
I have a simple program (written in Java) which uses the google protocol buffer
I have been trying to create a simple program with Python which uses OpenCV
I have a simple Delphi program that I'm working on, in which I am
I have written a simple Android application that uses a sqlite database which is
I have a simple program that uses FindWindowEx & strncmp() inside a callback passed
I have an network application which uses a select loop like this: bool shutdown=false;
I have a simple plugin architecture for my program that uses an interface to
I have a simple Python program which uses a read-eval-print loop to read user

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.