Solved Is QSharedMemory supported on Android?
-
I am working on a mobile app, focusing on Android for now. I'm quite new to C++, Qt QML etc...
My latest challenge is to create a background service. I have been fighting this for a week, with little success.
My latest problem is in a test case I'm starting to build. I was hoping to communicate between process and app using QSharedMemory, but have had no luck on Android so far. I simply cannot create a buffer.My main.cpp is (trying to reduce it to a minimum for this question):
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QSharedMemory> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; QSharedMemory _sharedMemory("QtMemTest", nullptr); if (!_sharedMemory.create(sizeof(int), QSharedMemory::ReadWrite)) qDebug() << "Failed " << _sharedMemory.errorString(); else qDebug() << "Size " << QString::number(_sharedMemory.size()); return app.exec(); }
The QML doesn't really matter, it's the hello world template.
On Windows, I get:
Size "4096"
On Android:
main.cpp:17 (int main(int, char**)): Failed "QSharedMemory::create: unable to lock"Forget about communication between processes etc... I only have one process in this example, have tried different keys to no avail.
This really looks like a basic problem... Do I need a specific Android permission? Is this available in Android?
I am using Qt Creator 4.6.2, am targeting gcc 4.9, Qt 5.9.6 for Android armv7.As a side question, is this the simplest way to communicate between my two processes, knowing one of them is a service?
Thanks!FYI I have tried on Android devices running 6.0 and 8.0.
-
@gomgom said in Is QSharedMemory supported on Android?:
Out of curiosity, do you know if Qsharedmemory is supposed to work under Android?
Hi,
As far as I know, QSharedMemory does not work on Android and IOS.
If you look into the detailed description of QSharedMemory, only Windows and Unix is listed in the description.
And for those, Windows and Mac in my case, I can confirm they work. Haven‘t worked with QSharedMemory on Linux jet, but they are usually close enough to UNIX-conform that it should work.
-
FYI just tried with Qt 5.11.1, the problem is still there.
Also, when calling the QSharedMemory functions, I get a series of warnings as follows:
W libsharedExample.so: (null):0 ((null)): Unimplemented code.
For instance commenting in/out the line with the create call adds/removes 6 such warnings.
-
Hi and welcome to devnet,
Depending on what you want to do, intent like described in this blog post might be what you need.
-
@SGaist Hi, thanks for the welcome!
Thanks also for the lead, I will follow that post and give it a try.
Out of curiosity, do you know if Qsharedmemory is supposed to work under Android? Asking because I'm struggling a bit with Qt documentation. Qt being such a wide scope project, with already quite many versions behind it, it's hard for a newby to figure out which doc, post, article etc... is still relevant and prevails. So in essence where would you turn to to find this kind of info (short of looking at the code!).
Thanks. -
@gomgom Another follow up question: I am still trying to figure out what is implemented in Android. I tried another test case today, playing with QSystemSemaphore, and I still get these "unimplemented code" warnings.
Is there a way to turn functions on and off in Qt? When using pre-built Qt images (not building it yourself), are all features turned on (compiling under Windows)? -
@gomgom said in Is QSharedMemory supported on Android?:
Out of curiosity, do you know if Qsharedmemory is supposed to work under Android?
Hi,
As far as I know, QSharedMemory does not work on Android and IOS.
If you look into the detailed description of QSharedMemory, only Windows and Unix is listed in the description.
And for those, Windows and Mac in my case, I can confirm they work. Haven‘t worked with QSharedMemory on Linux jet, but they are usually close enough to UNIX-conform that it should work.
-
Thanks for the answers, it indeed looks like the QSharedMemory and QSystemSemaphore classes are not available in Android.
I guess I should have trusted theUnimplemented code
warnings :-)It would just be good if the Qt class docs were to mention the platform on which each class is implemented! I realize the scope of the work but this could be phased in... In the "since" fields, maybe the supported platforms could be indicated in parenthesis next to the revision number?
Anyway, thanks for the answers.
-
@gomgom said in Is QSharedMemory supported on Android?:
It would just be good if the Qt class docs were to mention the platform on which each class is implemented! I realize the scope of the work but this could be phased in... In the "since" fields, maybe the supported platforms could be indicated in parenthesis next to the revision number?
Well, there is this page:
http://doc.qt.io/qt-5/qtmodules.html
that targets the Qt Moduls and the platforms they support or don't. It doesn't help in your case, as QSharedMemory is part of the QtCore modul x) -
@J.Hilk Nice, thanks! It's a good start, I had not paid attention to the platform column on that page.
Trying to mark you first post above as the correct answer, but I don't get this option. Is this not like on stackoverflow, doesn't the asker get to pick the correct answer? Right now I only get this option for my own posts.
-
Sadly, that's a defect in nodebb.
Just in case, classes that are not available on one or more platforms are usually annotated for that like QSharedMemory is. If not, then it's a bug in the documentation.