-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Fix memory leaks from errors skipping resource release #4308
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
base: develop
Are you sure you want to change the base?
Fix memory leaks from errors skipping resource release #4308
Conversation
// free pkts when exit | ||
vector<SrsRtpPacket*>* pkts_ptr = &pkts; | ||
SrsAutoFreeH(vector<SrsRtpPacket*>, pkts_ptr, free_packets); | ||
|
||
if (merge_nalus && nn_samples > 1) { | ||
if ((err = package_nalus(msg, samples, pkts)) != srs_success) { | ||
return srs_error_wrap(err, "package nalus as one"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an error occurs, the SrsRtpPacket
within the vector is not released, which could potentially lead to a memory leak.
TRANS_BY_GPT4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use vector<SrsSharedPtr<SrsRtpPacket>>
to replace SrsAutoFreeH
.
|
||
// free pkts when exit | ||
vector<SrsRtpPacket*>* pkts_ptr = &pkts; | ||
SrsAutoFreeH(vector<SrsRtpPacket*>, pkts_ptr, free_packets); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is a memory leak.
But, SrsAutoFreeH
is already deprecated, the recommended smart pointer is SrsSharedPtr
and SrsUniquePtr
. And in this case, vector<SrsSharedPtr<SrsRtpPacket>>
seems can replace SrsAutoFreeH
here, the package_nalus
, package_single_nalue
, package_fu_a
& consume_packets
need a little patch then.
Oh, I do think vector<SrsSharedPtr<SrsRtpPacket>>
is better here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Converting to smart pointers is feasible, but it would affect several functions. The functions consume_packets
, package_fu_a
, package_single_nalu
, and package_nalus
would all require modifications.
TRANS_BY_GPT4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, refine the SrsSharedPtr
to support a custom deleter.
TRANS_BY_GPT4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No custom deleter here, I do think only tiny effort is needed to do the replacement.
e.g.
srs_error_t SrsRtcRtpBuilder::package_nalus(SrsSharedPtrMessage* msg, const vector<SrsSample*>& samples, vector<SrsSharedPtr<SrsRtpPacket>& pkts)
{
...
if (nn_bytes < kRtpMaxPayloadSize) {
SrsSharedPtr<SrsRtpPacket> pkt(new SrsRtpPacket());
pkts.push_back(pkt);
...
}
...
}
srs_error_t SrsRtcRtpBuilder::consume_packets(vector<SrsSharedPtr<SrsRtpPacket>>& pkts)
{
srs_error_t err = srs_success;
// TODO: FIXME: Consume a range of packets.
for (int i = 0; i < (int)pkts.size(); i++) {
SrsSharedPtr<SrsRtpPacket> pkt = pkts[i];
if ((err = bridge_->on_rtp(pkt.get())) != srs_success) {
err = srs_error_wrap(err, "consume sps/pps");
break;
}
}
return err;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SrsAutoFreeH is deprecated, but it can still be used, especially no alternative there. A better solution is to support a custom free handler in SrsUniquePtr to replace SrsAutoFreeH. SrsSharedPtr is not ideal here because it's a hot path and may cause a performance decline. See #4309
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SrsAutoFreeH
is faster than SrsSharedPtr
. OK, I accept this view.
|
||
// free pkts when exit | ||
vector<SrsRtpPacket*>* pkts_ptr = &pkts; | ||
SrsAutoFreeH(vector<SrsRtpPacket*>, pkts_ptr, free_packets); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SrsAutoFreeH
is faster than SrsSharedPtr
. OK, I accept this view.
No description provided.