stl::map的insert插入数据报错:(Suspended : Signal : SIGSEGV:Segmentation fault)

最近接手一个老项目,在老项目中使用了map存储缓存数据,如下:

#include "conf.h"
int Frame::parse()
{

    std::map<string, int64_t> tmpString;
    tmpString.insert(std::map<string, int64_t>::value_type(string("0000"), 12123323));
    tmpString.insert(std::map<string, int64_t>::value_type(string("1111"), 54678456)); //Crashes here.
    tmpString.insert(std::map<string, int64_t>::value_type(string("2222"), 45654548));
}

结果发现每次在insert第二次时程序core。

堆栈如下:

Thread [3] 29672 [core: 2] (Suspended : Signal : SIGSEGV:Segmentation fault)
	std::basic_string<char, std::char_traits<char>, std::allocator<char> >::compare(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const at 0x354669c47d
	std::operator< <char, std::char_traits<char>, std::allocator<char> >() at basic_string.h:2,317 0x449d91
	std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::operator() at stl_function.h:230 0x44a46b
	std::_Rb_tree<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, unsigned long>, std::_Select1st<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, unsigned long> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, unsigned long> > >::_M_insert_unique() at stl_tree.h:1,170 0x44ef40
	std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned long, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, unsigned long> > >::insert() at stl_map.h:500 0x44e9f5
	Frame::parse() at AnySDFrame.cpp:220 0x44df6b	

查找了一上午也没找到原因。最后在stackoverflow中找到了相同的问题,结果找到了原因。

在“conf.h”文件中,我定义了一些结构体,并使用了#pragma pack(4)来设置结构体内存对齐,如下:

#include <string> 

#pragma pack (4)

typedef struct
{
....
}

原因是:没有在“conf.h”文件中没有用“#pragma pack()”恢复字节对齐,导致包含“conf.h”文件的文件都是以4字节对齐,这样可能导致map insert函数报错。

stackoverflow中也说明了器错误的应用头文件,导致了这一问题:

Problem solved.

Thought I‘d add it here on the off chance anyone else ever does the same thing.

I slowly removed files in my project to try and find the offending file. I was thinking that it must be something defined in a header file that was causing issues (like a static). It took a long time but I think I‘ve found it. I had a header file that defines
a number of structs. These are serialized to the wire so I had them 1 byte aligned using #pragma pack (push) which I put at the top of the file and #pragma pack (pop) at the bottom. But I then added a couple of #include statements after the first #pragma definition
meaning that these includes were aligned incorrectly and caused some nondeterministic behavior. Thanks everyone that had a look. Should probably use the attribute syntax
and I wouldn‘t had the problem. Offending code is below for completeness.

例如下代码:

#pragma pack (push)
#pragma pack (1)

#include <string> //Wrong place for includes!
#include <Units.h> 

typedef struct
{
....
}
#pragma pack (pop) 

Thanks to everyone who had a look at initial problem.

参考:http://stackoverflow.com/questions/13153047/memory-corruption-due-to-pragma-pack-error-std-map-corruption-crashing-on-in

时间: 2024-10-13 13:34:54

stl::map的insert插入数据报错:(Suspended : Signal : SIGSEGV:Segmentation fault)的相关文章

支持泛型AVL Tree的简单实现,并和STL map比较了插入,删除,查找的性能

1.问题描述: 1)AVL tree是一种自平衡树.它通过左右子树的高度差来控制树的平衡,当高度差是不大于1的时候,认为树是平衡的.树的平衡保证了树在极端情况下 (输入序列不够随机)的性能.很显然当左右子树高度平衡,保证了任何插入,删除,查找操作平均性能呢个,当不平衡时(有的子树很高),当 要操作的元素在这个子树时,性能会很差: 2)AVL tree 和Red black tree 都是一种平衡树,它的操作的时间复杂度是:O(lgN) ,N是树的节点的数目: 3)本文实现了AVL Tree, 并

Entity Framework插入数据报错:Validation failed for one or more entities. See &#39;EntityValidationErrors&#39; property for more details.

stackoverflow的解决方法 try { db.Entity.Add(entity); db.SaveChanges(); } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { System

向Oracle数据库中CLOB插入数据报错问题

今天在项目中向数据库的CLOB属性插入一段篇文章(1000~2000)字就会报一个字符串过长的错误. 网上说用流来处理,没有这么做.这像是一个Bug,只要把插入的数据,默认扩充到2000以上就ok了. 下面是这段代码: if((temp.length()>=1000)&&(temp.length()<=2000)){ temp=StringUtils.rightPad(temp, 2008); } 使用StringUtils的rightPad方法使没超过2000的部分,在右边自

解决mysql插入数据报错[Err] 1146 - Table &#39;performance_schema.session_status&#39; doesn&#39;t exist

解决办法:1.打开cmd 执行命令cd/ 进入C盘根目录2.dir 查看C盘根目录下文件夹  找到 Program Files文件夹3.cd Program Files 进入该文件夹下 再输入dir 查看该文件夹下所有文件夹4.cd MySQL 进入该文件夹  再输入dir 查看该文件夹下所有文件夹5.cd MySQLServer 5.7 进入该文件夹  再输入dir 查看该文件夹下所有文件夹6.cd bin 进入该文件夹  7.输入mysql_upgrade -u root -p --forc

mysql插入数据报错IntegrityError: (1062, &quot;Duplicate entry &#39;xx&#39; for key &#39;xxxxx&#39;&quot;)

1.问题描述 MySQL插入数据的时候报错,提示如下: IntegrityError: (1062, "Duplicate entry 'xx' for key 'xxxxx'")2.问题分析 根据错误提示我们可以大致得到以下信息: 1.IntegrityError表示这个字段在MySQL中的类型为:整型. 2.Duplicate Entry:“Duplicate”——something that is an exact copy  of something else.所以这句话的意思

mysql插入数据报错1366

数据表插入中文数据报错 Warning Code : 1366 Incorrect string value: '\xE5\x9C\xA8' for column 'name' at row 1 原因是表中列的编码是默认 latin1 要改成utf8 ALTER TABLE student CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci 把student表列编码改成utf8 SHOW FULL COLUMNS FROM student

程序插入数据报错ORA-01461: can bind a LONG value only for

我的ORACLE表里没有long字段,可是保存时报错: ORA-01461 :仅可以为插入LONG列的LONG值赋值 ORA-01461: can bind a LONG value only for insert into a LONG column 本来我这张表里只有一个VARCHAR2(4000)的字段,一直没有这种错误发生,后来我把另一个字段长度调整为VARCHAR2(4000),这错误就开始发生了. 你的数据库字符集可能是UTF-8的,对于UTF-8或欧洲的某些字符集,oracle在存

Mycat+Mysql 插入数据报错 i[Err] 1064 - partition table, insert must provide ColumnList

使用Navicat连接Mycat 8066 成功插入了分库表和全局表 1.全局表 sql如下: INSERT INTO `t_rank` VALUES ('259bfdc3-7922-4839-96c7-61c89e877dc5', '法国', '7', '11', '11', '12', null, '34'); INSERT INTO `t_rank` VALUES ('41eece5d-9d86-4cfe-b0ce-e6d4e4021cac', '中国', '2', '38', '27',

数据库往表中插入数据报错

[Error Code: 1452, SQL State: 23000]  Cannot add or update a child row: a foreign key constraint fails (`workflow`.`act_id_membership`, CONSTRAINT `ACT_FK_MEMB_USER` FOREIGN KEY (`USER_ID_`) REFERENCES `act_id_user` (`ID_`)) sql: INSERT INTO act_id_m