c++ - 为什么 valgrind 谈论“不匹配的免费()”
问题描述
我正在尝试构建一个树型结构,它将存储 IMAP 命令的令牌。我正在尝试向它们添加字符串,并释放它们。但是 valgrind 抱怨,我不知道为什么。
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
typedef enum : uint8_t
{
TT_STRING,
TT_INT32,
TT_INT64,
TT_CHAR,
TT_PAIR
} TokenType;
typedef struct
{
int32_t p_From;
int32_t p_To;
} Pair;
struct Token
{
union {
char *t_String;
int32_t t_Int32;
int64_t t_Int64;
char t_Char;
Pair t_Pair;
};
TokenType t_Type;
std::vector<Token> t_Children;
};
typedef struct Token Token;
void _token_free(Token &token)
{
if (token.t_Type == TT_STRING)
{
delete token.t_String;
}
for_each(token.t_Children.begin(), token.t_Children.end(), [=](Token &t){
_token_free(t);
});
}
Token _token_str_new(const std::string &str)
{
Token token;
token.t_Type = TT_STRING;
token.t_String = new char[str.size() + 1];
memcpy(reinterpret_cast<void *>(token.t_String), str.c_str(), str.size() + 1);
return token;
}
int main() {
Token token;
token.t_Int64 = 123;
token.t_Type = TT_INT64;
token.t_Children = {
_token_str_new("Hello World")
};
_token_free(token);
return 0;
}
Valgrind 说:
Mismatched free() / delete / delete []
_token_free(Token&)
_token_free(Token&)::{lambda(Token&)#1}::operator()(Token&) const
_token_free(Token&)::{lambda(Token&)#1} std::for_each<__gnu_cxx::__normal_iterator, _token_free(Token&)::{lambda(Token&)#1}>(__gnu_cxx::__normal_iterator<Token*, std::vector>, _token_free(Token&)::{lambda(Token&)#1}, _token_free(Token&)::{lambda(Token&)#1})
_token_free(Token&)
main
Address 0x4dc0c80 is 0 bytes inside a block of size 12 alloc'd
operator new[](unsigned long)
_token_str_new(std::__cxx11::basic_string<char, std::char_traits, std::allocator> const&)
main
解决方案
当您分配给token.t_String
in 时_token_str_new
,您使用new[]
. _token_free
使用delete
,这会导致不匹配。
你需要delete [] token.t_String
在_token_free
.
推荐阅读
- matlab - 如何为 2D 中的移动对象创建正弦路径,其中正弦波上的点由测量时间和对象的速度决定
- shell - 使嵌套的 awk 容错
- javascript - 为 Material.io 设置环境的资源?
- yii2 - 如何设置 YII 以使用与 PDO 不同的驱动程序
- graphql - 使用 graphql 从数据库中获取数据
- c - 使用 Xlib 处理非事件驱动任务的正确方法是什么?
- android - 用于键盘和编辑文本的默认 IME 操作的自定义图标
- angular - 如何在 Angular 中为组件设置背景图像?
- java - 哲学家进餐问题发生死锁时如何抛出异常?
- ios - Alamofire 5 swift 5 等待alamofire查询结果显示初始视图