sinomiko opened a new issue, #2466:
URL: https://github.com/apache/brpc/issues/2466

   **Is your feature request related to a problem? (你需要的功能是否与某个问题有关?)**
   先看看当前`ParallelChannel::AddChannel`
   
   
[ParallelChannel::AddChannel](https://github.com/apache/brpc/blob/dc9e786fbb5fd79f08b49fa8d55ec4e1a35f2125/src/brpc/parallel_channel.cpp#L460)
   ```cpp
   int ParallelChannel::AddChannel(ChannelBase* sub_channel,
                                   ChannelOwnership ownership,
                                   CallMapper* call_mapper,
                                   ResponseMerger* merger) {
       if (NULL == sub_channel) {
           LOG(ERROR) << "Param[sub_channel] is NULL";
           return -1;
       }
       if (_chans.capacity() == 0) {
           _chans.reserve(32);
       }
       SubChan sc;
       sc.chan = sub_channel;
       sc.ownership = ownership;
       sc.call_mapper = call_mapper;
       sc.merger = merger;
       _chans.push_back(sc);
       return 0;
   }
   ```
   
   
   当前设计使用很不方便,有潜在内存泄漏问题,如果return -1,调用处需要手动将`CallMapper* call_mapper`, 
`CallMapper* call_mapper` 内存传递给`butil::intrusive_ptr`,如果不处理就会泄漏内存。
   
   当前
   ```
              if (channel.AddChannel(sub_channel.get(), 
brpc::DOESNT_OWN_CHANNEL, call_mapper,
                                      response_merger) != 0) {
                   butil::intrusive_ptr<brpc::ResponseMerger> 
rm_ptr(response_merger);;
                   butil::intrusive_ptr<XXXCallMapper<Request>> 
cm_ptr(call_mapper);
                   LOG(ERROR) << "XXX“;
                   return -1;
               }
   ```
   很明显`butil::intrusive_ptr`显得很突然
   ```
                   butil::intrusive_ptr<brpc::ResponseMerger> 
rm_ptr(response_merger);;
                   butil::intrusive_ptr<XXXCallMapper<Request>> 
cm_ptr(call_mapper);
   ```
   
   期望直接将`intrusive_ptr` 作为参数传递进去。
   
   使用时不会导致`call_mapper`, `response_merger`潜在内存泄漏
   ```
              if (channel.AddChannel(sub_channel.get(), 
brpc::DOESNT_OWN_CHANNEL, call_mapper,
                                      response_merger) != 0) {
                   LOG(ERROR) << "XXX“;
                   return -1;
               }
   ```
   
   **Describe the solution you'd like (描述你期望的解决方法)**
   ```cpp
   int ParallelChannel::AddChannel(ChannelBase* sub_channel,
                                   ChannelOwnership ownership,
                                   const butil::intrusive_ptr<CallMapper>& 
all_mapper,
                                   const butil::intrusive_ptr<ResponseMerger>& 
merger)
   ```
   外部构造时,直接产生butil::intrusive_ptr托管内存,直接传递给`ParallelChannel::AddChannel`, 
无论出错还是成功,都不用再次去解决`CallMapper* call_mapper`, `ResponseMerger* merger` 没有释放问题。
   
   **Describe alternatives you've considered (描述你想到的折衷方案)**
   
   
   **Additional context/screenshots (更多上下文/截图)**
   
   


-- 
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