Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Memory leak with CreateProcess()

676 views
Skip to first unread message

lugeon

unread,
Mar 27, 2006, 7:31:02 AM3/27/06
to
I get a 388bytes memory leak each time I call the CreateProcess() function.
Here is my following code:

void main(int argc, char* argv[])
{
STARTUPINFO si; // = { sizeof(si) };
PROCESS_INFORMATION pi;

while (true) {
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );

if(CreateProcess(NULL,"C:\\temp\\TestFile.exe", NULL, NULL, FALSE, 0,
NULL, NULL, &si, &pi))
{
WaitForSingleObject(pi.hProcess, INFINITE);

DWORD ExitCode;
GetExitCodeProcess(pi.hProcess, &ExitCode);

CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
else
{
cout << "Error CreateProcess()..." << endl;
}
}
}

I use Visual .Net and Windows 2000 Professional. With XP or XP Embedded, the
problem is still here.

Can anybody tell me what is the trouble with my code?

adeb...@club-internet.fr

unread,
Mar 27, 2006, 10:05:18 AM3/27/06
to

lugeon wrote:
> I get a 388bytes memory leak each time I call the CreateProcess() function.
> Here is my following code:
<snip>

The code looks just fine. How do you measure those 388 bytes?

Arnaud
MVP - VC

Alexander Grigoriev

unread,
Mar 27, 2006, 10:58:09 AM3/27/06
to
By the way, you're NOT supposed to pass a constant string to CreateProcess.

"lugeon" <lug...@discussions.microsoft.com> wrote in message
news:433B7B08-FF3F-4CDD...@microsoft.com...

lugeon

unread,
Mar 27, 2006, 11:22:05 AM3/27/06
to
You are right...

But I write here the simplest code I could wrote. My leak is still present
if I replace my constant string by a LPTSTR.

Am I the only one to get a leak with this code, or has anybody the same
trouble?

My question is: It is the code itself, the configuration of the compiler, or
something else I don't understand. I have this leak with different computer,
and I also compile with different computer.

If you run this simple code, you will see the memory for this application
that will increase with the task manager. No need to use purify or anything
else to test.

"Alexander Grigoriev" a écrit :

qfel

unread,
Mar 27, 2006, 11:24:37 AM3/27/06
to
> By the way, you're NOT supposed to pass a constant string to
> CreateProcess.
Applies only to Unicode version.

chpichaud

unread,
Mar 27, 2006, 12:24:31 PM3/27/06
to
The 2nd parameter or CreateProcess is [in, out] so use a local
variable.

<<
lpCommandLine
[in, out] Pointer to a null-terminated string that specifies the
command line to execute. The maximum length of this string is 32K
characters.
Windows 2000: The maximum length of this string is MAX_PATH
characters.
The Unicode version of this function, CreateProcessW, will fail if this
parameter is a const string.

The lpCommandLine parameter can be NULL. In that case, the function
uses the string pointed to by lpApplicationName as the command line.

>>

Else:
put your string into a local TCHAR[].
Remove the std::cout function.
try it in ANSI and Unicode.

Christophe Pichaud

Luc Kumps

unread,
Mar 27, 2006, 12:41:37 PM3/27/06
to
lugeon wrote:
> You are right...
>
> But I write here the simplest code I could wrote. My leak is still
> present if I replace my constant string by a LPTSTR.
>
> Am I the only one to get a leak with this code, or has anybody the
> same trouble?

I added

#include <iostream>
#include <windows.h>
using namespace std;

in front of your code, and compiled with "cl -GX tmp.cpp"

Then I made a TestFile.exe which does Sleep(200) and ran the program. Task
Manager says VM size=156K. It stays that way forever...

Luc K


Luc Kumps

unread,
Mar 27, 2006, 1:33:46 PM3/27/06
to

WAIT! I made a mistake!
VM size *does* increase, just like you said it does!

Luc K


Luc Kumps

unread,
Mar 27, 2006, 3:05:27 PM3/27/06
to
lugeon wrote:
> I get a 388bytes memory leak each time I call the CreateProcess()
> function. Here is my following code:

I was able to reproduce this problem.
To my surprise, CreateProcess keeps eating memory!!!

I also found a simple workaround, but I would *really* like to get an
explanation for this!

Simply add
#include <objbase.h>
to your #include statements and at the start of your main program add one
line
HRESULT hr = CoInitializeEx(NULL,COINIT_MULTITHREADED);
(or any other call that initializes the COM library).

Compile with _WIN32_DCOM defined and your leak is gone. Tested on Win2000
and WinXp.
Just don't ask me why!!!

