Skip to content

SmartPtr: Use shared ptr to manage GB objects. v6.0.126#4080

Merged
winlinvip merged 16 commits intoossrs:developfrom
winlinvip:feature/smart-ptr
Jun 12, 2024
Merged

SmartPtr: Use shared ptr to manage GB objects. v6.0.126#4080
winlinvip merged 16 commits intoossrs:developfrom
winlinvip:feature/smart-ptr

Conversation

@winlinvip
Copy link
Copy Markdown
Member

The object relations:

gb

Session manages SIP and Media object using shared resource or shared ptr. Note that I actually use SrsExecutorCoroutine to delete the object when each coroutine is done, because there is always a dedicate coroutine for each object.

For SIP and Media object, they directly use the session by raw pointer, it's safe because session always live longer than session and media object.

@winlinvip winlinvip added the EnglishNative This issue is conveyed exclusively in English. label Jun 7, 2024
// SrsSharedPtr<MyClass> cp = ptr;
// cp->do_something();
template<class T>
class SrsSharedPtr
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems SrsSharedPtr shared similar api with std::shared_ptr, that's good, so why not add a compiling option to control whether use a template alias from std::shared_ptr or self defined shared ptr.

Suggested change
class SrsSharedPtr
#ifdef _USE_STD_SMART_PTR
template <class T>
using SrsSharedPtr = std::shared_ptr<T>;
#else
template <class T>
class SrsSharedPtr {
// current definition
};
#endif

The benefit is to quickly workaround the shared ptr bugs in the future, e.g. the ref_count_ is not atomic, it's ok for current single-thread coroutine solution, but what if multi-thread coroutine are implemented in the future, then it's disaster. For the basic level infrastructure codes, it's a smart idea to always has a replacement solution.

Copy link
Copy Markdown
Member Author

@winlinvip winlinvip Jun 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't fix. Using std::shared_ptr does not provide any benefits because if SrsSharedPtr has a bug, we would need to fix it.

TRANS_BY_GPT4

}
};

// Shared ptr smart pointer, see https://github.com/ossrs/srs/discussions/3667#discussioncomment-8969107
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a problem of this header file srs_core_autofree.hpp, but the empty srs_core_autofree.cpp, there is no reason to keep a empty cpp file except to waste compiler power.

Copy link
Copy Markdown
Member Author

@winlinvip winlinvip Jun 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To save on compilation time, please provide the specific amount of time saved by reducing one cpp file.
This is unrelated to the current PR; please submit a separate PR for this matter.

TRANS_BY_GPT4

return err;
}
// Wait for the SIP connection to be terminated.
while (true) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This while loop never break except return, is it a problem?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems no problem.


if ((err = resource->start()) != srs_success) {
srs_freep(conn);
SrsExecutorCoroutine* executor = new SrsExecutorCoroutine(_srs_gb_manager, conn, raw_conn, raw_conn);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think executor instance here is a memory leak. Here is the reason:

SrsExecutorCorountine is an ISrsResource which means it can use an ISrsResourceManager to manager its lifecycle, which is _srs_gb_manager in this case, check codes in SrsExecutorCoroutine::cycle():

srs_error_t SrsExecutorCoroutine::cycle()
{
    srs_error_t err = handler_->cycle();
    if (callback_) callback_->on_executor_done(this);
    manager_->remove(this);
    return err;
}

We have manager_->remove(this);, but where is the _srs_gb_manager->add(executor);?

And Check anther two SrsExecutorCoroutine instances, for SrsLazyGbMediaTcpConn and SrsLazyGbSession, they have same problem.

I didn't running any memory leak detector to confirm yet, but I think valgrind can detect it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems no problem. Won't fix.

@winlinvip winlinvip force-pushed the feature/smart-ptr branch from 5c7303e to 3088ff6 Compare June 12, 2024 13:42
@winlinvip winlinvip changed the title SmartPtr: Use shared ptr to manage GB objects. SmartPtr: Use shared ptr to manage GB objects. v5.0.214 v6.0.126 Jun 12, 2024
@winlinvip winlinvip added the RefinedByAI Refined by AI/GPT. label Jun 12, 2024
@winlinvip winlinvip changed the title SmartPtr: Use shared ptr to manage GB objects. v5.0.214 v6.0.126 SmartPtr: Use shared ptr to manage GB objects. v6.0.126 Jun 12, 2024
@winlinvip winlinvip merged commit 6834ec2 into ossrs:develop Jun 12, 2024
winlinvip added a commit to winlinvip/srs that referenced this pull request Jun 13, 2024
@winlinvip
Copy link
Copy Markdown
Member Author

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. RefinedByAI Refined by AI/GPT.

Development

Successfully merging this pull request may close these issues.

2 participants