【发布时间】:2022-01-11 08:20:44
【问题描述】:
由 grpc (grpc.pb.cc) 生成的文件中的以下声明会导致内存泄漏。 google::protobuf::ShutdownProtobufLibrary() 似乎没有释放此声明分配的内存。 你能告诉我如何释放它吗?
PROTOBUF_ATTRIBUTE_INIT_PRIORITY static ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptorsRunner dynamic_init_dummy_DxpGrpc_2eproto(&descriptor_table_DxpGrpc_2eproto);
Windows、C++、gRPC-1.40.0
在Windows c++环境中创建一个控制台应用程序并执行以下代码。
#include <crtdbg.h>.
#include "google/protobuf/service.h";
int main(int argc, char** argv) {
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
以下泄漏将被淘汰。
Detected memory leaks!
Dumping objects -> {159}
{Normal block at 0x00E49B18, 8 bytes long.
Data: < k > 10 6B C9 00 00 00 00 00
Object dump complete.
在grpc.pb.cc中声明AddDescriptorsRunner时,会调用下面类的DefaultConstruct()。
(文件:third_party\protobuf\src\google\protobuf\message_lite.h)
template <typename T>.
class ExplicitlyConstructed {
public:
void DefaultConstruct() { new (&union_) T(); }
template <typename... Args>
void Construct(Args&&... args) {
new (&union_) T(std::forward<Args>(args)...) ;
}
void Destruct() { get_mutable()->~T(); }
constexpr const T& get() const { return reinterpret_cast<const T&>(union_); }
T* get_mutable() { return reinterpret_cast<T*>(&union_); }
private:
// Prefer c++14 aligned_storage, but for compatibility this will do.
union AlignedUnion {
alignas(T) char space[sizeof(T)];
int64 align_to_int64;
void* align_to_ptr;
} union_;
};
【问题讨论】:
标签: c++ visual-studio protocol-buffers grpc grpc-c++