ossrs / srs

SRS is a simple, high-efficiency, real-time video server supporting RTMP, WebRTC, HLS, HTTP-FLV, SRT, MPEG-DASH, and GB28181.
https://ossrs.io
MIT License
24.7k stars 5.28k forks source link

SmartPtr: Support detect memory leak by valgrind. #4102

Open winlinvip opened 1 week ago

winlinvip commented 1 week ago
  1. Support detect memory leak by valgrind.
  2. Free the http handler entry.
  3. Free the stack of ST.
suzp1984 commented 1 day ago

Here is my env to testing & verify valgrind memory leak, there are a lot of outputs need to analysis, so I align my test env here.

Because I didn't find an image with valgrind in https://hub.docker.com/r/ossrs/srs/tags, I make my own one based on ossrs/srs:ubuntu20: start this image, running apt update -y && apt install -y valgrind, tag this container to an image, then I have the docker image to do the testing.

I would suggest tag an image with all the testing tools to the srs public docker hub.

Then build srs: (ossrs/srs:valgrind is the docker image in my local env)

docker run -it --rm -v pwd:/srs -w /srs ossrs/srs:valgrind bash -c "make clean_st" docker run -it --rm -v pwd:/srs -w /srs ossrs/srs:valgrind bash -c "./configure --valgrind=on && make"

and run the srs with valgrind:

docker run -p 1935:1935 -p 1985:1985 -p 8080:8080 -p 8085:8085 --env CANDIDATE=$(ifconfig en0 inet| grep 'inet '|awk '{print $2}') -p 8000:8000/udp -it --rm -v pwd:/srs -w /srs ossrs/srs:valgrind valgrind --leak-check=full ./objs/srs -c conf/rtmp2rtc.conf

do live streams publish and play, close the srs by ctrl + \ or ctrl + c, check the output.

suzp1984 commented 1 day ago

Here is my env to testing & verify valgrind memory leak, there are a lot of outputs need to analysis, so I align my test env here.

Because I didn't find an image with valgrind in https://hub.docker.com/r/ossrs/srs/tags, I make my own one based on ossrs/srs:ubuntu20: start this image, running apt update -y && apt install -y valgrind, tag this container to an image, then I have the docker image to do the testing.

I would suggest tag an image with all the testing tools to the srs public docker hub.

Then build srs: (ossrs/srs:valgrind is the docker image in my local env)

docker run -it --rm -v pwd:/srs -w /srs ossrs/srs:valgrind bash -c "make clean_st" docker run -it --rm -v pwd:/srs -w /srs ossrs/srs:valgrind bash -c "./configure --valgrind=on && make"

and run the srs with valgrind:

docker run -p 1935:1935 -p 1985:1985 -p 8080:8080 -p 8085:8085 --env CANDIDATE=$(ifconfig en0 inet| grep 'inet '|awk '{print $2}') -p 8000:8000/udp -it --rm -v pwd:/srs -w /srs ossrs/srs:valgrind valgrind --leak-check=full ./objs/srs -c conf/rtmp2rtc.conf

do live streams publish and play, close the srs by ctrl + \ or ctrl + c, check the output.

By this way, I found another definitely memory leak:

==1== 48 bytes in 1 blocks are definitely lost in loss record 48 of 145
==1==    at 0x484A3C4: operator new(unsigned long) (in /usr/lib/aarch64-linux-gnu/valgrind/vgpreload_memcheck-arm64-linux.so)
==1==    by 0x5BB90B: SrsPithyPrint::create_ingester() (srs_app_pithy_print.cpp:261)
==1==    by 0x5D842B: SrsIngester::SrsIngester() (srs_app_ingest.cpp:97)
==1==    by 0x517BE3: SrsServer::SrsServer() (srs_app_server.cpp:361)
==1==    by 0x51E4DB: SrsServerAdapter::SrsServerAdapter() (srs_app_server.cpp:1343)
==1==    by 0x6B842F: run_hybrid_server(void*) (srs_main_server.cpp:494)
==1==    by 0x6333F7: SrsThreadPool::start(void*) (srs_app_threads.cpp:939)
==1==    by 0x4881623: start_thread (pthread_create.c:477)
==1==    by 0x4C3049B: thread_start (clone.S:78)

https://github.com/ossrs/srs/blob/ea7e2c28491ae61cc1b7aaa207ce38a74fa36487/trunk/src/app/srs_app_ingest.cpp#L89-L106

srs_freep(pprint), maybe rename pprint to pprint_, if there is a private member naming convention in this way.