From 6a691f6beb7a6c1b28f0df0597328ad80214e729 Mon Sep 17 00:00:00 2001 From: Buqian Zheng Date: Wed, 25 Dec 2024 19:24:42 +0800 Subject: [PATCH] make FileReader to close fd using RAII (#1005) Signed-off-by: Buqian Zheng --- src/io/file_io.h | 14 ++++++++------ thirdparty/hnswlib/hnswlib/hnswalg.h | 2 -- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/io/file_io.h b/src/io/file_io.h index 6e27e716e..ea797b801 100644 --- a/src/io/file_io.h +++ b/src/io/file_io.h @@ -32,6 +32,13 @@ struct FileReader { } } + ~FileReader() { + if (fd_ != -1) { + close(fd_); + fd_ = -1; + } + } + int descriptor() const { return fd_; @@ -62,13 +69,8 @@ struct FileReader { return lseek(fd_, 0, SEEK_CUR); } - int - close() { - return ::close(fd_); - } - private: - int fd_; + int fd_ = -1; size_t size_; }; } // namespace knowhere diff --git a/thirdparty/hnswlib/hnswlib/hnswalg.h b/thirdparty/hnswlib/hnswlib/hnswalg.h index ac9e03de3..f1b0aafa9 100644 --- a/thirdparty/hnswlib/hnswlib/hnswalg.h +++ b/thirdparty/hnswlib/hnswlib/hnswalg.h @@ -892,8 +892,6 @@ class HierarchicalNSW : public AlgorithmInterface { input.read(linkLists_[i], linkListSize); } } - - input.close(); } void