Skip to content

Commit

Permalink
Fix delete void* bug
Browse files Browse the repository at this point in the history
  • Loading branch information
daquexian committed May 23, 2018
1 parent 49206ec commit b9ef5fb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
19 changes: 13 additions & 6 deletions dnnlibrary/src/main/cpp/ModelBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ModelBuilder &ModelBuilder::readFromFile(std::string filename) {
char *buffer = new char[len];
// read whole content of a file into buffer
if (ifs.read(buffer, len)) {
bufferPointers.push_back(buffer);
registerBufferPointer(buffer);
return readFromBuffer(buffer);
} else {
throw string("Read file error");
Expand Down Expand Up @@ -760,17 +760,24 @@ int ModelBuilder::compile(uint32_t preference) {
return 0;
}

void ModelBuilder::registerBufferPointer(void *pointer) {
bufferPointers.push_back(pointer);
void ModelBuilder::registerBufferPointer(float *pointer) {
floatBufPointers.push_back(pointer);
}

void ModelBuilder::registerBufferPointer(char *pointer) {
charBufPointers.push_back(pointer);
}

void ModelBuilder::clear() {
ANeuralNetworksCompilation_free(compilation);
ANeuralNetworksModel_free(model);
for (auto pointer : bufferPointers) {
for (auto pointer : charBufPointers) {
delete[] pointer;
}
for (auto pointer : floatBufPointers) {
delete[] pointer;
}
bufferPointers.clear();
floatBufPointers.clear();
}

ModelBuilder::ModelBuilder() {
Expand Down Expand Up @@ -899,7 +906,7 @@ uint32_t ModelBuilder::addFloat32NullOperandWithDims(std::vector<uint32_t> &dims

uint32_t ModelBuilder::addFloat32ZeroOperandWithDims(std::vector<uint32_t> &dims) {
float *zeros = new float[product(dims)];
bufferPointers.push_back(static_cast<void *>(zeros));
registerBufferPointer(zeros);
for (size_t i = 0; i < product(dims); i++) {
zeros[i] = 0;
}
Expand Down
6 changes: 4 additions & 2 deletions dnnlibrary/src/main/cpp/ModelBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
class ModelBuilder {
private:
ANeuralNetworksModel* model = nullptr;
std::vector<void*> bufferPointers;
std::vector<char *> charBufPointers;
std::vector<float *> floatBufPointers;
// NHWC
std::map<uint32_t, std::vector<uint32_t>> dimensMap;
std::vector<uint32_t> inputIndexVector;
Expand Down Expand Up @@ -149,7 +150,8 @@ class ModelBuilder {
std::vector<uint32_t> getOutputIndexes();
int setInputBuffer(const Model& model, int32_t index, void *buffer, size_t length);
int setOutputBuffer(const Model& model, int32_t index, void *buffer, size_t length);
void registerBufferPointer(void *pointer);
void registerBufferPointer(char *pointer);
void registerBufferPointer(float *pointer);
void clear();

ModelBuilder();
Expand Down

0 comments on commit b9ef5fb

Please sign in to comment.