问题描述
我在Linux上有一个C ++程序,经过一段时间后出现以下消息,该程序崩溃了:
I have a C++ program on Linux that crashes after some time with the message:
*** glibc detected *** free(): invalid pointer: 0x41e0ce94 ***
在程序内部,我大量使用了容器.它们必须存储简单类的对象.
Inside the program I make extensive use of containers. They have to store objects of a simple class.
编辑 2009-4-17:
EDIT 2009-4-17:
同时,很明显该错误与简单类无关.如果我更改容器以容纳其他数据类型,仍然会发生错误.问题一定在我的代码中的其他地方,此刻我正在尝试解决这个问题...
In the meantime it seems clear that the error has nothing to do with the simple class. The error still occurs if I change the containers to hold other datatypes. The problem must be somewhere else in my code, I'm trying to figure it out at the moment...
推荐答案
考虑使用std :: string而不是原始char指针保存字符串值.这样,您就不必担心在分配,复制和销毁方法中管理字符串数据了.您的问题很可能就在那里.
Consider using a std::string to hold the string value instead of a raw char pointer. Then you won't have to worry about managing the string data in your assignment, copy, and destruction methods. Most likely your problem lies there.
发布的较新类没有问题,如果仅使用char *指向字符串常量,则第一个版本也没有问题.问题出在程序的其他地方或您使用类的方式.您将需要花费更多的时间来挖掘调试器和/或valgrind以查找问题.我想弄清楚指定地址指向的内容,然后尝试确定为什么将其释放两次.
There's no issue with the newer class you posted, and no problem with the first version if you're only using the char * to point to string constants. The problem lies elsewhere in the program or with the way you're using the class. You'll have to spend more time digging in the debugger and/or valgrind to track down the problem. I would figure out what is pointed to at the specified address and try determine why it's being freed twice.
这篇关于容器问题:***检测到glibc *** free():无效的指针:0x41e0ce94 ***的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!