问题表征
我负责的功能使用了一个MAP(我们团队重写的std::map),在往MAP里添加元素的时候segv。
问题分析
这个MAP的key是个struct,里面有6个VECTOR(重写的std::vector)。先是研究了半天这个struct的lessThan函数,发现没有问题。
下一步,gdb查看了每个元素添加进MAP的过程,一共添加了3000+次,也没有问题。
后来发现,MAP本身所在的memPool和MAP中key里6个VECTOR使用的memPool不一致。进一步发现插入key前的struct使用的memPool和MAP的memPool不是一个。
MAP所在的memPool会被用来做cache,即会被保留将来使用。而struct的memPool是每次计算用的临时memPool,每次计算完会rewind。所以MAP中的key指向的位置可能已经无效了。
至于为什么在MAP加入key的时候(copy一份key在MAP中),使用的是key的memPool而不是MAP的memPool,原因在VECTOR的copy构造函数使用的是VECTOR的memPool。如果是一般对象就没有问题了。
解决方法
创建struct对象的时候,使用MAP所在的memPool。
受教了!呵呵!
How wonderful it is to be dreaming!
Hi there, I desire to subscribe for this weblog to obtain most recent updates,
therefore where can i do it please help.
Please let me know if you’re looking for a writer for your
site. You have some really good articles and I think
I would be a good asset. If you ever want to take
some of the load off, I’d really like to write some content for your blog in exchange for a link back to mine.
Please shoot me an email if interested. Cheers!
The post is useful
Thanks. How wonderful it is to be dreaming!
Please let me know if you’re looking for a writer for your
site. You have some really good articles and I think