[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

MemoryLeak on QueryServiceStatus

Posted on 2014-08-21
3
Medium Priority
?
333 Views
Last Modified: 2014-08-21
Hi

for tests I wrote a program with the following code (timerinterval is 1 second):
function ServiceGetStatus(sMachine, sService: string): DWORD;
 var
   SCManHandle, SvcHandle: SC_Handle;
   SS: TServiceStatus;
   dwStat: DWORD;
begin
   dwStat := 0;
   // Open service manager handle.
   SCManHandle := OpenSCManager(pChar(sMachine), nil, SC_MANAGER_CONNECT);
   if (SCManHandle > 0) then
   begin
     SvcHandle := OpenService(SCManHandle, pChar(sService), SERVICE_QUERY_STATUS);
     // if Service installed
     if (SvcHandle > 0) then
     begin
       // SS structure holds the service status (TServiceStatus);
       if (QueryServiceStatus(SvcHandle, SS)) then
       begin
         dwStat := ss.dwCurrentState;
       end;
    end;
   end;
   Result := dwStat;
end;

procedure TForm13.Timer1Timer(Sender: TObject);
begin
  timer1.enabled := false;
  ServiceGetStatus('','myService');
  timer1.enabled := true;
end;

Open in new window

If I execute this program the used memory increases more and more so there seems to be a memory leak. But where? Thanks for your help, Peter
0
Comment
Question by:formi
3 Comments
 
LVL 31

Assisted Solution

by:Zoppo
Zoppo earned 1000 total points
ID: 40276510
Hi formi,

I'm not sure but I guess the problem is you don't close the handle returned by OpenService, you should close each aquired handle with a call to CloseServiceHandle.

Hope that helps,

ZOPPO
0
 
LVL 28

Accepted Solution

by:
Sinisa Vuk earned 1000 total points
ID: 40276656
Both OpenSCManager and OpenService must be closed using CloseServiceHandle:
function ServiceGetStatus(sMachine, sService: string): DWORD;
 var
   SCManHandle, SvcHandle: SC_Handle;
   SS: TServiceStatus;
   dwStat: DWORD;
begin
   dwStat := 0;
   // Open service manager handle.
   SCManHandle := OpenSCManager(pChar(sMachine), nil, SC_MANAGER_CONNECT);
   if (SCManHandle > 0) then
   begin
     SvcHandle := OpenService(SCManHandle, pChar(sService), SERVICE_QUERY_STATUS);
     // if Service installed
     if (SvcHandle > 0) then
     begin
       // SS structure holds the service status (TServiceStatus);
       if (QueryServiceStatus(SvcHandle, SS)) then
       begin
         dwStat := ss.dwCurrentState;
       end;
       CloseServiceHandle(SvcHandle);
    end;
    CloseServiceHandle(SCManHandle);
   end;
   Result := dwStat;
end;

Open in new window

0
 

Author Closing Comment

by:formi
ID: 40278321
of course that was the Problem. Thanks!
0

Featured Post

Industry Leaders: 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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month18 days, 1 hour left to enroll

830 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