SEGFAULT when passing QString by refernce to library function - qt

I've been fighting with this all day long and I've tried passing a QString, a std::string, and a char* in many many different fashions, but if I pass it so that I can modify the parameter's value inside the library function then it SEGFAULTs. If I copy the library function, line for line, into the main app, I can pass references all day long as params and change their values inside the functions.
Here is the stripped down version of my function inside the library.
I have literally removed all code except for this line.
MySQLLib::ExecuteQuery(const QString& query, QString& results)
{
results = "Changed the value of this parameter.";
}
Here is the calling code from the main application.
bmdbTest is an instance of the above MySQLLib class...
All the other code in my library and application works. It just won't let me pass references to ANYTHING to my library.
MySQLProj::pbExecuteQuery_Click()
{
QString x = "Hello.";
bmdbTest->ExecuteQuery("SELECT ttid from test_table", x);
ui_MySQLProj1.textEdit->setText(x);
}
It SEGFAULTs on the bmdbTest->ExecuteQuery call.
I've even tried a simple int& as a parameter with no success.
I can however pass params as const QString& without issue. I just can't modify the param's value that way.
EDIT: I just figured it out. Thank you to "paxdiablo" for suggesting I check my variables for null or invalid pointers. I was really tired last night and I can't believe I missed this.
I just found the problem and I feel like a complete idiot. You mentioned about bmdbTest being null or invalid. The value of bmdbTest was fine as all my other functions worked fine, but when I was calling ExecuteQuery() I was passing the query string from the value in a QLineEdit from my GUI window like this.
bmdbTest->ExecuteQuery(leQuery->text(), resultString);
The leQuery->Text() was actually the problem as I must access leQuery like this.
bmdbTest->ExecuteQuery(ui_MySQLProj1.leQuery->text(), resultString);

You may want to check the value of bmdbTest itself. It may be null or an invalid pointer.
That seems to be indicated by the fact it's faulting on that line. If there were something suspect about the parameters, I would expect it to fault within the ExecuteQuery function.
You should be able to find out exactly where the crash is by putting suitable debug statements (with flushing) on either side of the results = ... and bmdbTest->ExecuteQuery(...) lines (or use a debugger if you have one).

Related

Is it possible to get a return value from a QRemoteObject Dynamic Replica slot?

I am unable to call a slot returning a value on a QRemoteObjectDynamicReplica.
It seems that InvokeMethod on Replica doesn't support return value.
I have only succeeded in calling void returning slots and even in this case, in DirectConnection mode, the invokeMethod finished before host slot invocation, so it seems that no host answer is in waiting.
I have a code like this which works perfectly on Host side, but not on Replica Side.
bool success = QMetaObject::invokeMethod(_replica,"getName", Qt::DirectConnection,
Q_RETURN_ARG(QString, retVal),
Q_ARG(QString, "id")
);
If i understand well the topic of REPC (i haven't try it yet), it seems that the calling of returning value slots is possible:
Usage is to declare SLOT followed by the desired signature wrapped in parentheses. The return value can be included in the declaration. If the return value is skipped, void will be used in the generated files.
Do REPC do some kind of magic to allow this feature, or did i miss something ?
Thanks for help.
For those who are looking for an answer on this, there is a way :) : the
QRemoteObjectPendingCall
undocumented argument.
bool success = QMetaObject::invokeMethod(_replica,"getName",Qt::DirectConnection,
Q_RETURN_ARG(QRemoteObjectPendingCall, call),
Q_ARG(QString, "id")
);
auto e = call.error();// , QRemoteObjectPendingCall::InvalidMessage);
call.waitForFinished();
//QVERIFY(call.isFinished());
qDebug() << QMetaType::typeName(call.returnValue().type());
QString retVal = call.returnValue().toString();
This is exactly the same kind a future object available for REPC Replica (except not templated)
No Documentation but there is some example in :
Qt Remote Objects integration tests
Sadly, there is currently (5.13.0) no way to get the pending reply in QML (QTBUG-77178) but Qt People are looking for it.

Meteor local package array returns empty on method call

