简体   繁体   English

写入时出现段错误,但读取时没有

[英]Getting a seg fault when writing, but not when reading

I initialize a pointer from a list of pointers.我从指针列表初始化一个指针。 I can read data from this pointer without a problem(line 2), but when I try to write any data(line 3), it crashes.我可以毫无问题地从此指针读取数据(第 2 行),但是当我尝试写入任何数据(第 3 行)时,它崩溃了。

When i put a breakpoint on line 3, the debugger shows that the pointer is pointing to the correct object based on its variables.当我在第 3 行放置断点时,调试器显示指针基于其变量指向正确的对象。

DimensionItem *DI = DimItems[j];
qDebug() << "DI xposition: " << DI->pos().x();
DI->setPos(1,1);

If I breakpoint on line 3, my application output is:如果我在第 3 行断点,我的应用程序输出是:

::Debug::{(../Zedspex/Widgets/automeasurewidget.cpp:434)}:: DI xposition:  5.15645e-312

And I can see data in DI in the debugger:我可以在调试器中看到 DI 中的数据: 段错误前调试器中的 DI

A pop-up saying a seg fault occurred.一个弹出窗口说发生了段错误。 This is the only message from the dubugger:这是来自 dubugger 的唯一消息:

signal 11 (Segmentation fault), address is 0x2c from 0x4340cec4
[bt]: (1) /usr/lib/libQt5Widgets.so.5(_ZN13QGraphicsItem6setPosERK7QPointF+0x8c) [0x4340cec4]

The contents of memory might stick around even after you have released it, so reading from invalid pointers may appear to work.即使在您释放内存之后,它的内容可能仍然存在,因此从无效指针中读取可能看起来有效。 On the other hand, if you write to that location you may get an exception.另一方面,如果您写入该位置,您可能会遇到异常。

signal 11 (Segmentation fault), address is 0x2c from 0x4340cec4信号 11(分段错误),地址是来自 0x4340cec4 的 0x2c

An address of 0x2c is very low and suggests that the pointer is null . 0x2c的地址非常低,表明指针为null

The very little evidence you have provided suggests that the pointer is in fact not valid.您提供的极少证据表明该指针实际上无效。 It may have been released (usually with delete in C++) at some point or overridden with a bad address.它可能在某个时候被释放(通常在 C++ 中使用delete )或被错误地址覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM