Valgrind - 有条件的跳跃或移动取决于未初始化的值

时间:2016-04-05 05:28:40

标签: c++ valgrind

我从2个上下文得到valgrind错误。这是第一个。它声称我有未初始化的价值,但我没有看到任何。请帮助我理解它告诉我问题所在:

        valgrind --leak-check=yes --track-origins=yes ./Lab4 in62.txt actual.txt
==4041== Memcheck, a memory error detector
==4041== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==4041== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==4041== Command: ./Lab4 in62.txt actual.txt
==4041== 
==4041== Conditional jump or move depends on uninitialised value(s)
==4041==    at 0x409F2C: __gnu_cxx::__enable_if<std::__is_char<char>::__value, bool>::__type std::operator==<char>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.h:4913)
==4041==    by 0x40AE3F: bool std::operator!=<char, std::char_traits<char>, std::allocator<char> >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.h:4950)
==4041==    by 0x407FFD: Relation::join_createMap(std::deque<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >, std::allocator<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > > >&, std::vector<Relation, std::allocator<Relation> >&, Rule&, int&, bool&, bool&, Tuple const&, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >&, std::vector<int, std::allocator<int> >&, std::vector<int, std::allocator<int> >&) (relation.cpp:317)
==4041==    by 0x408F87: Relation::join(std::vector<Relation, std::allocator<Relation> >&, Rule&, std::vector<Rule, std::allocator<Rule> >&) (relation.cpp:438)
==4041==    by 0x42069C: Database::ruleEvaluation(std::vector<Rule, std::allocator<Rule> >&, std::vector<Predicate, std::allocator<Predicate> >&, std::vector<Predicate, std::allocator<Predicate> >&, std::basic_ofstream<char, std::char_traits<char> >&) (database.cpp:77)
==4041==    by 0x41FA97: Database::evaluate(std::vector<Predicate, std::allocator<Predicate> >&, std::vector<Predicate, std::allocator<Predicate> >&, std::vector<Rule, std::allocator<Rule> >&, std::vector<Predicate, std::allocator<Predicate> >&, std::basic_ofstream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (database.cpp:12)
==4041==    by 0x42822C: main (main.cpp:27)
==4041==  Uninitialised value was created by a heap allocation
==4041==    at 0x4C2C12F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4041==    by 0x41642A: __gnu_cxx::new_allocator<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > >::allocate(unsigned long, void const*) (new_allocator.h:104)
==4041==    by 0x414A62: std::allocator_traits<std::allocator<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > > >::allocate(std::allocator<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > >&, unsigned long) (alloc_traits.h:360)
==4041==    by 0x411C7F: std::_Deque_base<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >, std::allocator<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > > >::_M_allocate_node() (stl_deque.h:601)
==4041==    by 0x41553C: std::_Deque_base<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >, std::allocator<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > > >::_M_create_nodes(std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >**, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >**) (stl_deque.h:726)
==4041==    by 0x4129DE: std::_Deque_base<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >, std::allocator<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > > >::_M_initialize_map(unsigned long) (stl_deque.h:700)
==4041==    by 0x40EB1F: std::_Deque_base<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >, std::allocator<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > > >::_Deque_base() (stl_deque.h:490)
==4041==    by 0x40B337: std::deque<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >, std::allocator<std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > > > >::deque() (stl_deque.h:883)
==4041==    by 0x408DC3: Relation::join(std::vector<Relation, std::allocator<Relation> >&, Rule&, std::vector<Rule, std::allocator<Rule> >&) (relation.cpp:425)
==4041==    by 0x42069C: Database::ruleEvaluation(std::vector<Rule, std::allocator<Rule> >&, std::vector<Predicate, std::allocator<Predicate> >&, std::vector<Predicate, std::allocator<Predicate> >&, std::basic_ofstream<char, std::char_traits<char> >&) (database.cpp:77)
==4041==    by 0x41FA97: Database::evaluate(std::vector<Predicate, std::allocator<Predicate> >&, std::vector<Predicate, std::allocator<Predicate> >&, std::vector<Rule, std::allocator<Rule> >&, std::vector<Predicate, std::allocator<Predicate> >&, std::basic_ofstream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (database.cpp:12)
==4041==    by 0x42822C: main (main.cpp:27)
[more errors]
==5323== 
==5323== 
==5323== HEAP SUMMARY:
==5323==     in use at exit: 72,704 bytes in 1 blocks
==5323==   total heap usage: 29,328 allocs, 29,327 frees, 3,430,706 bytes allocated
==5323== 
==5323== LEAK SUMMARY:
==5323==    definitely lost: 0 bytes in 0 blocks
==5323==    indirectly lost: 0 bytes in 0 blocks
==5323==      possibly lost: 0 bytes in 0 blocks
==5323==    still reachable: 72,704 bytes in 1 blocks
==5323==         suppressed: 0 bytes in 0 blocks
==5323== Reachable blocks (those to which a pointer was found) are not shown.
==5323== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==5323== 
==5323== For counts of detected and suppressed errors, rerun with: -v
==5323== ERROR SUMMARY: 309 errors from 2 contexts (suppressed: 0 from 0)

main.cpp中:25-27

std::ofstream out;
std::string arg = std::string(argv[2]);
myDatabase.evaluate(myDatalogProgram.getFactsVector(),myDatalogProgram.getQueriesVector(),myDatalogProgram.getRulesVector(),myDatalogProgram.getSchemesVector(), out, arg);

加入:

(relation.cpp:425是第6行,来自以下函数的std::deque<strPairMap> working_tuples_deque;

Relation Relation::join(std::vector<Relation> &cur_relation_vector, Rule &cur_rule, std::vector<Rule> &rules_vector){
    bool dup_scheme_params = false;
    strIntMap new_scheme_map;
    Scheme new_scheme;
    join_MakeScheme(cur_relation_vector, dup_scheme_params, new_scheme_map, new_scheme);
    std::deque<strPairMap> working_tuples_deque; //relation.cpp:425
    std::deque<strPairMap> working_tuples_deque_final;
    std::vector<int> grand_copy_vector;
    for (int j = 0; j < cur_relation_vector.size(); j++){
        for (auto &x: cur_relation_vector.at(j).tuple_set){
            bool diff_id_found = false;
            strPairMap temp_map;
            std::vector<int> map_indeces_to_copy;
            int added_to_vector = 0;
            if (working_tuples_deque.size() == 0){
                join_createFirstMap(grand_copy_vector, working_tuples_deque, temp_map, cur_relation_vector, added_to_vector, j, x);
            }
            else{
                join_createMap(working_tuples_deque, cur_relation_vector, cur_rule, j, dup_scheme_params, diff_id_found, x, temp_map, map_indeces_to_copy, grand_copy_vector);
                join_combinePrevMapsToCurMap(diff_id_found, map_indeces_to_copy, temp_map, grand_copy_vector, cur_relation_vector, working_tuples_deque,j);
            }
        }
    }
    join_removeUnworthyMaps(working_tuples_deque, working_tuples_deque_final, grand_copy_vector, new_scheme);
    /* Create new relation with new properties */
    Relation combo_relation(cur_rule.primary_predicate.getID(),new_scheme,{});
    std::set<Tuple> temp_tuple_set;
    for (auto& o: working_tuples_deque_final){
        Tuple temp_tuple;
        for (auto& z: o){
            temp_tuple.push_back(z.second.first);
        }
        temp_tuple_set.insert(temp_tuple);
    }
    combo_relation.tuple_set = temp_tuple_set;
    return combo_relation;
}

join_createMap:

void Relation::join_createMap(std::deque<strPairMap> &working_tuples_deque, std::vector<Relation> &cur_relation_vector, Rule &cur_rule, int &j, bool &dup_scheme_params, bool &diff_id_found, const Tuple &x, strPairMap &temp_map, std::vector<int> &map_indeces_to_copy, std::vector<int> &grand_copy_vector){
    for (int l = 0; l < working_tuples_deque.size(); l++){
        bool add_index_to_vector = true;
        for (int k = 0; k < cur_relation_vector.at(j).scheme.size(); k++){
            /* If curent scheme param k is found in map l in working deque */
            if (working_tuples_deque.at(l).find(cur_relation_vector.at(j).scheme.at(k))->second.second != cur_relation_vector.at(j).name){ //relation.cpp:317
                diff_id_found = true;
                if (working_tuples_deque.at(l).find(cur_relation_vector.at(j).scheme.at(k)) != working_tuples_deque.at(l).end()){
                    std::string param_to_match = working_tuples_deque.at(l).find(cur_rule.predicate_vector.at(j).parameter_vector.at(k).toString())->second.first;
                    /* but if the values with the matching scheme params don't match.. */
                    if (param_to_match != x.at(k)){
                        add_index_to_vector = false;
                    }
                }
            }
            std::pair<std::string, std::string> temp_pair("","");
            temp_pair.first = x.at(k);
            temp_pair.second = cur_relation_vector.at(j).name;
            temp_map.emplace(cur_relation_vector.at(j).scheme.at(k), temp_pair);
        }
        if (add_index_to_vector == true || dup_scheme_params == false){
            map_indeces_to_copy.push_back(l);
            grand_copy_vector.push_back(l);
        }
    }
}

什么类型的变量未被初始化?我们如何使用提供的信息确定它?

更新

我现在意识到问题出在relation.cpp:317内,join_createMap

if (working_tuples_deque.at(l).find(cur_relation_vector.at(j).scheme.at(k))->second.second != cur_relation_vector.at(j).name){...}

当我打破局面时:

for (int k = 0; k < cur_relation_vector.at(j).scheme.size(); k++){
/* If curent scheme param k is found in map l in working deque */
strPairMap::iterator compare_it = working_tuples_deque.at(l).find(cur_relation_vector.at(j).scheme.at(k));
std::pair<std::string,std::string> compare_pair = compare_it->second; // offending line
std::string compare_string = compare_pair.second;
if (compare_string != cur_relation_vector.at(j).name){...}
...
}

Valgrind瞄准了这一行:

std::pair<std::string,std::string> compare_pair = compare_it->second;

出现以下所有错误:

Source and destination overlap in memcpy(0x5ea3040, 0x5b2c450, 95601552)
Invalid read of size 8
Conditional jump or move depends on uninitialised value(s)
Uninitialised value was created by a heap allocation
Conditional jump or move depends on uninitialised value(s)
Uninitialised value was created by a heap allocation
terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_M_construct null not valid

Process terminating with default action of signal 6 (SIGABRT)

1 个答案:

答案 0 :(得分:0)

违规行#是relation.cpp:317不是 main.cpp:27。 (Valgrind从问题的根源追溯到最广泛的范围。)find函数有时会返回working_tuples_deque.at(l).end()。然后代码继续访问.end()的{​​{1}}元素,该元素无效。这就是Valgrind错误的起源。

至于解决方案,我首先初始化了一个单独的secondstd::string)。然后我使用compare_string语句仅在if未返回compare_string迭代器(即find函数实际找到的内容)时重新分配.end()

结果:

find
信任:所有评论员