Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Memory leak about ServerOptions #2871

Open
w-gc opened this issue Jan 13, 2025 · 1 comment · May be fixed by #2872
Open

[Bug] Memory leak about ServerOptions #2871

w-gc opened this issue Jan 13, 2025 · 1 comment · May be fixed by #2872

Comments

@w-gc
Copy link

w-gc commented Jan 13, 2025

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 (更多上下文/截图)

@yanglimingcn
Copy link
Contributor

@chenBright

@w-gc w-gc linked a pull request Jan 13, 2025 that will close this issue
@chenBright chenBright linked a pull request Jan 13, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants