c – 由于#pragma pack错误引起的内存损坏 – std map损坏 – 在插入时崩溃

前端之家收集整理的这篇文章主要介绍了c – 由于#pragma pack错误引起的内存损坏 – std map损坏 – 在插入时崩溃前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个项目,我正在与std地图有一些奇怪的行为.

我定义了自己的typedef映射,它将字符串映射到自定义类型的指针.在我将第一对添加到地图后,应用程序在我超出地图的任何时候崩溃了.

在经历了很多混乱之后,我将地图更改为a并将其移动到我的应用程序中的第一个调用,它仍然崩溃.我不知道会发生什么.任何帮助,将不胜感激.

这是目前崩溃的代码.

  1. LoggerPtr syslogger(Logger::getLogger("CISInterface"));
  2.  
  3. int main(int argc,char *argv[])
  4. {
  5. typedef std::map<string,string> MyMapDef;
  6. MyMapDef tmpString;
  7. tmpString.insert(MyMapDef::value_type("0000","d"));
  8. tmpString.insert(MyMapDef::value_type("1111","d")); //Crashes here.
  9. tmpString.insert(MyMapDef::value_type("2222","d"));
  10.  
  11. // std::string configFile;
  12. // int c;
  13. // if(argc < 2)
  14. // {
  15. // //Must have c option
  16. // std::cout << "Usage -c configFileName" << std::endl;
  17. // exit(EXIT_FAILURE);
  18. // }
  19. //Rest of main commented out.
  20. ...

这是堆栈跟踪 –

  1. CISInterface Debug [C/C++ Application]
  2. gdb/mi (10/31/12 6:02 PM) (Suspended)
  3. Thread [1] (Suspended: Signal 'SIGSEGV' received. Description: Segmentation fault.)
  4. 6 std::basic_string<char,std::char_traits<char>,std::allocator<char> >::compare(std::basic_string<char,std::allocator<char> > const&) const() 0x00000032fd49c416
  5. 5 std::operator< <char,std::allocator<char> >() basic_string.h:2317 0x0000000000417ec7
  6. 4 std::less<std::basic_string<char,std::allocator<char> > >::operator() stl_function.h:230 0x000000000041706f
  7. 3 std::_Rb_tree<std::basic_string<char,std::allocator<char> >,std::pair<std::basic_string<char,std::allocator<char> > const,std::basic_string<char,std::allocator<char> > >,std::_Select1st<std::pair<std::basic_string<char,std::allocator<char> > > >,std::less<std::basic_string<char,std::allocator<std::pair<std::basic_string<char,std::allocator<char> > > > >::_M_insert_unique() stl_tree.h:1170 0x0000000000415d00
  8. 2 std::map<std::basic_string<char,std::allocator<char> > > > >::insert() stl_map.h:500 0x00000000004150eb
  9. 1 main() CISInterface.cpp:29 0x000000000041916d
  10. gdb (10/31/12 6:02 PM)
  11. /home/cillian/workspace/CISInterface/Debug/CISInterface (10/31/12 6:02 PM)

我应该关注哪些其他方面可能会导致问题.它可以在我链接的库中吗?我创建了第二个项目,只有这些代码链接相同的库(但没有任何代码可以调用它们.)并且它不会崩溃.

解决方法

问题解决了.

我以为我会把它添加到其他任何人做同样事情的机会.

我慢慢删除了项目中的文件,试图找到有问题的文件.我认为它必须是头文件中定义的导致问题的东西(如静态).花了很长时间,但我想我已经找到了.我有一个头文件,定义了许多结构.这些是序列化到线路,所以我使用#pragma pack(push)对齐它们,我放在文件的顶部,#pragma pack(pop)在底部.但是我在第一个#pragma定义之后添加了几个#include语句,这意味着这些包含错误地对齐并导致一些不确定的行为.谢谢大家看看.应该使用属性语法,我不会有问题.以下是违规代码的完整性.

  1. #pragma pack (push)
  2. #pragma pack (1)
  3.  
  4. #include <string> //Wrong place for includes!
  5. #include <Units.h>
  6.  
  7. typedef struct
  8. {
  9. ....
  10. }
  11. #pragma pack (pop)

感谢所有看过初始问题的人.

猜你在找的C&C++相关文章