SrsAutoFree模式,避免内存泄漏和错误

来源:互联网 发布:sql注入用户名密码 编辑:程序博客网 时间:2024/05/20 05:27

C/C++中内存是一个很难处理的事情,正如强项就是弱项,强大的地方也是致命的地方。

内存不释放就会泄漏,多次释放就会段错误,越界更恐怖。

不释放和多次释放都可以用SrsAutoFree规避,越界就需要工具和经验的问题。

释放和多次释放,原因是内存或者对象的生命周期过程,譬如在一个while循环中,有些时候要释放,有些时候continue就好,就容易出问题。

真的需要活N久的对象吗?很少。大部分的作用域在当前函数和子函数,局部变量就可以搞定,是的,有些时候就是没有办法用局部变量,譬如由一个函数收取的包,在当前函数就需要释放。

考虑下面的逻辑:

[cpp] view plain copy
  1. int SrsClient::publish(SrsSource* source, bool is_fmle)  
  2. {  
  3.     int ret = ERROR_SUCCESS;  
  4.       
  5.     SrsPithyPrint pithy_print(SRS_STAGE_PUBLISH_USER);  
  6.       
  7.     while (true) {  
  8.         // switch to other st-threads.  
  9.         st_usleep(0);  
  10.           
  11.         SrsCommonMessage* msg = NULL;  
  12.         if ((ret = rtmp->recv_message(&msg)) != ERROR_SUCCESS) {  
  13.             srs_error("recv identify client message failed. ret=%d", ret);  
  14.             return ret;  
  15.         }  
  16.   
  17.         SrsAutoFree(SrsCommonMessage, msg, false);  
  18.           
  19.         pithy_print.set_age(msg->header.timestamp);  
  20.   
  21.         // reportable  
  22.         if (pithy_print.can_print()) {  
  23.             srs_trace("<- clock=%u, time=%"PRId64", obytes=%"PRId64", ibytes=%"PRId64", okbps=%d, ikbps=%d",   
  24.                 (int)srs_get_system_time_ms(), pithy_print.get_age(), rtmp->get_send_bytes(), rtmp->get_recv_bytes(), rtmp->get_send_kbps(), rtmp->get_recv_kbps());  
  25.         }  
  26.           
  27.         // process audio packet  
  28.         if (msg->header.is_audio() && ((ret = source->on_audio(msg)) != ERROR_SUCCESS)) {  
  29.             srs_error("process audio message failed. ret=%d", ret);  
  30.             return ret;  
  31.         }  
  32.         // process video packet  
  33.         if (msg->header.is_video() && ((ret = source->on_video(msg)) != ERROR_SUCCESS)) {  
  34.             srs_error("process video message failed. ret=%d", ret);  
  35.             return ret;  
  36.         }  
  37.           
  38.         // process onMetaData  
  39.         if (msg->header.is_amf0_data() || msg->header.is_amf3_data()) {  
  40.             if ((ret = msg->decode_packet()) != ERROR_SUCCESS) {  
  41.                 srs_error("decode onMetaData message failed. ret=%d", ret);  
  42.                 return ret;  
  43.             }  
  44.           
  45.             SrsPacket* pkt = msg->get_packet();  
  46.             if (dynamic_cast<SrsOnMetaDataPacket*>(pkt)) {  
  47.                 SrsOnMetaDataPacket* metadata = dynamic_cast<SrsOnMetaDataPacket*>(pkt);  
  48.                 if ((ret = source->on_meta_data(msg, metadata)) != ERROR_SUCCESS) {  
  49.                     srs_error("process onMetaData message failed. ret=%d", ret);  
  50.                     return ret;  
  51.                 }  
  52.                 srs_trace("process onMetaData message success.");  
  53.                 continue;  
  54.             }  
  55.               
  56.             srs_trace("ignore AMF0/AMF3 data message.");  
  57.             continue;  
  58.         }  
  59.           
  60.         // process UnPublish event.  
  61.         if (msg->header.is_amf0_command() || msg->header.is_amf3_command()) {  
  62.             if ((ret = msg->decode_packet()) != ERROR_SUCCESS) {  
  63.                 srs_error("decode unpublish message failed. ret=%d", ret);  
  64.                 return ret;  
  65.             }  
  66.               
  67.             // flash unpublish.  
  68.             if (!is_fmle) {  
  69.                 srs_trace("flash publish finished.");  
  70.                 return ret;  
  71.             }  
  72.           
  73.             SrsPacket* pkt = msg->get_packet();  
  74.             if (dynamic_cast<SrsFMLEStartPacket*>(pkt)) {  
  75.                 SrsFMLEStartPacket* unpublish = dynamic_cast<SrsFMLEStartPacket*>(pkt);  
  76.                 return rtmp->fmle_unpublish(res->stream_id, unpublish->transaction_id);  
  77.             }  
  78.               
  79.             srs_trace("ignore AMF0/AMF3 command message.");  
  80.             continue;  
  81.         }  
  82.     }  
  83.       
  84.     return ret;  
  85. }  
