LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 03-23-2009, 10:45 AM   #1
likeuclinux
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Rep: Reputation: 0
Lightbulb Interesting question of memory leak detected by vagrind of forked process


I know If I malloc a memory, I need to free both in parent and forked process, but how about there is no explicit malloc&new call, here is the simple code valgrind throw me the warning of possibly memory loss:

<verbatim>
#include <string>
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sys/wait.h>
#include <sched.h>


using namespace std;
struct WorkData {
int cpu;
string commandStr;
};

static void* work( void* data )
{
}
#define MAX_CONCURRENCY 20
int main() {
WorkData workData[ MAX_CONCURRENCY ];
pid_t pid[ MAX_CONCURRENCY ];

int curLevel = 0;
workData[curLevel].commandStr = string("runMM5 1 2 3 4 5");
workData[curLevel].cpu = curLevel;
if ( (pid[curLevel] = fork()) == 0 ) {
work( reinterpret_cast<void*> ( &(workData[curLevel])));
exit( 0 );
}

}

</verbatim>

build:
g++ -ggdb -g -o test.exe test.cpp

run:
valgrind --leak-check=yes --trace-children=yes ./test.exe


==27730== 41 bytes in 1 blocks are possibly lost in loss record 1 of 1
[rong@tux7 tmp]$ ==27730== at 0x4A06205: operator new(unsigned long) (vg_replace_malloc.c:167)
==27730== by 0x34FCC9B8A0: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /usr/lib64/libstdc++.so.6.0.8)
==27730== by 0x34FCC9C3A4: (within /usr/lib64/libstdc++.so.6.0.8)
==27730== by 0x34FCC9C551: std::string::string(char const*, std::allocator<char> const&) (in /usr/lib64/libstdc++.so.6.0.8)
==27730== by 0x400A3A: main (test.cpp:30)


If I don't fork, then there is no memory loss, anybody have an idea why this is happening?
 
Old 03-23-2009, 11:51 AM   #2
likeuclinux
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thumbs up ok, I answer my own question

It is turned out to be subtle difference between exit and return for c++ program, exit is for C, for C++, it won't do proper stack unwind.

after change the exit(0) to return 0, the valgrind report no error
 
  


Reply

Tags
memoryleak, valgrind


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
memory leak: Parent killing child process wkhoo Programming 12 06-04-2008 06:39 PM
Interesting question regarding the process in Linux namanhams Programming 7 03-24-2008 11:07 AM
SSH forked process shozmo Linux - Newbie 5 06-23-2006 03:49 AM
memory leak question C/C++ allomeen Programming 10 01-19-2006 09:03 PM
finding memory leak of a running process raees Linux - General 1 03-16-2004 11:43 AM


All times are GMT -5. The time now is 04:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration