Skip to content

Commit

Permalink
Fixed memory leak in server_async
Browse files Browse the repository at this point in the history
  • Loading branch information
dgquintas committed Jun 2, 2015
1 parent 206e6e8 commit c9516d4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
4 changes: 4 additions & 0 deletions include/grpc++/server_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ class ServerContext {
template <class R, class W>
friend class ::grpc::ServerReaderWriter;

// Prevent copying.
ServerContext(const ServerContext&);
ServerContext& operator=(const ServerContext&);

class CompletionOp;

void BeginCompletionOp(Call* call);
Expand Down
33 changes: 18 additions & 15 deletions test/cpp/qps/server_async.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include <forward_list>
#include <functional>
#include <memory>
#include <mutex>
#include <sys/time.h>
#include <sys/resource.h>
Expand Down Expand Up @@ -158,26 +159,27 @@ class AsyncQpsServerTest : public Server {
void *)> request_method,
std::function<grpc::Status(const RequestType *, ResponseType *)>
invoke_method)
: next_state_(&ServerRpcContextUnaryImpl::invoker),
: srv_ctx_(new ServerContext),
next_state_(&ServerRpcContextUnaryImpl::invoker),
request_method_(request_method),
invoke_method_(invoke_method),
response_writer_(&srv_ctx_) {
request_method_(&srv_ctx_, &req_, &response_writer_,
response_writer_(srv_ctx_.get()) {
request_method_(srv_ctx_.get(), &req_, &response_writer_,
AsyncQpsServerTest::tag(this));
}
~ServerRpcContextUnaryImpl() GRPC_OVERRIDE {}
bool RunNextState(bool ok) GRPC_OVERRIDE {
return (this->*next_state_)(ok);
}
void Reset() GRPC_OVERRIDE {
srv_ctx_ = ServerContext();
srv_ctx_.reset(new ServerContext);
req_ = RequestType();
response_writer_ =
grpc::ServerAsyncResponseWriter<ResponseType>(&srv_ctx_);
grpc::ServerAsyncResponseWriter<ResponseType>(srv_ctx_.get());

// Then request the method
next_state_ = &ServerRpcContextUnaryImpl::invoker;
request_method_(&srv_ctx_, &req_, &response_writer_,
request_method_(srv_ctx_.get(), &req_, &response_writer_,
AsyncQpsServerTest::tag(this));
}

Expand All @@ -198,7 +200,7 @@ class AsyncQpsServerTest : public Server {
response_writer_.Finish(response, status, AsyncQpsServerTest::tag(this));
return true;
}
ServerContext srv_ctx_;
std::unique_ptr<ServerContext> srv_ctx_;
RequestType req_;
bool (ServerRpcContextUnaryImpl::*next_state_)(bool);
std::function<void(ServerContext *, RequestType *,
Expand All @@ -218,25 +220,26 @@ class AsyncQpsServerTest : public Server {
void *)> request_method,
std::function<grpc::Status(const RequestType *, ResponseType *)>
invoke_method)
: next_state_(&ServerRpcContextStreamingImpl::request_done),
: srv_ctx_(new ServerContext),
next_state_(&ServerRpcContextStreamingImpl::request_done),
request_method_(request_method),
invoke_method_(invoke_method),
stream_(&srv_ctx_) {
request_method_(&srv_ctx_, &stream_, AsyncQpsServerTest::tag(this));
stream_(srv_ctx_.get()) {
request_method_(srv_ctx_.get(), &stream_, AsyncQpsServerTest::tag(this));
}
~ServerRpcContextStreamingImpl() GRPC_OVERRIDE {}
bool RunNextState(bool ok) GRPC_OVERRIDE {
return (this->*next_state_)(ok);
}
void Reset() GRPC_OVERRIDE {
srv_ctx_ = ServerContext();
srv_ctx_.reset(new ServerContext);
req_ = RequestType();
stream_ =
grpc::ServerAsyncReaderWriter<ResponseType, RequestType>(&srv_ctx_);
stream_ = grpc::ServerAsyncReaderWriter<ResponseType, RequestType>(
srv_ctx_.get());

// Then request the method
next_state_ = &ServerRpcContextStreamingImpl::request_done;
request_method_(&srv_ctx_, &stream_, AsyncQpsServerTest::tag(this));
request_method_(srv_ctx_.get(), &stream_, AsyncQpsServerTest::tag(this));
}

private:
Expand Down Expand Up @@ -278,7 +281,7 @@ class AsyncQpsServerTest : public Server {
}
bool finish_done(bool ok) { return false; /* reset the context */ }

ServerContext srv_ctx_;
std::unique_ptr<ServerContext> srv_ctx_;
RequestType req_;
bool (ServerRpcContextStreamingImpl::*next_state_)(bool);
std::function<void(
Expand Down

0 comments on commit c9516d4

Please sign in to comment.