PDA

View Full Version : Will system call cause memory leak?



baobui
2nd June 2011, 13:41
Hi all,

In my application I has to use many call to external command using the system("") call. My problem is even though this command is already finish, it still appear as a running job when I type the ps command on the console. For example:

79 root /sbin/getty 38400 tty2
82 root -sh
83 root /sbin/syslogd -n -m 0
84 root /sbin/klogd -n
85 root /usr/bin/tail -f /var/log/messages
105 root /AutoPrint -qws
106 root [flush-179:0]
107 root /AutoPrint -qws
108 root /AutoPrint -qws
109 root /AutoPrint -qws
112 root [kjournald]
122 root ps

The PID from 107 to 109 came from my application (AutoPrint) after a system() command. If I run it long enough, there are many jobs with the same name (/AutoPrint -qws) come out when I type ps command. Is it a memory leak?

Thank you very much

mvuori
2nd June 2011, 18:07
system() is a C/C++ runtime API and/or an operating system API and of course should not leak memory or leave processes hanging, but what it does, depends on the call used and the operating system.

You might want to use QProcess instead of system() for better control over the processes.

SixDegrees
2nd June 2011, 23:18
If the system call returns, the command should be finished and the shell it launched in closed, unless the command was launched in the background. In this case, the call would likely never turn and your program would hang.

Also, ps seems to be reporting that instances of YOUR program are persisting rather than the commands launched by it, assuming your application is named AutoPrint. If so, then your program isn't exiting properly and the problem has nothing to do with system calls.