Even simpler workaround: simply call ::SysAllocString(L"abc"); at the start
of your program...

BTW, I tried to use CreateProcessW, it has the same 'feature'!

Luc K


Arnaud Debaene

unread,
Mar 27, 2006, 4:34:26 PM3/27/06
to
lugeon wrote:
> "adeb...@club-internet.fr" a écrit :
> With Purify

What does is says exactly? Where does it locate the leak?

PS : Please don't top-post

Arnaud
MVP - VC


Norman Diamond

unread,
Mar 27, 2006, 9:02:00 PM3/27/06
to
"qfel" <q_...@aster.pl> wrote in message
news:e093k7$68r$1...@inews.gazeta.pl...
[Alexander Grigoriev:]

>> By the way, you're NOT supposed to pass a constant string to
>> CreateProcess.
>
> Applies only to Unicode version.

Applies only to SOME Unicode versions.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecoreos5/html/wce50lrfCreateProcess.asp
<< BOOL CreateProcess(
<< LPCWSTR pszImageName,
<< LPCWSTR pszCmdLine,
<< [...]
<< pszCmdLine


<< [in, out] Pointer to a null-terminated string that specifies the
<< command line to execute.

I have a vague recollection of some other place too where the API requires a
pointer to constant so that the API can modify the user's constant.

anton bassov

unread,
Mar 27, 2006, 10:12:38 PM3/27/06
to
Hi guys

Judging from what you are saying here, it looks like you just have
discovered one more bug in Windows. Apparently, if
COM library is initialized, execution takes a bit different path, so that
memory leak does not occur. This seems to be the only logical explanation


Regards

Anton Bassov

David J. Craig

unread,
Mar 27, 2006, 11:26:06 PM3/27/06
to
No. Please don't bottom post.

"Arnaud Debaene" <adeb...@club-internet.fr> wrote in message
news:%23vw0rYe...@TK2MSFTNGP11.phx.gbl...

lugeon

unread,
Mar 28, 2006, 1:15:02 AM3/28/06
to
No more memory leak.

Thank you very much for your help! I couldn't have found by myself....

Jochen Kalmbach [MVP]

unread,
Mar 28, 2006, 2:27:25 AM3/28/06
to
Hi Luc!

> I was able to reproduce this problem.
> To my surprise, CreateProcess keeps eating memory!!!

On XP-SP2, I was not able to repro the problem...

Here is my code:

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

int _tmain()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;

ULONG ulCount = 0;

LPTSTR szWritableCmd = new TCHAR[1024];
while(true) {
ZeroMemory(&si, sizeof(si) );


si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi) );

LPCTSTR szCmd = _T("%SystemRoot%\\system32\\cmd.exe /c");
ExpandEnvironmentStrings(szCmd, szWritableCmd, 1024);
if(CreateProcess(NULL, szWritableCmd, NULL, NULL, FALSE, 0,
NULL, NULL, &si, &pi) != FALSE)
{
ulCount++;


WaitForSingleObject(pi.hProcess, INFINITE);
DWORD ExitCode;
GetExitCodeProcess(pi.hProcess, &ExitCode);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

if ((ulCount % 10) == 0)
_tprintf(_T("%d\n"), ulCount);
}
else
{
_tprintf(_T("Error creating process..."));
}
}
delete [] szWritableCmd;
}


Greetings
Jochen

Luc Kumps

unread,
Mar 28, 2006, 4:40:43 AM3/28/06
to
Jochen Kalmbach [MVP] wrote:
> Hi Luc!
>
>> I was able to reproduce this problem.
>> To my surprise, CreateProcess keeps eating memory!!!
>
> On XP-SP2, I was not able to repro the problem...

I can't reproduce it on XP-SP2 either.
By the way, here's the code needed to show the memory usage at runtime:

#include <psapi.h>
using namespace std;

void showMemInfo() {
PROCESS_MEMORY_COUNTERS pmc;
if ( GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)) ) {
cout << pmc.PagefileUsage << ".";
}
}

On my Win2000-SP4 this eats memory in 'steps' of 4096 or 8192 bytes...
On a Windows NT 4.0 system (tested in VMWARE), no leak either.

Luc


Hector Santos

unread,
Mar 28, 2006, 7:56:22 AM3/28/06
to
Touché!! <g>


"David J. Craig" <Da...@yoshimuni.com> wrote in message
news:#9AH8#hUGHA...@tk2msftngp13.phx.gbl...

David Jones

