Tuvie commented on code in PR #2252:
URL: https://github.com/apache/brpc/pull/2252#discussion_r1196652483


##########
src/brpc/rdma/rdma_helper.cpp:
##########
@@ -399,36 +399,39 @@ static void GlobalRdmaInitializeOrDieImpl() {
     // Find the first active port
     int available_devices = 0;
     g_port_num = FLAGS_rdma_port;
+    auto context_deleter = [](ibv_context* p) { IbvCloseDevice(p); };
     for (int i = 0; i < num; ++i) {
-        ibv_context* context = IbvOpenDevice(g_devices[i]);
+        std::unique_ptr<ibv_context, decltype(context_deleter)> context(
+            IbvOpenDevice(g_devices[i]), context_deleter);
         if (!context) {
             PLOG(ERROR) << "Fail to open rdma device " << 
IbvGetDeviceName(g_devices[i]);
             ExitWithError();
         }
         ibv_port_attr attr;
-        if (IbvQueryPort(context, g_port_num, &attr) < 0) {
+        if (IbvQueryPort(context.get(), g_port_num, &attr) < 0) {
             PLOG(WARNING) << "Fail to query port " << g_port_num 
                           << " on " << IbvGetDeviceName(g_devices[i]);
             if (FLAGS_rdma_device.size() > 0) {
                 ExitWithError();
             }
-            IbvCloseDevice(context);
             continue;
         }
         if (attr.state != IBV_PORT_ACTIVE) {
-            IbvCloseDevice(context);
             continue;
         }
         if (FLAGS_rdma_device.size() > 0) {
             if (strcmp(context->device->name, FLAGS_rdma_device.c_str()) == 0) 
{
                 ++available_devices;
-                g_context = context;
+                g_context = context.release();
                 g_gid_tbl_len = attr.gid_tbl_len;
                 g_lid = attr.lid;
                 break;
             }
         } else {
-            g_context = context;
+            if (g_context) {

Review Comment:
   I think this fix to memory leak is ok. But maybe we do not need to use the 
last available device. Use the first one is more reasonable. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to