Skip to content
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

Support custom deleter for SrsUniquePtr. #4309

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

Conversation

winlinvip
Copy link
Member

@winlinvip winlinvip commented Mar 21, 2025

SrsUniquePtr does not support array or object created by malloc, because we only use delete to dispose the resource. You can use a custom function to free the memory allocated by malloc or other allocators.

     char* p = (char*)malloc(1024);
      SrsUniquePtr<char> ptr(p, your_free_chars);

This is used to replace the SrsAutoFreeH. For example:

      addrinfo* r = NULL;
      SrsAutoFreeH(addrinfo, r, freeaddrinfo);
      getaddrinfo("127.0.0.1", NULL, &hints, &r);

Now, this can be replaced by:

      addrinfo* r = NULL;
      getaddrinfo("127.0.0.1", NULL, &hints, &r);
      SrsUniquePtr<addrinfo> r2(r, freeaddrinfo);

Please aware that there is a slight difference between SrsAutoFreeH and SrsUniquePtr. SrsAutoFreeH will track the address of pointer, while SrsUniquePtr will not.

      addrinfo* r = NULL;
      SrsAutoFreeH(addrinfo, r, freeaddrinfo); // r will be freed even r is changed later.
      SrsUniquePtr<addrinfo> ptr(r, freeaddrinfo); // crash because r is an invalid pointer.

@winlinvip winlinvip added the EnglishNative This issue is conveyed exclusively in English. label Mar 21, 2025
@duiniuluantanqin duiniuluantanqin self-requested a review April 1, 2025 00:58
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.

5 participants