I have a local package with the following code
#articleSubmitMethodCallbacks = []
articleSubmitMethodCallbacks.push(addThumbnailOnSubmit)
This works and returns an array with a function in.
Then I have a method called articleInsert
In that method I have the following code
article = articleSubmitMethodCallbacks.reduce(((result,currentFunction)->
return currentFunction (result)
), article)
Now for some reason, every time I call this method, articleSubmitMethodCallbacks stays an empty array, even though before it ran it has the function in it. It somehow gets reset, any idea why this happens?
Okay strange answer but here it goes, apparently it has something to do with the naming articleSubmitMethodCallbacks that interferes with something inside Meteor.
If I use any other array name, it works perfect. To be clear I didn't overwrite articleSubmitMethodCallbacks anywhere and in fact the code above was the only reference to it in the whole project.

Vector of structures is causing file.exe to stop working

I'm trying to create a dynamic array of pointers to structure objects. I have done this before, but never really understood it, so I'm lost now that it's failing.
My code is:
struct object {
char* alias;
char* mapInfo;
char* binaryData;
};
class ATP
{
public:
ATP();
std::vector<std::shared_ptr<object>> objects;
};
This compiles fine, but when I try to run it it says
"ATPEditor.exe has stopped working. A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available."
I'm not trying to use anything like a push_back yet, I'm just trying to create the array.
Does anyone know why this is failing? Or if there is somewhere else I might have made a mistake?
EDIT: When copying the rest of my code into this window, I noticed that I had mistyped the constructor. Once I fixed it the problem went away. Thanks.

Error with sequence argument when using QtConcurrent map

I'm trying to use QtConcurrent::map to run this function
//This function is used through QtConcurrent::map to create images from a QString path
void MainWindow::createQImage(QString* path) {
//create an image from the given path
QImage* t = new QImage(*path);
imageList->append(t);
}
on this container/sequence (declared in the mainwindow header and initialized in the mainwindow constructor)
QList<QImage *> *imageList = new QList<QImage *>;
Here's the code I'm trying to run
QFutureWatcher<void> futureWatcher;
futureWatcher.setFuture(QtConcurrent::map(imageList, &MainWindow::createQImage));
and here are the errors I'm getting:
request for member 'begin' in 'sequence', which is of non-class type 'QList<QImage*>*'
request for member 'end' in 'sequence', which is of non-class type 'QList<QImage*>*'
I need the "createQImage" function to be run for every element in "imageList," which can reach into the thousands. I believe the problem to be with the first parameter to the map function. And from what I've read, it may have to do with compatibility. There isn't much sample code online that I was able to relate to. I'm new to Qt and not the most experienced of programmers but I'd appreciate some help and feedback.
Alternatively, is there better way to do this using QtConcurrent?
Thanks in advance!
QtConcurrent::map wants a sequence as its first argument. You passed it a pointer to a sequence.
If you do
futureWatcher.setFuture(QtConcurrent::map(*imageList, &MainWindow::createQImage));
it should be happy.
Note that the compiler was reasonably clear about what the problem was. Take the time to read the errors carefully, they're usually not as cryptic as they perhaps at first seem. In this case it was telling you that the argument you passed was not of a class type. A quick look at the argument type at the end of the error reveals that it is a pointer.
QList, QImage, QString are Copy-On-Write types (see other Qt implicitly shared types), so you shouldn't use pointers to these types because they are basically already smart pointers.
And if you remove all pointers from your code, it should also fix the main problem.

qt tr() in static variable

I have problem concerning translations in qt. All translations in my porject work fine, but one, which is in a static variable of a class. Corresponding part of code looks as follows
The header file is similar to this:
typedef struct {
int type;
QString problematicString;
} info;
MyClass::QObject_Descendant
{
Q_OBJECT;
//some functions like constructor, destructor... etc.
....
static info myClassInfo;//class that makes problems
}
and in implementation file I initialize the variable as follows:
info MyClass::myClassInfo={
1,
tr("something to be translated")
};
And whatever I do (trying with QT_TR_NOOP, then tr() and others) I cannot get myClassInfo.problematicString translated. The weirdest thing is that the text "something to be translated"
appears in *.ts file.
If someone has any hints, please share them with me. Thanks in advance.
Chris.
Static variables are instantiated (and thus, constructor code run) before your int main function is run. The translation code is set up in the QApplication constructor (I believe), which isn't run until your int main function has been entered. Thus, you are trying to get the translation of a string before the code to support it has been initialized.
To avoid this, you could either accept that the given string isn't translated and explicitly translate it every time it is used, or use the Construct on First Use idiom instead of a static member variable.

Resources