<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Unable to read data from share memory]]></title><description><![CDATA[<p dir="auto">Hii all,<br />
I am try to read data from share memory and it is showing<br />
"Unable to attach to shared memory segment: "QSharedMemory::handle:: UNIX key file doesn't exist" "<br />
hear is my source code for read data from share memory```<br />
code_text</p>
<pre><code class="language-#include">#include &lt;QSharedMemory&gt;
#include &lt;QDebug&gt;

int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);

    // Name of the shared memory segment
    QString shmKey = "my_memory";

    // Create a shared memory object
    QSharedMemory sharedMemory(shmKey);

    // Attach to the shared memory segment
    if (!sharedMemory.attach()) {
        qDebug() &lt;&lt; "Unable to attach to shared memory segment:" &lt;&lt; sharedMemory.errorString();
        return 1;
    }

    // Read data from the shared memory
    char *from = static_cast&lt;char*&gt;(sharedMemory.data());
    QString message = QString::fromUtf8(from);

    qDebug() &lt;&lt; "Data read from shared memory:" &lt;&lt; message;

    return a.exec();
}</code></pre>
]]></description><link>https://forum.qt.io/topic/158303/unable-to-read-data-from-share-memory</link><generator>RSS for Node</generator><lastBuildDate>Wed, 10 Jun 2026 02:33:26 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/158303.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 20 Aug 2024 05:14:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Unable to read data from share memory on Wed, 21 Aug 2024 08:40:31 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/biswa">@<bdi>Biswa</bdi></a> Seems this <code>posix_ipc</code> and <code>QSharedMemory</code> both use <code>shm_open</code> API under POSIX, I suppose this could work.<br />
As I said, here we need to let <code>QSharedMemory</code> use "/my_memory123" as the native API key (to call shm_open).<br />
So instead of creating a <code>QSharedMemory</code> with key directly, try to use</p>
<pre><code>QSharedMemory sharedMemory;
sharedMemory.setNativeKey("/my_memory123");
</code></pre>
<p dir="auto">P.S. There's some helpful information in Qt's documentation about this kind of case.<br />
For Qt5, read the Warning part of <a href="https://doc.qt.io/qt-5/qsharedmemory.html#details" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/qsharedmemory.html#details</a><br />
For Qt6, read <a href="https://doc.qt.io/qt-6/native-ipc-keys.html#interoperability-with-non-qt-applications" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/native-ipc-keys.html#interoperability-with-non-qt-applications</a></p>
]]></description><link>https://forum.qt.io/post/807747</link><guid isPermaLink="true">https://forum.qt.io/post/807747</guid><dc:creator><![CDATA[Bonnie]]></dc:creator><pubDate>Wed, 21 Aug 2024 08:40:31 GMT</pubDate></item><item><title><![CDATA[Reply to Unable to read data from share memory on Wed, 21 Aug 2024 07:36:28 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/bonnie">@<bdi>Bonnie</bdi></a> <a class="plugin-mentions-user plugin-mentions-a" href="/user/bonnie">@<bdi>Bonnie</bdi></a><br />
for writing data in shared memory am using pythoncode</p>
<pre><code>import posix_ipc
import mmap
import os
import struct

SHARED_MEMORY_NAME = "/my_memory123"
MEMORY_SIZE = 1024

# Create or open shared memory segment
shared_memory = posix_ipc.SharedMemory(SHARED_MEMORY_NAME, posix_ipc.O_CREAT, size=MEMORY_SIZE)

# Map the shared memory segment to memory
mapfile = mmap.mmap(shared_memory.fd, MEMORY_SIZE)

# Example integers to write
integers = [0x02, 0x03, 0x04, 0x05]  # List of integers
# Convert integers to bytes
data = struct.pack(f'{len(integers)}I', *integers)

# Write integer data to shared memory
mapfile.seek(0)  # Move to the start of the shared memory segment
mapfile.write(data)

print("Integer data written to shared memory.")

# Clean up
mapfile.close()
os.close(shared_memory.fd)
</code></pre>
]]></description><link>https://forum.qt.io/post/807745</link><guid isPermaLink="true">https://forum.qt.io/post/807745</guid><dc:creator><![CDATA[Biswa]]></dc:creator><pubDate>Wed, 21 Aug 2024 07:36:28 GMT</pubDate></item><item><title><![CDATA[Reply to Unable to read data from share memory on Wed, 21 Aug 2024 06:33:08 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/biswa">@<bdi>Biswa</bdi></a> Then how do you create shared memory in python? Can you share the code? Are you sure they are using the same mechanism?<br />
One thing need to be noticed is that, when you create a QSharedMemory object by "QSharedMemory sharedMemory(shmKey);", shmKey will not be the actual key used by the native API.</p>
]]></description><link>https://forum.qt.io/post/807738</link><guid isPermaLink="true">https://forum.qt.io/post/807738</guid><dc:creator><![CDATA[Bonnie]]></dc:creator><pubDate>Wed, 21 Aug 2024 06:33:08 GMT</pubDate></item><item><title><![CDATA[Reply to Unable to read data from share memory on Wed, 21 Aug 2024 06:19:17 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a><br />
I want to  write data in Shared Memory using python language and that data i want to read from Shared Memory.</p>
]]></description><link>https://forum.qt.io/post/807735</link><guid isPermaLink="true">https://forum.qt.io/post/807735</guid><dc:creator><![CDATA[Biswa]]></dc:creator><pubDate>Wed, 21 Aug 2024 06:19:17 GMT</pubDate></item><item><title><![CDATA[Reply to Unable to read data from share memory on Tue, 20 Aug 2024 06:39:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/biswa">@<bdi>Biswa</bdi></a><br />
As <a href="https://doc.qt.io/qt-6/qsharedmemory.html#details" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/qsharedmemory.html#details</a> says:</p>
<blockquote>
<p dir="auto">One QSharedMemory object must create() the segment and this call specifies the size of the segment. All other processes simply attach() to the segment that must already exist.</p>
</blockquote>
<p dir="auto">So where have you, either in this process or another process, called <a href="https://doc.qt.io/qt-6/qsharedmemory.html#create" target="_blank" rel="noopener noreferrer nofollow ugc">bool QSharedMemory::create(qsizetype size, QSharedMemory::AccessMode mode = ReadWrite)</a>?  I imagine that is what error message <code>UNIX key file doesn't exist</code> indicates.  Tell me, does the message really include the word <code>UNIX</code> in it??</p>
]]></description><link>https://forum.qt.io/post/807655</link><guid isPermaLink="true">https://forum.qt.io/post/807655</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 20 Aug 2024 06:39:55 GMT</pubDate></item></channel></rss>