I'm trying to build an tree kind structure, which will store tokens for an IMAP command. I'm trying to add strings to them, and free them. But valgrind complains, and I do not know why.
#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 says:
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