从RTMP协议栈拿到包后,使用在这个作用域一定会释放,所以使用AutoFree就可以保证只释放一次,而且一定释放一次。

AutoFree实现很可靠,用C++的构造和析构,以及宏定义就可以搞定。

  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
/*
The MIT License (MIT)
Copyright (c) 2013 winlin
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef SRS_CORE_AUTO_FREE_HPP
#define SRS_CORE_AUTO_FREE_HPP
/*
#include <srs_core_auto_free.hpp>
*/
#include <srs_core.hpp>
/**
* auto free the instance in the current scope.
*/
#define SrsAutoFree(className, instance, is_array) \
__SrsAutoFree<className> _auto_free_##instance(&instance, is_array)
template<class T>
class __SrsAutoFree
{
private:
T** ptr;
bool is_array;
public:
/**
* auto delete the ptr.
* @is_array a bool value indicates whether the ptr is a array.
*/
__SrsAutoFree(T** _ptr, bool _is_array){
ptr = _ptr;
is_array = _is_array;
}
virtual ~__SrsAutoFree(){
if (ptr == NULL || *ptr == NULL) {
return;
}
if (is_array) {
delete[] *ptr;
} else {
delete *ptr;
}
*ptr = NULL;
}
};
#endif
 来自CODE的代码片
srs_core_auto_free.hpp

对比以下代码,没有使用auto free模式:

[cpp] view plain copy
  1. // ignore when no messages.  
  2. int count = (int)msgs.size();  
  3. if (msgs.empty()) {  
  4.         continue;  
  5. }  
  6.   
  7. // all msgs to forward.  
  8. int i = 0;  
  9. for (i = 0; i < count; i++) {  
  10.         SrsSharedPtrMessage* msg = msgs[i];  
  11.         msgs[i] = NULL;  
  12.   
  13.         // we erased the sendout messages, the msg must not be NULL.  
  14.         srs_assert(msg);  
  15.           
  16.         ret = client->send_message(msg);  
  17.         if (ret != ERROR_SUCCESS) {  
  18.                 srs_error("forwarder send message to server failed. ret=%d", ret);  
  19.   
  20.                 // convert the index to count when error.  
  21.                 i++;  
  22.                   
  23.                 break;  
  24.         }  
  25. }  
  26.   
  27. // clear sendout mesages.  
  28. if (i < count) {  
  29.         srs_warn("clear forwarded msg, total=%d, forwarded=%d, ret=%d", count, i, ret);  
  30. else {  
  31.         srs_info("clear forwarded msg, total=%d, forwarded=%d, ret=%d", count, i, ret);  
  32. }  
  33. msgs.erase(msgs.begin(), msgs.begin() + i);  
  34.   
  35. if (ret != ERROR_SUCCESS) {  
  36.         break;  
  37. }  

使用auto free模式后:

[cpp] view plain copy
  1. // ignore when no messages.  
  2. if (count <= 0) {  
  3.     srs_verbose("no packets to forward.");  
  4.     continue;  
  5. }  
  6. SrsAutoFree(SrsSharedPtrMessage*, msgs, true);  
  7.   
  8. // all msgs to forward.  
  9. for (int i = 0; i < count; i++) {  
  10.     SrsSharedPtrMessage* msg = msgs[i];  
  11.       
  12.     srs_assert(msg);  
  13.     msgs[i] = NULL;  
  14.       
  15.     if ((ret = client->send_message(msg)) != ERROR_SUCCESS) {  
  16.         srs_error("forwarder send message to server failed. ret=%d", ret);  
  17.         return ret;  
  18.     }  
  19. }  

如下图:

原创粉丝点击