unread,
Mar 28, 2006, 9:00:37 AM3/28/06
to
David J. Craig wrote:
> No. Please don't bottom post.
>
> "Arnaud Debaene" <adeb...@club-internet.fr> wrote in message
> news:%23vw0rYe...@TK2MSFTNGP11.phx.gbl...
>
>>PS : Please don't top-post

*ever* need to read the quoted text to get context for your reply.
message might get lost or arrive out of order... no one would
Outlook does it automatically; we don't do it because the parent
(top-to-bottom, left-to-right). After all, we only quote because
the fact that it follows the order in which people normally read
practiced in virtually all public non- microsoft.* groups, plus
read. Nevermind the fact that it's a usenet convention and is
Yes, don't bottom post. That makes everything so much harder to

David

Scherbina Vladimir

unread,
Mar 28, 2006, 9:24:11 AM3/28/06
to
guys, slow down, that the matter of style, and information is not losted
when one is making top ot bottom posting ...

--
Vladimir
http://spaces.msn.com/vladimir-scherbina/


David J. Craig

unread,
Mar 28, 2006, 12:09:24 PM3/28/06
to
There are two reasons for my preferences. I use Outlook Express for some
newsgroups and Agent for others. Microsoft has decreed by the way OE
handles replies that top posting is their standard and this is a Microsoft
newsgroup which I access via msnews. The other is that since I use a news
reader I have the history available. If I need to read the old stuff, I can
scroll down, but I hate to do all that scrolling just to read the latest.

There are languages that use top to bottom - right to left, such as Japanese
in normal written communications. They do use the left to right, top to
bottom, but they also use right to left in some circumstances.

"David Jones" <nc...@tadmas.com> wrote in message
news:SvbWf.3450$fS6.3068@dukeread11...

Hector Santos

unread,
Mar 29, 2006, 1:43:55 AM3/29/06
to

"David Jones" <nc...@tadmas.com> wrote:

> David J. Craig wrote:
>>
>> No. Please don't bottom post.
>>
>> "Arnaud Debaene" <adeb...@club-internet.fr> wrote in message
>>

>>>PS : Please don't top-post
>
> *ever* need to read the quoted text to get context for your reply.
> message might get lost or arrive out of order... no one would
> Outlook does it automatically; we don't do it because the parent
> (top-to-bottom, left-to-right). After all, we only quote because
> the fact that it follows the order in which people normally read
> practiced in virtually all public non- microsoft.* groups, plus
> read. Nevermind the fact that it's a usenet convention and is
> Yes, don't bottom post. That makes everything so much harder to
>
> David

We write online and offline mail readers since the 80s even before Microsoft
had its own. It is not a usenet convention. Before usenet, we had
corporate email systems. Top posting is more closer associated with
corporate electronic mail. The concept of "quoting", inline quoting,
bottom posting, etc was something that evolved over time.

I will venture to say that most people over 35-40, who are not experience in
the public discussion forums, whether it was online, fidonet echos,
newsgroups, etc, all use top posting replies simply because that is how it
was done with their corporate or work mail system.

You have to remember that generally most messaging is single minded, so in
most cases, top posting is fine. Sometimes you just don't want to destroy
the original content so that there is nothing taken out of content.

It is when you start to get into a "chat" like messaging where you have
multiple thoughts, concepts, long messages, maybe where a top post becomes
almost like a "disservice" to the OP sometimes because it may not cover all
the OP concepts, or maybe you don't want to either.

Just like this message with where I am covering several points, some will
top post with one or more points, others will selected quote using inline or
bottom posting.

In fact, in our old Silver Xpress Mail Reader, it has a Smart "Thought"
Quoting system where it helps you highlight and select words, sentences and
paragraphs as you read the message and allows you to quote each one
individually building it up so that the final buffer has a nicely quoted
reply messages where you can do your final edit prior to saving and sending.
This was original done for the Visually Impaired (Blind Users) as part of
our SFI (Speech Friendly Interface) system. They needed a way to HEAR each
sentence or paragraph, etc, pausing at each one so they can QUOTE it and
write a response. They loved it, and even many of the non-blind used it
too!

In any case, I've seen it all during our 25+ year history with millions of
users over the years using our mail products. I would say, at this point, I
say anyone who complains about top or bottom posting is just trying to play
"god", trying to show they know something more than the other, and in my
view, are just ignorant. However, I do believe that once you do get
involved in active participation that you use some common sense aesthetics
so that people can read your mail without straining. Typically most people
will learn for themselves just by hanging out, reading other messages nicely
quoted, etc.

--
Hector Santos, Santronics Software, Inc.
http://www.santronics.com

0 new messages