Skip to content

Commit

Permalink
Fixes #126
Browse files Browse the repository at this point in the history
Fixed memory leak in CheckServiceStatus function
  • Loading branch information
nefarius committed Nov 12, 2023
1 parent 3d398c8 commit 0141e84
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 42 deletions.
83 changes: 43 additions & 40 deletions Watchdog/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include <initguid.h>
#include <devguid.h>

#include <scope_guard.hpp>

#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>

Expand All @@ -29,50 +27,55 @@ class WatchdogTask : public Poco::Task
SC_HANDLE sch = nullptr;
SC_HANDLE svc = nullptr;

sg::make_scope_guard(
[sch, svc]() noexcept
{
if (sch) CloseServiceHandle(svc);
if (svc) CloseServiceHandle(svc);
});

sch = OpenSCManager(
nullptr,
nullptr,
SC_MANAGER_ALL_ACCESS
);
if (sch == nullptr)
__try
{
return GetLastError();
}
sch = OpenSCManager(
nullptr,
nullptr,
SC_MANAGER_ALL_ACCESS
);
if (sch == nullptr)
{
return GetLastError();
}

svc = OpenService(
sch,
serviceName.c_str(),
SC_MANAGER_ALL_ACCESS
);
if (svc == nullptr)
{
return GetLastError();
}
svc = OpenService(
sch,
serviceName.c_str(),
SC_MANAGER_ALL_ACCESS
);
if (svc == nullptr)
{
return GetLastError();
}

SERVICE_STATUS_PROCESS stat;
DWORD needed = 0;
BOOL ret = QueryServiceStatusEx(
svc,
SC_STATUS_PROCESS_INFO,
(BYTE*)&stat,
sizeof stat,
&needed
);
if (ret == 0)
{
return GetLastError();
}

SERVICE_STATUS_PROCESS stat;
DWORD needed = 0;
BOOL ret = QueryServiceStatusEx(
svc,
SC_STATUS_PROCESS_INFO,
(BYTE*)&stat,
sizeof stat,
&needed
);
if (ret == 0)
serviceState = stat.dwCurrentState;

return ERROR_SUCCESS;
}
__finally
{
return GetLastError();
if (svc)
CloseServiceHandle(svc);
if (sch)
CloseServiceHandle(sch);
}

serviceState = stat.dwCurrentState;

return ERROR_SUCCESS;
return GetLastError();
}

public:
Expand Down
3 changes: 1 addition & 2 deletions Watchdog/vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"dependencies": [
"poco",
"spdlog",
"winreg",
"scope-guard"
"winreg"
]
}

0 comments on commit 0141e84

Please sign in to comment.