C++ 堆损坏-向量推回

C++ 堆损坏-向量推回,c++,heap,memory-corruption,C++,Heap,Memory Corruption,我似乎有一个损坏的堆,我不明白为什么会发生这种情况 以下是valgrind留下的痕迹 ==12697== Use of uninitialised value of size 4 ==12697== at 0xDD0725: __gnu_cxx::__atomic_add(int volatile*, int) (in /usr/lib/libstdc++.so.6.0.7) ==12697== by 0x1C3AD9BB: chargeRate::chargeRate(charge

我似乎有一个损坏的堆,我不明白为什么会发生这种情况

以下是valgrind留下的痕迹

==12697== Use of uninitialised value of size 4
==12697==    at 0xDD0725: __gnu_cxx::__atomic_add(int volatile*, int) (in /usr/lib/libstdc++.so.6.0.7)
==12697==    by 0x1C3AD9BB: chargeRate::chargeRate(chargeRate const&) (in /root//app/libapp++.so)
==12697==    by 0x1C4C9C22: __gnu_cxx::__mt_alloc_base<chargeRate>::construct(chargeRate*, chargeRate const&) (mt_allocator.h:585)
==12697==    by 0x1C4CAC9F: std::vector<chargeRate, std::allocator<chargeRate> >::_M_insert_aux(__gnu_cxx::__normal_iterator<chargeRate*, std::vector<chargeRate, std::allocator<chargeRate> > >, chargeRate const&) (vector.tcc:284)
==12697==    by 0x1C4CAF9E: std::vector<chargeRate, std::allocator<chargeRate> >::push_back(chargeRate const&) (stl_vector.h:610)
==12697==    by 0x1C4C8A03: WebTranslations::getChargeDetails(std::vector<std::string, std::allocator<std::string> >&, std::vector<chargeRate, std::allocator<chargeRate> >&) (WebTranslations.cpp:427)
==12697==    by 0x1C4C8F83: WebTranslations::getChargeTranslations(std::vector<std::string, std::allocator<std::string> >&, std::vector<std::string, std::allocator<std::string> >&) (WebTranslations.cpp:1172)

==12697== 
==12697== Invalid read of size 4
==12697==    at 0xDB468B: std::string::string(std::string const&) (in /usr/lib/libstdc++.so.6.0.7)
==12697==    by 0x1C3AD9E0: chargeRate::chargeRate(chargeRate const&) (in /root//app/libapp++.so)
==12697==    by 0x1C4C9C22: __gnu_cxx::__mt_alloc_base<chargeRate>::construct(chargeRate*, chargeRate const&) (mt_allocator.h:585)
==12697==    by 0x1C4CAC9F: std::vector<chargeRate, std::allocator<chargeRate> >::_M_insert_aux(__gnu_cxx::__normal_iterator<chargeRate*, std::vector<chargeRate, std::allocator<chargeRate> > >, chargeRate const&) (vector.tcc:284)
==12697==    by 0x1C4CAF9E: std::vector<chargeRate, std::allocator<chargeRate> >::push_back(chargeRate const&) (stl_vector.h:610)
==12697==    by 0x1C4C8A03: WebTranslations::getChargeDetails(std::vector<std::string, std::allocator<std::string> >&, std::vector<chargeRate, std::allocator<chargeRate> >&) (WebTranslations.cpp:427)
==12697==    by 0x1C4C8F83: WebTranslations::getChargeTranslations(std::vector<std::string, std::allocator<std::string> >&, std::vector<std::string, std::allocator<std::string> >&) (WebTranslations.cpp:1172)
==12697==    by 0x1C299E48: zif__get_charge_translations (in /usr/lib/php4/.so)
==12697==    by 0x1BCE0916: zend_do_fcall_common_helper (in /usr/lib/httpd/modules/libphp5.so)
==12697==    by 0x1BCF1088: zend_do_fcall_handler (in /usr/lib/httpd/modules/libphp5.so)
==12697==    by 0x1BCDDD92: execute (in /usr/lib/httpd/modules/libphp5.so)
==12697==    by 0x1BCE02A9: zend_do_fcall_common_helper (in /usr/lib/httpd/modules/libphp5.so)
==12697==  Address 0xFFFFFFFC is not stack'd, malloc'd or (recently) free'd
==12697== 
==12697== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==12697==  GPF (Pointer out of bounds?)
==12697==    at 0xDB468B: std::string::string(std::string const&) (in /usr/lib/libstdc++.so.6.0.7)
==12697==    by 0x1C3AD9E0: chargeRate::chargeRate(chargeRate const&) (in /root//app/libapp++.so)
==12697==    by 0x1C4C9C22: __gnu_cxx::__mt_alloc_base<chargeRate>::construct(chargeRate*, chargeRate const&) (mt_allocator.h:585)
==12697==    by 0x1C4CAC9F: std::vector<chargeRate, std::allocator<chargeRate> >::_M_insert_aux(__gnu_cxx::__normal_iterator<chargeRate*, std::vector<chargeRate, std::allocator<chargeRate> > >, chargeRate const&) (vector.tcc:284)
==12697==    by 0x1C4CAF9E: std::vector<chargeRate, std::allocator<chargeRate> >::push_back(chargeRate const&) (stl_vector.h:610)
==12697==    by 0x1C4C8A03: WebTranslations::getChargeDetails(std::vector<std::string, std::allocator<std::string> >&, std::vector<chargeRate, std::allocator<chargeRate> >&) (WebTranslations.cpp:427)
==12697==    by 0x1C4C8F83: WebTranslations::getChargeTranslations(std::vector<std::string, std::allocator<std::string> >&, std::vector<std::string, std::allocator<std::string> >&) (WebTranslations.cpp:1172)
获取详细信息:

vector<chargeRate> my_vector;
my_vector.push_back(this->getChargeRateDetails(chargeStructureNames[i]));
vector my_vector;
my_vector.push_back(此->获取ChargeRateDetails(chargeStructureNames[i]);
GetChargeRate详细信息:

where : vector<vector<string> > StringMatrix
StringMatrix *results; //used to retrive results from database.
chargeRate chargeInformation;
...
//populate results, check them
..
chargeInformation.chargeType = (*results)[FIRST_ROW][CHARGE_TYPE];
return chargeInformation;
其中:向量字符串矩阵
矩阵*结果//用于从数据库检索结果。
收费资料;
...
//填充结果,检查它们
..
chargeInformation.chargeType=(*结果)[第一行][费用类型];
返回收费信息;
编辑:我知道这是返回一个“副本”。。这是为了测试一些东西,我将同样的东西插入到通过引用传递的向量的副本中


干杯

不确定应用程序中发生了什么,但可能与(不)使用back_inserter有关?
有一个很好的例子。

看起来你的复制构造函数做的事情不对。为了将元素推送到向量上,将创建
费率的副本。乍一看

at 0xDD0725: __gnu_cxx::__atomic_add(int volatile*, int)
chargeRate::chargeRate(chargeRate const&)
告诉我,
chargeRate
copy构造函数试图向未初始化的变量(由
\uuuu原子添加
int volatile*
参数指向)添加某些内容

很可能,您正在向未初始化的成员变量添加内容

StringMatrix *results;
似乎意在引用
字符串的数组。很可能
字符串矩阵
初始化不好。因此,第一行可能包含未初始化的
字符串
-类似内存,用于复制和构造新对象的
chargeType
成员

string
类包含一个引用计数器,当新的
string
指向相同的数据时,需要增加该计数器,因此这可能是有意义的


您可以通过(临时)分配空字符串而不是
(*结果)[第一行][费用类型]来验证这一点

如果
getChargeRateDetails
通过引用返回,则您试图使用对已超出范围的变量的引用。

源代码将大大简化调试。这似乎是chargeRate类的复制构造函数中的一个问题,push_back()创建struc的副本,因此,您可能会在一个副本中释放一些相关对象,然后在新创建的副本中使用它
chargeRate
结构不包含引用
\uuuu atomic\u add
的副本构造函数。这不计算。很抱歉,我无法理解,即应该有据我所知,编译器..是的,目前我的结构没有定义拷贝常量。我依赖于相同的位拷贝。+1 Uff!良好的分析技能:-)。对于我自己来说,如果我看到像typedef struct{bool..,std::string…}myStruct这样的东西;我不再进一步阅读代码。这根本不值得。@Valentin Heinitz,谢谢,我也希望世界和平和代码库干净。。。不幸的是,我们都知道这不会发生。我们犯错误,我们清理别人犯的错误,并在路上学习。。。但是谢谢你的意见。@xtofl:我正在打印返回前后的结构变量,内容似乎很好。。。这足以说明stringmatrix返回了正确的值。字符串矩阵实际上定义为:vectorStringMatrix@RickoM:回来前后
chargeRate
getChargeRateDetails
函数的本地对象,在“返回后”无效。除非你是通过引用(cfr@sth的答案)返回它。我认为你应该设法找出问题何时消失,并从那里进一步分析。
StringMatrix *results;