summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2013-07-06 16:20:03 +0200
committerStanislaw Halik <sthalik@misaki.pl>2013-07-06 16:20:03 +0200
commitfec421b3a47055e1d819c7fb34aabae040aade03 (patch)
treea096255e3e92b8cbecc64cbbf04dd5760bf1f70b
parent778deec98a4e71974d7bd568cfaf9c5c88f38a4b (diff)
Prevent handle leak in error path
-rw-r--r--ftnoir_protocol_sc/ftnoir_protocol_sc.cpp45
1 files changed, 28 insertions, 17 deletions
diff --git a/ftnoir_protocol_sc/ftnoir_protocol_sc.cpp b/ftnoir_protocol_sc/ftnoir_protocol_sc.cpp
index a178e009..3069a3bf 100644
--- a/ftnoir_protocol_sc/ftnoir_protocol_sc.cpp
+++ b/ftnoir_protocol_sc/ftnoir_protocol_sc.cpp
@@ -150,18 +150,14 @@ PDWORD_PTR MsgResult = 0;
}
}
-//
-// Returns 'true' if all seems OK.
-//
-bool FTNoIR_Protocol::checkServerInstallationOK()
-{
- HANDLE hactctx = INVALID_HANDLE_VALUE;
- ULONG_PTR actctx_cookie = NULL;
- if (!SCClientLib.isLoaded())
- {
+class ActivationContext {
+public:
+ ActivationContext(const int resid) {
+ hactctx = INVALID_HANDLE_VALUE;
+ actctx_cookie = NULL;
ACTCTXA actx = {0};
actx.cbSize = sizeof(ACTCTXA);
- actx.lpResourceName = MAKEINTRESOURCEA(142);
+ actx.lpResourceName = MAKEINTRESOURCEA(resid);
actx.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID;
QString path = QCoreApplication::applicationDirPath() + "/opentrack-proto-simconnect.dll";
QByteArray name = QFile::encodeName(path);
@@ -177,11 +173,32 @@ bool FTNoIR_Protocol::checkServerInstallationOK()
} else {
qDebug() << "SC: can't create win32 activation context";
}
-
+ }
+ ~ActivationContext() {
+ if (hactctx != INVALID_HANDLE_VALUE)
+ {
+ DeactivateActCtx(0, actctx_cookie);
+ ReleaseActCtx(hactctx);
+ }
+ }
+private:
+ ULONG_PTR actctx_cookie;
+ HANDLE hactctx;
+};
+
+//
+// Returns 'true' if all seems OK.
+//
+bool FTNoIR_Protocol::checkServerInstallationOK()
+{
+ if (!SCClientLib.isLoaded())
+ {
qDebug() << "SCCheckClientDLL says: Starting Function";
SCClientLib.setFileName("SimConnect.DLL");
+ ActivationContext ctx(142);
+
if (!SCClientLib.load()) {
qDebug() << "SC load" << SCClientLib.errorString();
return false;
@@ -242,12 +259,6 @@ bool FTNoIR_Protocol::checkServerInstallationOK()
}
qDebug() << "FTNoIR_Protocol::checkServerInstallationOK() says: SimConnect functions resolved in DLL!";
-
- if (hactctx != INVALID_HANDLE_VALUE)
- {
- DeactivateActCtx(0, actctx_cookie);
- ReleaseActCtx(hactctx);
- }
return true;
}