From d7658a6e889460ff7963b25c3108dc38d8283a4b Mon Sep 17 00:00:00 2001 From: Liangliang He Date: Wed, 3 Apr 2019 11:45:12 +0800 Subject: [PATCH] Fix nullptr in mace_run when build with code mode --- docs/development/how_to_debug.rst | 2 +- mace/benchmark/benchmark_model.cc | 7 +++++-- mace/examples/cli/example.cc | 7 +++++-- mace/port/file_system.h | 17 +++++++++++++++-- mace/port/posix/file_system.cc | 4 ++-- mace/tools/validation/mace_run.cc | 7 +++++-- 6 files changed, 33 insertions(+), 11 deletions(-) diff --git a/docs/development/how_to_debug.rst b/docs/development/how_to_debug.rst index 7efdfcd2..05a44ead 100644 --- a/docs/development/how_to_debug.rst +++ b/docs/development/how_to_debug.rst @@ -90,7 +90,7 @@ For android, you can use `ndk-stack tools engine; MaceStatus create_engine_status; // Create Engine - std::unique_ptr model_graph_data; + std::unique_ptr model_graph_data = + make_unique(); if (FLAGS_model_file != "") { auto fs = GetFileSystem(); auto status = fs->NewReadOnlyMemoryRegionFromFile(FLAGS_model_file.c_str(), @@ -294,7 +296,8 @@ int Main(int argc, char **argv) { } } - std::unique_ptr model_weights_data; + std::unique_ptr model_weights_data = + make_unique(); if (FLAGS_model_data_file != "") { auto fs = GetFileSystem(); auto status = fs->NewReadOnlyMemoryRegionFromFile( diff --git a/mace/examples/cli/example.cc b/mace/examples/cli/example.cc index 26f615d1..89fa3e16 100644 --- a/mace/examples/cli/example.cc +++ b/mace/examples/cli/example.cc @@ -31,6 +31,7 @@ #include "mace/port/file_system.h" #include "mace/public/mace.h" #include "mace/utils/logging.h" +#include "mace/utils/memory.h" #include "mace/utils/string_util.h" // if convert model to code. #ifdef MODEL_GRAPH_FORMAT_CODE @@ -201,7 +202,8 @@ bool RunModel(const std::vector &input_names, std::shared_ptr engine; MaceStatus create_engine_status; - std::unique_ptr model_graph_data; + std::unique_ptr model_graph_data = + make_unique(); if (FLAGS_model_file != "") { auto fs = GetFileSystem(); auto status = fs->NewReadOnlyMemoryRegionFromFile(FLAGS_model_file.c_str(), @@ -211,7 +213,8 @@ bool RunModel(const std::vector &input_names, } } - std::unique_ptr model_weights_data; + std::unique_ptr model_weights_data = + make_unique(); if (FLAGS_model_data_file != "") { auto fs = GetFileSystem(); auto status = fs->NewReadOnlyMemoryRegionFromFile( diff --git a/mace/port/file_system.h b/mace/port/file_system.h index 91b6f458..8ef35de8 100644 --- a/mace/port/file_system.h +++ b/mace/port/file_system.h @@ -27,8 +27,21 @@ class ReadOnlyMemoryRegion { public: ReadOnlyMemoryRegion() = default; virtual ~ReadOnlyMemoryRegion() = default; - virtual const void *data() = 0; - virtual uint64_t length() = 0; + virtual const void *data() const = 0; + virtual uint64_t length() const = 0; +}; + +class ReadOnlyBufferMemoryRegion : public ReadOnlyMemoryRegion { + public: + ReadOnlyBufferMemoryRegion() : data_(nullptr), length_(0) {} + ReadOnlyBufferMemoryRegion(const void *data, uint64_t length) : + data_(data), length_(length) {} + const void *data() const override { return data_; } + uint64_t length() const override { return length_; } + + private: + const void *data_; + uint64_t length_; }; class FileSystem { diff --git a/mace/port/posix/file_system.cc b/mace/port/posix/file_system.cc index a7873b96..75055e45 100644 --- a/mace/port/posix/file_system.cc +++ b/mace/port/posix/file_system.cc @@ -38,8 +38,8 @@ class PosixReadOnlyMemoryRegion : public ReadOnlyMemoryRegion { munmap(const_cast(addr_), length_); } }; - const void *data() override { return addr_; }; - uint64_t length() override { return length_; }; + const void *data() const override { return addr_; }; + uint64_t length() const override { return length_; }; private: const void *addr_; diff --git a/mace/tools/validation/mace_run.cc b/mace/tools/validation/mace_run.cc index 0653304f..ed8bb0b9 100644 --- a/mace/tools/validation/mace_run.cc +++ b/mace/tools/validation/mace_run.cc @@ -37,6 +37,7 @@ #include "mace/port/env.h" #include "mace/port/file_system.h" #include "mace/utils/logging.h" +#include "mace/utils/memory.h" #include "mace/utils/string_util.h" #ifdef MODEL_GRAPH_FORMAT_CODE @@ -244,7 +245,8 @@ bool RunModel(const std::string &model_name, } #endif // MACE_ENABLE_OPENCL - std::unique_ptr model_graph_data; + std::unique_ptr model_graph_data = + make_unique(); if (FLAGS_model_file != "") { auto fs = GetFileSystem(); status = fs->NewReadOnlyMemoryRegionFromFile(FLAGS_model_file.c_str(), @@ -254,7 +256,8 @@ bool RunModel(const std::string &model_name, } } - std::unique_ptr model_weights_data; + std::unique_ptr model_weights_data = + make_unique(); if (FLAGS_model_data_file != "") { auto fs = GetFileSystem(); status = fs->NewReadOnlyMemoryRegionFromFile(FLAGS_model_data_file.c_str(), -- GitLab