Solved

CreateProcess resource allocation issue

Posted on 2004-04-30
4
200 Views
Last Modified: 2010-04-05
Hi,

I am creating a process from my Delphi 7 application. I wait for it to complete and then continue execution of my program. Here is my code:

FillChar(StartupInfo, SizeOf(StartupInfo), 0);
FillChar(ProcessInfo, SizeOf(ProcessInfo), 0);
if CreateProcess(nil, PChar(CommandLine), nil, nil, True, 0, nil, nil, StartupInfo, ProcessInfo) then
  repeat
    Application.ProcessMessages;
    GetExitCodeProcess(ProcessInfo.hProcess, ExitCode);
  until (ExitCode <> STILL_ACTIVE) or Application.Terminated;

It works fine. My problem is that Memory Sleuth indicates that there is a Thread Handle and a Process Handle still allocated once my application terminates.

My question is, how do I free these resources?

Regards,

Bill
0
Comment
Question by:wipnav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 17

Expert Comment

by:geobul
ID: 10961724
Hi,

Add these two lines at the end of your code:

CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread );

Regards, Geo
0
 
LVL 17

Accepted Solution

by:
geobul earned 250 total points
ID: 10961750
if CreateProcess(nil, PChar(CommandLine), nil, nil, True, 0, nil, nil, StartupInfo, ProcessInfo) then
  try
    repeat
      Application.ProcessMessages;
      GetExitCodeProcess(ProcessInfo.hProcess, ExitCode);
    until (ExitCode <> STILL_ACTIVE) or Application.Terminated;
  finally
    CloseHandle(ProcessInfo.hProcess);
    CloseHandle(ProcessInfo.hThread );
  end;

Regards, Geo
0
 
LVL 1

Author Comment

by:wipnav
ID: 10961914
Geo,

Perfect!

Thank you very much.

Regards,

Bill
0
 
LVL 17

Expert Comment

by:geobul
ID: 10962050
You are welcome :-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question