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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:24:25+00:00 2026-06-12T13:24:25+00:00

allocated pthread_mutex_t* use the malloc(), when the program aborted or exit, it need to

  • 0

allocated pthread_mutex_t* use the “malloc()”, when the program aborted or exit, it need to free it?
if not free it, it will have problems?
just i know, the system will reclaim memory when your process terminates.
the pthread_mutex_t may have bad effect?

just like the question:

My program (a text-mode web browser) is dynamically allocating memory.

I do free unneeded blocks during runtime, of course. And I do free everything before normal termination – so that memory leak checkers won’t give me false positives (and to be flexible should major refactorings ever become needed).

Now, what I do not do is freeing memory before abnormal termination. (Currently, my program terminates on signals and after failed mallocs/reallocs.)

My question is: Do you consider this bad style? Should I free on abnormal termination?

  • 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-12T13:24:26+00:00Added an answer on June 12, 2026 at 1:24 pm

    My take on all this ‘free everything on normal termination’ mantra, (or even abnormal termination), is that, usually, the downside outweighs the upside:

    1) The OS has alreday been designed, tested and soak-tested by billions of users. It can easily free all the left-over allocated memory after stopping every thread in any state on any core. Can you do this with your user-code – no.

    2) You have to add code to do it. Every time you add code, you potentially add more bugs. Attempting to free all allocated memory in a busy complex, multithreaded system is a nightmare, unless you are an OS. The memory is all over the place, in thread vars, queues etc and may, or may not, being currently accessed by one or more threads. Ownership is uncertain and continually changing. Wot you gonna do?

    3) You have to continually test and extend your shutdown system to ensure that all the memory is freed after changes/enhancements/upgrades.

    4) Continually checking ‘terminate’ flags etc. in threads, just so that the threads can free some memory on shutdown, is extra overhead.

    5) Many libraries do not free all memory when closed anyway. If the library is opaque/translucent, you may not be able to do anything about it.

    6) If a thread throws a serious exception, the memory may be in such a state that attempting to free it from user code will result in yet more exceptions in other threads.

    7) Risk/reward – Nice, clean Valgrind output vs. calls/emails from customers ‘Hey, your app sometimes will not shut down and we have to call an admin to kill-9/TaskManager it’.

    8) Filter and count all the threads on SO re. ‘Cannot stop my thread to shut down my app’.

    With some apps, it is unavoidable that some threads may have to be terminated in a structured manner on normal shutdown, eg. to commit and close DB connections, flush and close files. Freeing memory does not fall into that category, even for normal shutdown.

    If your app is really in serious trouble, you should not try to ‘clean up’ memory – your app is already dirty, needs scrapping and replacing with a new instance.

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

Sidebar

Related Questions

I have allocated a chunk of memory of type char and size is say
How is memory allocated when I use: public class MyClass { public const string
I know that variables allocated on that stack of a function become inaccessible when
I have a structure declared and allocated in this way typedef { char* a;
I'm making a thread class to use as a wrapper for pthreads. I have
I have been trying to debug my code whenever I had free-time for the
Basically I want an arbitrarily large stack. I know that's not possible, but could
I have a dynamically allocated array : myRectangle lastRectanglesArray = new myRectangle[lastMaxLabel]; I would
I've allocated a chuck of memory with char* memoryChunk = malloc ( 80* sizeof(char)
Why do we need boost::thread_specific_ptr, or in other words what can we not easily

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.