在 QString toUtf8() 中崩溃
Crash in QString toUtf8()
我有一个插槽:
void Foo::func(QString str1, const QString& str2, int width, int height)
{
std::unique_lock<std::mutex> _lock(m_mutex);
#ifdef _DEBUG
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
if (GlobalMemoryStatusEx(&statex)) {
qDebug() << QString("There are %1 free MB of physical memory.n").arg(statex.ullAvailPhys / 1024 / 1024);
}
#endif
BaseClass::someFunc(
str1.toStdString(),
str2.toUtf8().constData(),
width,
height
);
}
这似乎是正确的,并且有效。但是,如果程序长时间工作(例如晚上),它会在此功能中崩溃
str2.toUtf8().constData()
我最初以为这是一个基于线程的错误,但我的锁不起作用。所有局部变量和类成员都可以。崩溃错误是:
First-chance exception at 0x76E45B68 foo.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x00EFC6EC
但是从上面的代码中,最后一个 MEMORYSTATUSEX 显示可用内存是
There are 2314 free MB of physical memory
我也认为 str2 太大了,但它适用于 49152、168441等长度。问题出在哪里?我错过了什么吗?
堆栈跟踪:
KernelBase.dll!_RaiseException@16() Unknown
[External Code]
Qt5Cored.dll!qBadAlloc() Line 2849 C++
Qt5Cored.dll!QByteArray::QByteArray(int size, Qt::Initialization __formal) Line 1409 C++
Qt5Cored.dll!QUtf8::convertFromUnicode(const QChar * uc, int len) Line 151 C++
Qt5Cored.dll!QString::toUtf8_helper(const QString & str) Line 4373 C++
Qt5Cored.dll!QString::toUtf8() Line 56 C++
foo.exe!Foo::func(QString str1, const QString & str2, int width, int height) Line 3600 C++
foo.exe!Foo::qt_static_metacall(QObject * _o, QMetaObject::Call _c, int _id, void * * _a) Line 1126 C++
Qt5Cored.dll!QMetaObject::activate(QObject * sender, int signalOffset, int local_signal_index, void * * argv) Line 3717 C++
Qt5Cored.dll!QMetaObject::activate(QObject * sender, const QMetaObject * m, int local_signal_index, void * * argv) Line 3582 C++
foo.exe!SomeClass::func(QString _t1, const QString & _t2, int _t3, int _t4) Line 137 C++
foo.exe!SomeClass::qt_static_metacall(QObject * _o, QMetaObject::Call _c, int _id, void * * _a) Line 81 C++
Qt5Cored.dll!QMetaCallEvent::placeMetaCall(QObject * object) Line 485 C++
Qt5Cored.dll!QObject::event(QEvent * e) Line 1246 C++
Qt5Widgetsd.dll!QApplicationPrivate::notify_helper(QObject * receiver, QEvent * e) Line 3720 C++
Qt5Widgetsd.dll!QApplication::notify(QObject * receiver, QEvent * e) Line 3164 C++
Qt5Cored.dll!QCoreApplication::notifyInternal(QObject * receiver, QEvent * event) Line 935 C++
Qt5Cored.dll!QCoreApplication::sendEvent(QObject * receiver, QEvent * event) Line 228 C++
Qt5Cored.dll!QCoreApplicationPrivate::sendPostedEvents(QObject * receiver, int event_type, QThreadData * data) Line 1552 C++
Qt5Cored.dll!QCoreApplication::sendPostedEvents(QObject * receiver, int event_type) Line 1410 C++
qwindowsd.dll!QWindowsGuiEventDispatcher::sendPostedEvents() Line 81 C++
Qt5Cored.dll!qt_internal_proc(HWND__ * hwnd, unsigned int message, unsigned int wp, long lp) Line 414 C++
[External Code]
Qt5Cored.dll!QEventDispatcherWin32::processEvents(QFlags<enum QEventLoop::ProcessEventsFlag> flags) Line 807 C++
qwindowsd.dll!QWindowsGuiEventDispatcher::processEvents(QFlags<enum QEventLoop::ProcessEventsFlag> flags) Line 73 C++
Qt5Cored.dll!QEventLoop::processEvents(QFlags<enum QEventLoop::ProcessEventsFlag> flags) Line 129 C++
Qt5Cored.dll!QEventLoop::exec(QFlags<enum QEventLoop::ProcessEventsFlag> flags) Line 204 C++
Qt5Cored.dll!QCoreApplication::exec() Line 1188 C++
Qt5Guid.dll!QGuiApplication::exec() Line 1508 C++
Qt5Widgetsd.dll!QApplication::exec() Line 2957 C++
foo.exe!WinMain(HINSTANCE__ * __formal, HINSTANCE__ * __formal, char * lpCmdLine, int __formal) Line 348 C++
[External Code]
提前感谢!
对于那些像我一样偶然发现这个线程的人来说,因为他们想通过调用 str.toUtf8().constData()
或 str.toUtf8().data()
将QString
转换为char *
,但这会导致程序崩溃或抛出异常,例如 XCode 上的EXC_BAD_ACCESS
,这是答案:
- 在像
foo = str.toUtf8().constData();
这样的调用中,str.toUtf8()
部分创建一个 temp 对象。 - 对
tmp.data()
或tmp.constData()
的调用返回指向由对象本身管理的内部缓冲区的指针,该缓冲区的生存期与 temp 对象的生存期相关联。 源。 - 一旦该语句被执行,其地址被分配给
foo
的对象的生存期一旦被data()
或constData()
返回,就会结束。 - 因此,正如预期的那样,在该语句之后,
foo
将保存生存期结束的对象的地址,从而导致崩溃和异常。
相关文章:
- 当回溯以零开始时,如何调试崩溃
- 将 Qvector<uint8_t> 转换为 QString
- 内联映射初始化的动态atexit析构函数崩溃
- 执行函数时导致崩溃的变量
- 程序崩溃并显示"std::out_of_range"错误
- CoInitialize()在单独的线程上崩溃而不返回
- 使用调试/崩溃报告将应用程序部署到客户端
- 为什么所有C++编译器都会崩溃或挂起此代码
- 为什么lambda在clang上崩溃而不是在gcc上崩溃
- 为什么我的多线程作业队列崩溃
- ExtractIconEx:可以工作,但偶尔会崩溃
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口
- 为什么引用传递会导致此崩溃(C++)
- 试图创建流或fopen时程序崩溃
- 程序在访问shared_ptr持有QString时崩溃
- 在 QString toUtf8() 中崩溃
- 使用QString会在QMap::remove之后导致崩溃
- Qt4 C++:来自多个线程的QString变量使用崩溃
- QString分配崩溃
- QString::toStdString()在std::string析构函数上崩溃