有效内存分段错误
我在访问一个在 gdb 中看起来有效且完全可访问的对象时遇到分段错误。分段不是总是与不可访问的内存有关吗?
编辑:更多细节。
崩溃发生在 gdb 下,因此我可以检查对象的内存。它已将成员设置为正确的值,因此我不可能访问只读内存。发生崩溃的指令是一种 Var = Obj.GetMember()
,其中 Var、GetMember 和相应的成员是短整数。 错位?我想这会导致总线错误,而不是分段。我会尝试重建一切。问题是这段代码每秒运行数千次,并且分段每隔几天发生一次。
I get segmentation fault accessing an object which looks valid and fully accessible in gdb. Isn't segmentation is always about inaccessible memory?
EDIT: more details.
The crash happend under gdb so I could examine the object's memory. It had the members set to proper values so there is no chance I was accessing read-only memory. The instruction where crashed happed is kind of Var = Obj.GetMember()
where Var, GetMember and the corresponding member are short integers.
Misalignment? I suppose it would cause bus error, not segmentation. I'll try to rebuild all. The problem is that this piece of code runs thousands times a second and the segmentation happens once in several days.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
即使在某些情况下访问“有效”内存,您也可能会遇到错误:
如果不查看核心转储,就无法区分错误指令(加载/存储/执行)是什么以及所访问内存的映射权限到底是什么。
You can get faults even if accessing "valid" memory under some circumstances:
Without a look at the coredump, to figure out what the faulting instruction (load/store/execute) was and what exactly the mapping permissions for the accessed memory were it's impossible to distinguish.
尝试完全重建(make clean && make),当我遇到如此奇怪的错误时,这对我有帮助。
Late UPD:
如果这确实解决了问题,通常意味着您的 makefile 有问题,通常是 .cpp 和 .h 文件之间的依赖关系搞砸了,例如:a.cpp 包含 bh,但 bh 未在 a 中列出.cpp 的依赖项。
Try complete rebuild (make clean && make), this had helped me a couple of times when I encountered such weird errors.
Late UPD:
If this does fix the problem, it usually means that something is wrong with your makefile, usually screwed-up dependencies between .cpp and .h files, for example: a.cpp includes b.h, but b.h is not listed in a.cpp's dependencies.
基本上,是的。您是否使用核心转储来分析您的段错误?
Basically, yes. Did you use the core dump to analyze your seg fault?
代码会很有帮助,但是你做了 make clean 吗?如果您增加了类的大小并且您的依赖项不正确,则不会为实例分配足够的空间,并且该类将溢出并损坏内存中它前面的任何内容。
Code would very much help, but have you done a make clean? If you've increased the size of a class and your dependencies aren't right then there won't be enough space allocated for an instance and that class will then overrun and corrupt whatever it precedes in memory.