首页 > 解决方案 > 为什么 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

标签: c++

解决方案


当您分配给token.t_Stringin 时_token_str_new,您使用new[]. _token_free使用delete,这会导致不匹配。

你需要delete [] token.t_String_token_free.


推荐阅读