fix:修正一个内存泄漏问题。

This commit is contained in:
taynpg 2025-04-09 09:14:30 +08:00
parent 5bbe4f0ecd
commit 119f1e8f72
3 changed files with 20 additions and 1 deletions

View File

@ -122,6 +122,15 @@ void CClient::print_help(bool detail)
TLOGI("{}", sp);
}
void CClient::free_buf_manual(CFrameBuffer* buf)
{
if (buf == nullptr) {
return;
}
delete buf->data_;
buf->data_ = nullptr;
}
void CClient::run(const std::string& ip, const std::string& port, const std::string& config_dir)
{
fs::path fp(config_dir);
@ -1331,6 +1340,8 @@ void CClient::send_file_data_th(const char* keys)
TLOGE("Stop Trans {} To {} failed.", t->cur_file_, str_key);
return;
}
free_buf_manual(buf.get());
buf->type_ = TYPE_TRANS_FILE;
buf->mark_ = 1;

View File

@ -71,6 +71,7 @@ private:
std::string read_uuid();
void get_id();
void print_help(bool detail);
void free_buf_manual(CFrameBuffer* buf);
private:
void handle_frame(CFrameBuffer* buf);

View File

@ -142,7 +142,14 @@ void serialize(CMessageInfo& msg_info, char** out_buf, int& len)
// 计算总长度
len = sizeof(int) * 4 + info.id.size() + info.uuid.size() + info.str.size() + info.data.size() + kz;
*out_buf = new char[len]{}; // 分配内存(调用方负责释放)
// 《这里为了效率》,认为如果 *out_buf 不为空,则直接使用,且长度符合要求
// 调用方负责确保内存一致性和可用性。
// 即,如果调用方及高频率调用 serialize, 且每次 len 固定就复用内存,完了再释放。
// 低频率或者 len 不固定时,每次都释放内存,并置 nullptr。
if (*out_buf == nullptr) {
*out_buf = new char[len] {}; // 分配内存(调用方负责释放)
}
char* ptr = *out_buf + kz;