diff options
author | Dan Goodliffe <daniel.goodliffe@pressassociation.com> | 2015-09-01 10:43:32 +0100 |
---|---|---|
committer | Dan Goodliffe <daniel.goodliffe@pressassociation.com> | 2015-09-01 16:56:11 +0100 |
commit | 16b056dec27672e81e12075849bf4844ae3ce377 (patch) | |
tree | 9a49216e2e6d19629fb821388c118e842569ad23 | |
parent | Typedef the stream, not the source (diff) | |
download | libadhocutil-16b056dec27672e81e12075849bf4844ae3ce377.tar.bz2 libadhocutil-16b056dec27672e81e12075849bf4844ae3ce377.tar.xz libadhocutil-16b056dec27672e81e12075849bf4844ae3ce377.zip |
Don't free the context's stack while it's still being executed
-rw-r--r-- | libadhocutil/runtimeContext.cpp | 16 | ||||
-rw-r--r-- | libadhocutil/runtimeContext.h | 2 | ||||
-rw-r--r-- | libadhocutil/unittests/testContext.cpp | 3 |
3 files changed, 17 insertions, 4 deletions
diff --git a/libadhocutil/runtimeContext.cpp b/libadhocutil/runtimeContext.cpp index cb938d4..4cb5a17 100644 --- a/libadhocutil/runtimeContext.cpp +++ b/libadhocutil/runtimeContext.cpp @@ -2,6 +2,7 @@ #include <stdexcept> RuntimeContext::RuntimeContext(size_t stacksize) : + completed(false), swapped(false) { stack = malloc(stacksize); @@ -26,17 +27,26 @@ RuntimeContext::swapContext() swapcontext(&ctxInitial, &ctxCallback); } else { - if (stack) { + if (!completed) { swapcontext(&ctxCallback, &ctxInitial); + if (completed) { + free(stack); + stack = nullptr; + } } } } +bool +RuntimeContext::hasCompleted() const +{ + return completed; +} + void RuntimeContext::callbackWrapper(RuntimeContext * rc) { rc->callback(); - free(rc->stack); - rc->stack = nullptr; + rc->completed = true; } diff --git a/libadhocutil/runtimeContext.h b/libadhocutil/runtimeContext.h index aa424d4..b7ce0a3 100644 --- a/libadhocutil/runtimeContext.h +++ b/libadhocutil/runtimeContext.h @@ -11,6 +11,7 @@ class DLL_PUBLIC RuntimeContext { virtual ~RuntimeContext(); void swapContext(); + bool hasCompleted() const; protected: DLL_PRIVATE virtual void callback() = 0; @@ -21,6 +22,7 @@ class DLL_PUBLIC RuntimeContext { void * stack; ucontext_t ctxInitial; ucontext_t ctxCallback; + bool completed; bool swapped; }; diff --git a/libadhocutil/unittests/testContext.cpp b/libadhocutil/unittests/testContext.cpp index 727425e..de76493 100644 --- a/libadhocutil/unittests/testContext.cpp +++ b/libadhocutil/unittests/testContext.cpp @@ -3,7 +3,7 @@ #include "runtimeContext.h" -class TestRuntimeContext : RuntimeContext { +class TestRuntimeContext : public RuntimeContext { public: void run() { @@ -32,5 +32,6 @@ BOOST_AUTO_TEST_CASE ( basic ) TestRuntimeContext trc; trc.run(); BOOST_REQUIRE_EQUAL("aebfcd", trc.log); + BOOST_REQUIRE(trc.hasCompleted()); } |