Skip to content

[Bug] Memory leak about ServerOptions  #2871

Description

@w-gc

Describe the bug (描述bug)
The default ServerOptions _options of Server may leak since it will be covered by user's ServerOptions.

To Reproduce (复现方法)

ServerOptions::ServerOptions()
    : ....
    , rpc_pb_message_factory(new DefaultRpcPBMessageFactory()) // BAD: New here, but delete elsewhere
    , ... {
    // ...
}

class Server {
public:
    // ... 
    ServerOptions _options;
    // ... 
  ~Server() {
    //...
        delete _options.rpc_pb_message_factory; // <--- BAD: only the last '_options' will free rpc_pb_message_factory
        _options.rpc_pb_message_factory = NULL;
    //...
}

};

int Server::Start(const butil::EndPoint& endpoint, const ServerOptions* opt) {
    return StartInternal(
        endpoint, PortRange(endpoint.port, endpoint.port), opt);
}

int Server::StartInternal(const butil::EndPoint& endpoint,
                          const PortRange& port_range,
                          const ServerOptions *opt) {
    // ...
    if (opt) {
        _options = *opt; // <----- LEAK: _options.rpc_pb_message_factory miss free
    } else {
        // Always reset to default options explicitly since `_options'
        // may be the options for the last run or even bad options
        _options = ServerOptions(); // <----- LEAK: _options.rpc_pb_message_factory miss free
    }
    // ...
}

Expected behavior (期望行为)
No memory leak, pls.

Versions (各种版本)
OS:
Compiler:
brpc:
protobuf:

Additional context/screenshots (更多上下文/截图)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions