QWebEngineView crashes on load() or page() method - qt

I'm working on porting a Qt 5.5, QWebView project to Qt 5.6 (beta), QWebEngine. I've gone through the porting guide here. My code looks like this:
.h file defines _view like so:
QWebEngineView* _view;
and the .cpp constructor (class inherits from QWidget) has:
QVBoxLayout* vbox = new QVBoxLayout(this);
_view = new QWebEngineView(this);
connect(_view, SIGNAL(loadFinished(bool)), this, SLOT(loadPageFinished(bool)));
QString webDir = getReportBasePath() + no_tr("/index.html#") + startingPage;
// QWebEnginePage *page = _view->page(); // <-- CRASH!!
_view->load( QUrl("file:///" + webDir ) ); // <-- CRASH!!
_view->show();
vbox->addWidget(_view);
Upon doing either the page() or load() method, the whole thing crashes with:
Unhandled exception at 0x67019C66 (Qt5Cored.dll) in qxs.exe: 0xC0000005:
Access violation reading location 0x00000000.
I've verified that the _view pointer is not null.
If you look at the documentation, they have an example here that's almost identical to my code above. I've also tried replacing load() call to be identical to theirs:
view->load(QUrl("http://qt-project.org/"));
and it still crashes. Any ideas what could be causing these crashes?
Do I need to create a QWebEnginePage first and setPage() on the QWebEngineView? (I'm assuming not...) Could it have something to do with the Qt binaries (pre-built for Windows 32-bit MSVC 2013) that I'm using?
Relevant part of stack trace:
Qt5WebEngineWidgetsd.dll!QWebEnginePagePrivate::QWebEnginePagePrivate(QWebEngineProfile * _profile) Line 95 C++
Qt5WebEngineWidgetsd.dll!QWebEnginePage::QWebEnginePage(QObject * parent) Line 393 C++
Qt5WebEngineWidgetsd.dll!QWebEngineView::page() Line 145 C++
Qt5WebEngineWidgetsd.dll!QWebEngineView::load(const QUrl & url) Line 157 C++
qxs.exe!ReportWidget::ReportWidget(QcaMain * qm, QWidget * parent, QString startingPage) Line 321 C++
It crashes here:
QWebEnginePagePrivate::QWebEnginePagePrivate(QWebEngineProfile *_profile)
: adapter(new WebContentsAdapter)
, history(new QWebEngineHistory(new QWebEngineHistoryPrivate(this)))
, profile(_profile ? _profile : QWebEngineProfile::defaultProfile())
, settings(new QWebEngineSettings(profile->settings()))
, view(0)
, isLoading(false)
, scriptCollection(new QWebEngineScriptCollectionPrivate(browserContextAdapter()->userScriptController(), adapter.data()))
, m_backgroundColor(Qt::white)
, fullscreenMode(false)
{
memset(actions, 0, sizeof(actions));
}
I thought maybe that had something to do with _profile being NULL, so I attempted to first set a QWebEngineProfile like so:
QWebEnginePage* page = new QWebEnginePage( QWebEngineProfile::defaultProfile(), _view );
_view->setPage( page );
Then it instead crashes in qwebengineprofile.cpp here:
static QWebEngineProfile* profile = new QWebEngineProfile(
new QWebEngineProfilePrivate(BrowserContextAdapter::defaultContext()),
BrowserContextAdapter::globalQObjectRoot());
with stack trace:
Qt5Cored.dll!convert_to_wchar_t_elided(wchar_t * d, unsigned int space, const char * s) Line 256 C++
Qt5Cored.dll!qt_message_fatal(QtMsgType __formal, const QMessageLogContext & context, const QString & message) Line 1593 C++
Qt5Cored.dll!QMessageLogger::fatal(const char * msg, ...) Line 784 C++
Qt5WebEngineCored.dll!`anonymous namespace'::subProcessPath(void) C++
Qt5WebEngineCored.dll!WebEngineLibraryInfo::getPath(int) C++
Qt5WebEngineCored.dll!WebEngineContext::WebEngineContext(void) C++
Qt5WebEngineCored.dll!WebEngineContext::current(void) C++
Qt5WebEngineCored.dll!QtWebEngineCore::BrowserContextAdapter::defaultContext(void) C++
> Qt5WebEngineWidgetsd.dll!QWebEngineProfile::defaultProfile() Line 516 C++

Problem solved. I was missing some key files required by QWebEngine, otherwise it'll crash. These files must be in the same directory as the executable. They are put there by the windeployqt.exe tool, so that's the best way to ensure your Qt application has everything it needs to run without crashing.
qtwebengine_resources.pak
qtwebengine_resources_100p
qtwebengine_resources_200p.pak.pak
QtWebEngineProcess.exe
icudtl.dat
The reason this got me was our development group was formerly using Qt 4.8, and uses an in-house method to copy the required Qt dlls and whatnot into the target directory. When upgrading to Qt 5.x and adding QWebEngine, we didn't realize the files above were required.

You can set Windows environment variable called QTWEBENGINEPROCESS_PATH
Example:
QTWEBENGINEPROCESS_PATH C:\Qt\Qt5.6.0\5.6\msvc2013_64\bin\QtWebEngineProcess.exe
With this solution, no need to copy resources files into your projects output folders

Related

QPixmap::load() crash - trying to avoid it

I am working on a project, using Qt 4.8.3 for an ARM platform. In my project, I use QGraphicsItems... one of which is a subclass of QGraphicsPixmapItem.
The code was tested with a 32 bit bitmap image - and it crashes.
The crash occurs not just when running on the ARM, but also in QVFB.
QPixmap p;
if (!p.load(filename)) // crashes here
return false;
I have tried to surround this with a try...catch, but it did not help.
I seem unable to step in the Qt code for this version... but the crash occurs inside QImageReader::read(QImage*).
The stack trace:
QImageReader::read(QImage*)
QImageReader::read()
QPixmapData::fromFile(QString const&*, QFlags<QT::ImageConversionFlag>)
QPixmap::load(QString const&, char const*, QFlags<QT::ImageConversionFlag>)
QPixmapItem::loadItemFromFile // mine, the code above
Any other type of image works... and the same 32 bit bitmap loads properly in windows, same Qt version. It fails to load (returning false) in the same Qt version, for Desktop.
I would be happy to exclude this type of file - but I don't know how.
Is there any way to check for the image type without loading the image and avoiding the crash ?
Is there a way to perhaps load the image header only, and verify its type ?
Since you want to exclude 32-bit BMP images, you have to read a BMP header. First two bytes are the characters "BM" and bytes 28, 29 contain bits per pixel.
Here is a small example where we read a file into QByteArray, check its format and load it to QPixmap if OK.
#include <QtCore>
#include <QtGui>
int main(int argc,char** argv)
{
QApplication app(argc,argv);
QFile file("./plot.bmp");
if (!file.open(QIODevice::ReadOnly)) return 1;
QByteArray ba=file.readAll();
if(ba[0]=='B' && ba[1]=='M' && ba[28] == 32) {
qDebug() << "Wrong format!";
return 1;
}
QPixmap pixmap;
pixmap.loadFromData(ba);
qDebug()<<"OK!";
return 0;
}
Or if you don't want to read everything into memory, you can open a file using QFile, ifstream etc, check these bytes and then close it.

Declaring variables in header file of QT causes program to unexpectedly finish

I have a project "Cybaware_ver1" on QtCreator based on QWidget and everything was working fine. I have a header file with a class containing public and private variables and functions. However, since one day whenever I try to declare a new variable in the header file, even an "int" variable, and run the project, I get
The program has unexpectedly finished.
/home/devdeep/Cybaware_ver1-build-Desktop_Qt_5_0_0_GCC_64bit_SDK-Debug/Cybaware_ver1 exited with code 0
Previously I could add variables in the header file without any problem. Currently, I am able to add new functions, modify UI etc. Just I am unable to add variables to the header file. I have found a temporary solution of declaring these variables as static in the cpp file. However, I would like to find a solution to this.
I have tried re-installing Qt but the problem still persists. I am running it on Ubuntu Linux.
Please let me know if there is a way to fix this. Also, I am not sure what other information I can provide. So please let me know about that.
UPDATE: OK. I used the debugger at it says that the error is a segmentation fault.
It points to the following section of qdebug.h and to the line marked by qt_message_output.
public:
inline QDebug(QIODevice *device) : stream(new Stream(device)) {}
inline QDebug(QString *string) : stream(new Stream(string)) {}
inline QDebug(QtMsgType t) : stream(new Stream(t)) {}
inline QDebug(const QDebug &o):stream(o.stream) { ++stream->ref; }
inline QDebug &operator=(const QDebug &other);
inline ~QDebug() {
if (!--stream->ref) {
if(stream->message_output) {
QT_TRY {
qt_message_output(stream->type,
stream->context,
stream->buffer);
} QT_CATCH(std::bad_alloc&) { /* We're out of memory - give up. */ }
}
delete stream;
}
}
I guess it is a out of memory error. Possible, closing other programs might help?
UPDATE2: Calling "run qmake" did not help. Here is my header file:
#ifndef MAINVIEW_H
#define MAINVIEW_H
#include <QWidget>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QGraphicsLineItem>
#include <QGraphicsEllipseItem>
#include <QGraphicsSimpleTextItem>
#include <list>
#include <QMouseEvent>
#include <QEvent>
#include <QString>
#include <boost/thread.hpp>
#include <qcustomplot.h>
#include <QTimer>
namespace Ui {
class MainView;
}
class MainView : public QWidget
{
Q_OBJECT
public:
int num_missions;
int num_services;
int timer;
int* num_servpermission;
int missionstartx;
//boost::thread pt;
QGraphicsScene mynewscene;
QGraphicsRectItem* myrect;
QGraphicsRectItem** missionline;
QGraphicsLineItem** missionplayline;
QGraphicsRectItem** playrect;
std::list<QGraphicsLineItem**> missionticks;
std::list<QGraphicsEllipseItem**> missionservices;
std::list<QGraphicsSimpleTextItem**> missionservicesno;
std::list<QGraphicsLineItem**>::iterator ti;
std::list<QGraphicsSimpleTextItem**>::iterator ssi;
//std::list<QGraphicsEllipseItem**>::iterator si;
int* missionlength;
int** missionplayxy;
int* missiontickscount;
int* missioniteration;
int missionselected;
int missiontickselected;
int missiontickoffset;
bool isrepeated;
int temp;
QCustomPlot** customPlot;
QTimer dataTimer;
explicit MainView(QWidget *parent = 0);
void playthread();
bool eventFilter(QObject*, QEvent*);
void testfunc();
void clearview();
void test1();
~MainView();
private slots:
void on_startButton_clicked();
void realtimeDataPlot();
void on_pushButton_clicked();
private:
Ui::MainView *ui;
};
#endif // MAINVIEW_H
The program crashes on adding any variable. For example, I added the variable int temp just now and it would crash. Adding anything new makes it crash such as uncommenting the declaration std::list::iterator si;. I take it out and everything works fine.
UPDATE3: My external header file qcustomplot.h is located in the same directory. I am also using boost but that I set up using apt-get. Here is the stack trace:
0 MainView::playthread mainview.cpp 530 0x418fa1
1 boost::_mfi::mf0::operator() mem_fn_template.hpp 49 0x424690
2 boost::_bi::list1 >::operator(), boost::_bi::list0> bind.hpp 253 0x424600
3 boost::_bi::bind_t, boost::_bi::list1 > >::operator() bind_template.hpp 20 0x4245af
4 boost::detail::thread_data, boost::_bi::list1 > > >::run thread.hpp 61 0x42431c
5 thread_proxy /usr/lib/libboost_thread.so.1.46.1 0x7ffff7bcfba9
6 start_thread /lib/x86_64-linux-gnu/libpthread.so.0 0x7ffff6557efc
7 clone /lib/x86_64-linux-gnu/libc.so.6 0x7ffff5af159d
8 ??
The 0th pt in the above trace points to
ui->missionview->invalidateScene()
Here the graphics view gets updated from a function playthread binded to a boost thread. But I am not sure why this would be a problem on adding a variable in the header file as I am not using the variable anywhere in the cpp file.
Go into the project folder and delete the *.pro.user file. Next time you open the project you'll just need to reconfigure it (i.e. choose the appropriate build kit).
I know this is an old thread, but I ran into the same problem myself. Every time I tried to add a variable to my header file it was causing a heap corruption error. I wasn't allocating memory on the heap, or even declaring pointer variables for that matter. It was very frustrating because I didn't know what it was at first. I finally narrowed it down to adding variables to my header file causing the crash. Based on doomguy's experience I knew that it was something persistent in the build folder because he said reinstalling Qt didn't work. I'm glad I read that because I would have been pissed if I reinstalled this behemoth and it still didn't work. Based on prior experience corruption in the .user file can cause issues, and deleting it kinda gives you a clean slate. Moral of the story: If you're getting unusual behavior that seems to point to a problem in the IDE try deleting the .user file before trying more extreme measures.
Not sure if the problem is cause by this, but if the header contains a QObject derived class you should run qmake and after that build your application.
LE: also if this isn't the case, i suggest that you show us a little more code (your code, not Qt code)

Qt Mkdir with a environment variable

QDir *temp = new QDir("%USERPROFILE%");
bool ok = temp->mkdir("abc");
and it does not work.
For portable Qt code, use static method QProcessEnvironment::systemEnvironment():
QProcessEnvironment env(QProcessEnvironment::systemEnvironment());
QDir *temp = new QDir(env.value("USERPROFILE")); // returns empty string for unset variable
bool ok = temp->mkdir("abc");
Alternative, as suggested in a comment of the actual question, you can also use
#include <cstdio>
QString envValue(QString::fromLocal8bit(::getenv("USERPROFILE"))); // standard, yet deprecated in Windows...
I'd prefer using the first alternative, letting Qt take care of character encoding stuff. If you want to use non-Qt functions, I think in Windows you should use getenv_s or _wgetenv_s to do this "properly".

force call to QString(const char *) constructor in Qt 4.7

I am trying to compile a library written in Qt 4.6. On my current Linux machine I have only Qt 4.7 installed. The following code part:
/*file try.h*/
void fileOpen(QString s = NULL) ;
/*file try.cpp*/
void MainWindow::fileOpen(QString s) {
QString filename ;
if(s.isNull()) filename = QFileDialog::getOpenFileName(
this,
"Choose a file",
".",
"Source file (*.)");
else filename = s ;
}
compiles with the following error (I used cmake but the corresponding line code is the one listed above):
In member function ‘virtual int MainWindow::qt_metacall(QMetaObject::Call, int,
void**)’:
/homes/combi/hodorog/Developments/axelOld/build/axel/src/QGui/moc_MainWindow.cxx:141:26:
error: conversion from ‘long int’ to ‘QString’ is ambiguous
/homes/combi/hodorog/Developments/axelOld/build/axel/src/QGui/moc_MainWindow.cxx:141:26:
note: candidates are:
/usr/include/QtCore/qstring.h:426:43: note: QString::QString(const char*)
/usr/include/QtCore/qstring.h:105:14: note: QString::QString(const QChar*)
So I am guessing the problem is that in qt. 4.7. there are two QString constructors that can take a pointer as an argument (as listed in the compilation error), whereas in qt 4.6. there is only one QString constructor that can take a pointer as an argument. How can I force QString to call the constructor with const char * as an argument?
Thank a lot for your help in advance,
madalina
void fileOpen(QString s = NULL);
You are trying to construct a QString object with 0. It seems you are confusing the null of pointers with a null QString. A null QString is one which is created with the constructor QString(). Given how your function is implemented (referring to s.isNull()), you should change the function declaration to
void fileOpen(QString s = QString());

Why does my program halt when calling front() on a std::queue?

I want to use the Irrnet network library in an Irrlicht game.
The source code uses Linux sockets and I'm trying to port it for Windows replacing it with code that uses Windows' Winsock2.
The library compiles successfully but when I try to run the Quake example it crashes. I located the line at which the program stops but i can't figure out how to solve the problem.
The program stops at the second call of the function getNextItem
class NetworkType {
public :
NetworkType();
~NetworkType();
template<class T>
void getNextItem(irr::core::vector3d<T>& data);
private:
typedef std::queue<std::string> Container;
Container items;
};
template<class T>
void NetworkType::getNextItem(irr::core::vector3d<T>& data) {
T X, Y, Z;
std::istringstream item(items.front());
// the program does not get here the second time it calls this function
items.pop();
item >> X;
item >> Y;
item >> Z;
data = irr::core::vector3d<T>(X, Y, Z);
}
and exactly at this line
std::istringstream item(items.front());
Can anyone tell me why does the program stop the second time it gets to this line ?
here is the link for the complete source code
I assume by "stops" you mean "crashes" in some fashion? Likely causes for a crash on the line in question are:
The NetworkType instance that is invoking the getNextItem() method is garbage (the this pointer is garbage or null). This could happen due to bad pointer math elsewhere, a premature delete or destruction of the instance, et cetera. This would manifest as a fault when the program attempted to access the items member.
The items container is empty. In these cases the return value of front() is undefined (since it is a reference) and the constructor for istringstream may be crashing. front() itself may be raising a debug/runtime check error as well depending on your compiler and its configuration.
Actually you might have a runtime error on this one if the dequeue is empty: MSDN deque
So just check the deque isn't empty before you try to pop a value from it.
if(items.size()>0)
{
//do things
}
else
{
//error deque empty
}
[edit] confounded std and (I guess) MSDN ( OP doesn't say) lib.

Resources