堆损坏 - 矢量push_back
我似乎有一个损坏的堆,我无法弄清楚为什么会发生这种情况......
以下是 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)
代码只是将结构推入向量中。该结构中有一些字符串变量。如果进一步调试需要源代码,请告诉我。
一些来源:
typedef struct{
string chargeType; // The name of the charge type, eg "Date Units"
string unitSize;
string rate;
bool perConnection;
string cap;
bool useMaxDailyCharge;
string maxDailyCharge;
string identifier;
} chargeRate;
getChargeDetails :
vector<chargeRate> my_vector;
my_vector.push_back(this->getChargeRateDetails(chargeStructureNames[i]));
getChargeRateDetails :
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;
编辑 :我知道这是返回一个“副本”..这是完成了一些测试,我将其插入到通过引用传递的向量副本中。
干杯!
I seem to have a corrupted heap, I cannot figure out why this is happening....
Following is the trace from 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)
The code is just pushing a structure into a vector. The structure has a few string variables in it. Please let me know if the source is required for further debugging.
Some source:
typedef struct{
string chargeType; // The name of the charge type, eg "Date Units"
string unitSize;
string rate;
bool perConnection;
string cap;
bool useMaxDailyCharge;
string maxDailyCharge;
string identifier;
} chargeRate;
getChargeDetails :
vector<chargeRate> my_vector;
my_vector.push_back(this->getChargeRateDetails(chargeStructureNames[i]));
getChargeRateDetails :
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;
Edit : I am aware this is returning a "copy" .. this is done test out a few things, i am inserting the same into a copy of vector passed by reference.
Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果
getChargeRateDetails
通过引用返回,则您正在尝试使用对已经超出范围的变量的引用。In case
getChargeRateDetails
returns by reference you are trying to use a reference to a variable that is already out of scope.看来您的复制构造函数没有做正确的事情。为了将元素推送到向量上,需要创建
chargeRate
的副本。乍一看,告诉我 ChargeRate 复制构造函数尝试向未初始化的变量添加一些内容(由 __atomic_add 的 int volatile* 参数指向) >.
可能,您正在向未初始化的成员变量添加一些内容,
这可能是
StringMatrix
的数组。 code> 未初始化好,因此,第一行可能包含未初始化的类似字符串的内存,用于复制构造新的chargeType
成员。 。string
类包含一个引用计数器,当新的string
指向相同数据时,该引用计数器需要增加,因此您可以通过 ( 来验证这一点 暂时)分配一个空字符串而不是
(*results)[FIRST_ROW][CHARGE_TYPE]
。It looks like your copy constructor doesn't do the right thing. In order to push an element onto a vector, a copy of your
chargeRate
is created. At first sight,Tells me that the
chargeRate
copy constructor tries to add something to an uninitialized variable (pointed at by theint volatile*
argument of__atomic_add
.Probably, you are
__atomic_add
ing something to an uninitialized member variable.Seems intended to refer to an array of
string
s. Chances are that theStringMatrix
is not initialized well. Hence, the first row may contain an uninitializedstring
-like piece of memory, that is used to copy-construct thechargeType
member of your new object.The
string
class contains a reference counter that needs to be increased when a newstring
points to the same data, so this may make sense.You can verify this by (temporarily) assigning e.g. an empty string instead of
(*results)[FIRST_ROW][CHARGE_TYPE]
.不确定您的应用程序中发生了什么,但也许它与(不)使用 back_inserter 有关?
这里有一个很好的例子。
Not sure what is going on in your application, but maybe it has to do with (not) using back_inserter?
there is a good example here.