Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[qt5cpp] Fix crash when API return a map container #7933

Merged
merged 2 commits into from
Apr 2, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ void
QJsonObject obj = doc.object();

foreach(QString key, obj.keys()) {
qint32* val;
Copy link
Contributor

@etherealjoy etherealjoy Mar 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be {{returnBaseType}}

Copy link
Contributor

@etherealjoy etherealjoy Mar 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not part of this PR, but in case the {{returnBaseType}} is dynamically allocated (Object, QString,QDateTime,QbyteArray ... etc ), it has to be wrapped with QObjectWrapper for later deletion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you tell me more about QObjectWrapper? I never used it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, it is a template class which holds the pointer to the memory allocated for dynamically allocated types like QString, Object, QByteArray etc.., for the parameter to be passed in the slot callback. In this file you can find it being used, so once the signal is emitted and the slot activated, the deleteLater ensures the object is deleted after the process event has finished.
Present in the template QObjectWrapper.mustache

auto objwrapper = new {{prefix}}QObjectWrapper<{{{returnBaseType}}}*> (o);
objwrapper->deleteLater();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like I mentioned, it is not part of the crash, but simply to avoid memory leaks for complex types. If you have the time it is a good thing to add, to remove memory leaks. But for the qt5cpp petstore example the cleanup is not working because the loop is exited on receiving the callback. In a real qt application the event loop would still be running and the object deleted.

qint32 val;
setValue(&val, obj[key], "{{returnBaseType}}", QString());
output->insert(key, *val);
output->insert(key, val);
}
{{/isMapContainer}}
{{^isMapContainer}}
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/qt5cpp/client/SWGStoreApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ SWGStoreApi::getInventoryCallback(SWGHttpRequestWorker * worker) {
QJsonObject obj = doc.object();

foreach(QString key, obj.keys()) {
qint32* val;
qint32 val;
setValue(&val, obj[key], "qint32", QString());
output->insert(key, *val);
output->insert(key, val);
}
worker->deleteLater();

Expand Down