From 928ec8ee51faf0e57fbc084d6da04d1f85704e07 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Fri, 5 Jun 2015 08:45:45 -0700 Subject: [PATCH 1/2] Fix memory leak --- include/grpc++/impl/proto_utils.h | 5 ++++- src/cpp/server/server.cc | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/include/grpc++/impl/proto_utils.h b/include/grpc++/impl/proto_utils.h index bf82f2b23d3..9c5f60980aa 100644 --- a/include/grpc++/impl/proto_utils.h +++ b/include/grpc++/impl/proto_utils.h @@ -36,6 +36,7 @@ #include +#include #include #include #include @@ -66,7 +67,9 @@ class SerializationTraitshandler()->RunHandler(MethodHandler::HandlerParameter( &call_, &ctx_, request_payload_, call_.max_message_size())); + request_payload_ = nullptr; void* ignored_tag; bool ignored_ok; cq_.Shutdown(); From 2f4a49c8d37be8468a9e2a37a33f187dc952e954 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Fri, 5 Jun 2015 09:36:52 -0700 Subject: [PATCH 2/2] Fix memory leak --- include/grpc++/impl/call.h | 17 +++++++++++++---- include/grpc++/server.h | 2 -- src/cpp/server/server.cc | 1 + 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/include/grpc++/impl/call.h b/include/grpc++/impl/call.h index 94b0f915c6f..9b07e2ac5e9 100644 --- a/include/grpc++/impl/call.h +++ b/include/grpc++/impl/call.h @@ -34,7 +34,7 @@ #ifndef GRPCXX_IMPL_CALL_H #define GRPCXX_IMPL_CALL_H -#include +#include #include #include #include @@ -85,6 +85,8 @@ class CallOpSendInitialMetadata { op->data.send_initial_metadata.metadata = initial_metadata_; } void FinishOp(bool* status, int max_message_size) { + if (!send_) return; + gpr_free(initial_metadata_); send_ = false; } @@ -244,6 +246,7 @@ class CallOpServerSendStatus { protected: void AddOp(grpc_op* ops, size_t* nops) { + if (!send_status_available_) return; grpc_op* op = &ops[(*nops)++]; op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; op->data.send_status_from_server.trailing_metadata_count = @@ -255,7 +258,9 @@ class CallOpServerSendStatus { } void FinishOp(bool* status, int max_message_size) { - send_status_details_ = false; + if (!send_status_available_) return; + gpr_free(trailing_metadata_); + send_status_available_ = false; } private: @@ -269,7 +274,6 @@ class CallOpServerSendStatus { class CallOpRecvInitialMetadata { public: CallOpRecvInitialMetadata() : recv_initial_metadata_(nullptr) { - memset(&recv_initial_metadata_arr_, 0, sizeof(recv_initial_metadata_arr_)); } void RecvInitialMetadata(ClientContext* context) { @@ -280,6 +284,7 @@ class CallOpRecvInitialMetadata { protected: void AddOp(grpc_op* ops, size_t* nops) { if (!recv_initial_metadata_) return; + memset(&recv_initial_metadata_arr_, 0, sizeof(recv_initial_metadata_arr_)); grpc_op* op = &ops[(*nops)++]; op->op = GRPC_OP_RECV_INITIAL_METADATA; op->data.recv_initial_metadata = &recv_initial_metadata_arr_; @@ -297,7 +302,7 @@ class CallOpRecvInitialMetadata { class CallOpClientRecvStatus { public: - CallOpClientRecvStatus() { memset(this, 0, sizeof(*this)); } + CallOpClientRecvStatus() : recv_status_(nullptr) {} void ClientRecvStatus(ClientContext* context, Status* status) { recv_trailing_metadata_ = &context->trailing_metadata_; @@ -307,6 +312,9 @@ class CallOpClientRecvStatus { protected: void AddOp(grpc_op* ops, size_t* nops) { if (recv_status_ == nullptr) return; + memset(&recv_trailing_metadata_arr_, 0, sizeof(recv_trailing_metadata_arr_)); + status_details_ = nullptr; + status_details_capacity_ = 0; grpc_op* op = &ops[(*nops)++]; op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; op->data.recv_status_on_client.trailing_metadata = @@ -323,6 +331,7 @@ class CallOpClientRecvStatus { *recv_status_ = Status( static_cast(status_code_), status_details_ ? grpc::string(status_details_) : grpc::string()); + gpr_free(status_details_); recv_status_ = nullptr; } diff --git a/include/grpc++/server.h b/include/grpc++/server.h index cdfdc865877..834d19c882b 100644 --- a/include/grpc++/server.h +++ b/include/grpc++/server.h @@ -108,8 +108,6 @@ class Server GRPC_FINAL : public GrpcLibrary, private CallHook { bool FinalizeResult(void** tag, bool* status) GRPC_OVERRIDE; protected: - void FinalizeMetadata(ServerContext* context); - Server* const server_; ServerContext* const context_; ServerAsyncStreamingInterface* const stream_; diff --git a/src/cpp/server/server.cc b/src/cpp/server/server.cc index 680f96061c6..ed1bb2a362a 100644 --- a/src/cpp/server/server.cc +++ b/src/cpp/server/server.cc @@ -317,6 +317,7 @@ bool Server::BaseAsyncRequest::FinalizeResult(void** tag, bool* status) { initial_metadata_array_.metadata[i].value_length))); } } + grpc_metadata_array_destroy(&initial_metadata_array_); context_->call_ = call_; context_->cq_ = call_cq_; Call call(call_, server_, call_cq_, server_->max_message_size_);