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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:52:40+00:00 2026-05-23T04:52:40+00:00

I have a simple program which creates a thread, and waits when this thread

  • 0

I have a simple program which creates a thread, and waits when this thread ends, and then the program also ends. When I compile this program with C(gcc) compiler, and check it with valgrind, no problem is occured, but when I compile it with C++(g++) compiler, and check with valgrind, it shows that my program has memory leaks. What could be the problem?

Here is my program,

#include <pthread.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>


unsigned char b = 0;


void* threadfunc1( void *pVoid )
{
    while( b == 0 )
    {
    usleep(10000);
    }
    pthread_exit(0);
}



int main(void)
{

    int status;
    pthread_attr_t tattr;
    pthread_t   thread1;

    status = pthread_attr_init(&tattr);
    status = pthread_attr_setdetachstate(&tattr,PTHREAD_CREATE_JOINABLE);
    status = pthread_attr_setscope(&tattr,PTHREAD_SCOPE_SYSTEM);

    if( pthread_create( &thread1, &tattr, threadfunc1, NULL ) != 0 )
    {
        exit(1);
    }

    usleep(1000000);
    b = 1;
    pthread_join( thread1, NULL);
    usleep(2000000);

    return 0;
}

this is the result, when I compile it using g++, and check in valgrind

==7658== HEAP SUMMARY:
==7658==     in use at exit: 28 bytes in 1 blocks
==7658==   total heap usage: 2 allocs, 1 frees, 172 bytes allocated
==7658== 
==7658== 28 bytes in 1 blocks are still reachable in loss record 1 of 1
==7658==    at 0x4024C1C: malloc (vg_replace_malloc.c:195)
==7658==    by 0x400C01E: _dl_map_object_deps (dl-deps.c:506)
==7658==    by 0x40117E0: dl_open_worker (dl-open.c:297)
==7658==    by 0x400D485: _dl_catch_error (dl-error.c:178)
==7658==    by 0x401119F: _dl_open (dl-open.c:586)
==7658==    by 0x428D0C1: do_dlopen (dl-libc.c:86)
==7658==    by 0x400D485: _dl_catch_error (dl-error.c:178)
==7658==    by 0x428D1C0: dlerror_run (dl-libc.c:47)
==7658==    by 0x428D2DA: __libc_dlopen_mode (dl-libc.c:160)
==7658==    by 0x4048876: pthread_cancel_init (unwind-forcedunwind.c:53)
==7658==    by 0x40489EC: _Unwind_ForcedUnwind (unwind-forcedunwind.c:126)
==7658==    by 0x40464B7: __pthread_unwind (unwind.c:130)
==7658== 
==7658== LEAK SUMMARY:
==7658==    definitely lost: 0 bytes in 0 blocks
==7658==    indirectly lost: 0 bytes in 0 blocks
==7658==      possibly lost: 0 bytes in 0 blocks
==7658==    still reachable: 28 bytes in 1 blocks
==7658==         suppressed: 0 bytes in 0 blocks

So, what I’m doing wrong, is it my error or …, why when I compile it with gcc no problems are occured and when I compile it using C++ memory leaks are present?

  • 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-05-23T04:52:41+00:00Added an answer on May 23, 2026 at 4:52 am

    Your program don’t have memory leaks, you have

    ==7658==    definitely lost: 0 bytes in 0 blocks
    ==7658==    indirectly lost: 0 bytes in 0 blocks
    ==7658==      possibly lost: 0 bytes in 0 blocks
    

    “still reachable” does not mean memory leak.

    There are much questions here about “still reachable” by valgrind. Some of them:

    • Still Reachable Leak detected by Valgrind
    • Still reachable in valgrind
    • Valgrind claims there is unfreed memory. Is this bad?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple script which is used to start another program. This other
Have a look at this very simple example WPF program: <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
I have a simple Delphi program that I'm working on, in which I am
I have a web creation program which, when building a site, creates hundreds of
I am writing a simple OpenGL program with Qt Creator which basically creates a
I have simple test project which creates a frame and adds one JTextPane to
I have a simple Tray icon program that opens a site using System.Diagnostics.Process.Start(URL) And
I have two simple while loops in my program that I feel ought to
I have the need to express simple conditionals as program input. The input must
I have a relatively simple select statement in a VB6 program that I have

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.