Skip to content

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

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

duiniuluantanqin
Copy link
Member

No description provided.

@winlinvip winlinvip added the EnglishNative This issue is conveyed exclusively in English. label Mar 20, 2025
// 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");
Copy link
Member Author

@duiniuluantanqin duiniuluantanqin Mar 20, 2025

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

Copy link
Contributor

@suzp1984 suzp1984 left a 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);
Copy link
Contributor

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.

Copy link
Member Author

@duiniuluantanqin duiniuluantanqin Mar 21, 2025

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

Copy link
Member Author

@duiniuluantanqin duiniuluantanqin Mar 21, 2025

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

Copy link
Contributor

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

Copy link
Member

@winlinvip winlinvip Mar 21, 2025

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

Copy link
Contributor

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);
Copy link
Contributor

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.

@winlinvip winlinvip changed the title memory leak Fix memory leak when failed to free packets Mar 25, 2025
@winlinvip winlinvip changed the title Fix memory leak when failed to free packets Fix Memory Leaks Caused by Error Handling Skipping Resource Release Mar 25, 2025
@winlinvip winlinvip changed the title Fix Memory Leaks Caused by Error Handling Skipping Resource Release Fix memory leaks from errors skipping resource release Mar 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
EnglishNative This issue is conveyed exclusively in English.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants