【发布时间】:2016-06-01 07:49:02
【问题描述】:
我有一个插槽:
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]
提前致谢!
【问题讨论】:
-
可能是内存碎片的问题?你可能总共有
2314 free MB,但是例如长度 -
顺便说一句,使用像
str2.toUtf8().constData()这样的结构是非常糟糕的做法,因为您正在返回指向临时对象内部的指针。所以str2.toUtf8().constData()将在BaseClass::someFunc调用后立即失效。因此,如果您将其传递到某个地方,则会遇到问题 -
好吧,我会检查内存碎片时刻并在此处写入结果。
-
如果将 str2 设为普通 QString 而不是 const 引用会怎样?会不会是作为 str2 传递的字符串同时在其他地方发生了更改?
-
我想过这个并且已经尝试过,但是如果我这样做,行为将是相同的。 (我只是忘了写这个)