summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Butler <butlermichael@google.com>2018-05-03 12:05:40 -0700
committerMichael Butler <butlermichael@google.com>2018-05-08 14:14:12 -0700
commitaec086a4a628f1f186323f7db8f4574515d74f24 (patch)
tree38bdfe56d09d4ce867733d7e2ea0e5056ac355b0
parentf1a4694344c8acf2b157724ddf7df6ce2d1ac7bb (diff)
downloadml-aec086a4a628f1f186323f7db8f4574515d74f24.tar.gz
Free wrapper resource before assignment
Any resources owned by "this" (lefthand side of assignment) are never released (as the destructor would do), but are overwritten by the move-assignment process and are therefore leaked. This CL frees the resource before assignment. Additionally, it adds missing ownership documentation for Compilation. Bug: 79205067 Test: mma Test: NeuralNetworksTest_static Change-Id: Iacd561adee5205cb039fc7dea4fd1a816ca8d641
-rw-r--r--nn/runtime/include/NeuralNetworksWrapper.h11
1 files changed, 11 insertions, 0 deletions
diff --git a/nn/runtime/include/NeuralNetworksWrapper.h b/nn/runtime/include/NeuralNetworksWrapper.h
index c859baffc..6351726b9 100644
--- a/nn/runtime/include/NeuralNetworksWrapper.h
+++ b/nn/runtime/include/NeuralNetworksWrapper.h
@@ -91,6 +91,7 @@ public:
Memory(Memory&& other) { *this = std::move(other); }
Memory& operator=(Memory&& other) {
if (this != &other) {
+ ANeuralNetworksMemory_free(mMemory);
mMemory = other.mMemory;
mValid = other.mValid;
other.mMemory = nullptr;
@@ -127,6 +128,7 @@ public:
Model(Model&& other) { *this = std::move(other); }
Model& operator=(Model&& other) {
if (this != &other) {
+ ANeuralNetworksModel_free(mModel);
mModel = other.mModel;
mNextOperandId = other.mNextOperandId;
mValid = other.mValid;
@@ -226,6 +228,7 @@ public:
Event(Event&& other) { *this = std::move(other); }
Event& operator=(Event&& other) {
if (this != &other) {
+ ANeuralNetworksEvent_free(mEvent);
mEvent = other.mEvent;
other.mEvent = nullptr;
}
@@ -255,12 +258,19 @@ public:
~Compilation() { ANeuralNetworksCompilation_free(mCompilation); }
+ // Disallow copy semantics to ensure the runtime object can only be freed
+ // once. Copy semantics could be enabled if some sort of reference counting
+ // or deep-copy system for runtime objects is added later.
Compilation(const Compilation&) = delete;
Compilation& operator=(const Compilation&) = delete;
+ // Move semantics to remove access to the runtime object from the wrapper
+ // object that is being moved. This ensures the runtime object will be
+ // freed only once.
Compilation(Compilation&& other) { *this = std::move(other); }
Compilation& operator=(Compilation&& other) {
if (this != &other) {
+ ANeuralNetworksCompilation_free(mCompilation);
mCompilation = other.mCompilation;
other.mCompilation = nullptr;
}
@@ -303,6 +313,7 @@ public:
Execution(Execution&& other) { *this = std::move(other); }
Execution& operator=(Execution&& other) {
if (this != &other) {
+ ANeuralNetworksExecution_free(mExecution);
mExecution = other.mExecution;
other.mExecution = nullptr;
}