Results 1 to 3 of 3

Thread: QT shared memory

  1. #1
    Join Date
    Jan 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QT shared memory

    iam a begginer to QT,... can somebody tell me how to create a shared memory in Qt,
    pls give me some sample coding..
    i am using Fedora and QT4.5
    thanks in advance
    Last edited by sweetsubha2020; 16th January 2009 at 05:29.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QT shared memory

    Please launch Qt Assistant from your Qt installation and type "shared memory" (or just "shared") to the index tab.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: QT shared memory

    My project has the following thing,
    ---->Network connection
    ---->Retreive data from server and store it in shared memory in client side

    Network connection is working good.
    regarding shared memory, i retreive data from server and store it in Buffer1 in client side and

    from buffer1 it is then saved into shared memory so that it can be used by other processes.Later

    client application reads the data with the help of buffer2.

    It goes like this


    server<------------------>client
    (buffer1)--sharedmemory--(buffer2)


    First i read the data from server into a char array with the help of read()

    client.read(buffer,client.bytesAvailable());

    and then i write it to buffer1 with write()

    qbuffer.write(buffer);

    Next i have created a shared memory and wrote that data into shared memory.

    My problem is :
    >>>>i get some garbage values from both buffer1 and shared memory.i dont know
    how to allocate memory dynamically without garbage value
    >>>>Also when i read data from buffer2 i get segmentation error.

    //writing data from shared memory to buffer2
    qbuffer1.setData((char*)sharedMemory.constData(), sharedMemory.size());

    //reading data from buffer2
    const char *cha=qbuffer1.data().data();

    i have attached my code with this pls do help me....


    Qt Code:
    1. #include "client.h"
    2. #include <QHostAddress>
    3. #include <iostream>
    4. #include<QString>
    5. #include<QTimer>
    6. #include <QSharedMemory>
    7. #include<QChar>
    8. using namespace std;
    9. Client::Client(QObject* parent)
    10. {
    11. connect(&client, SIGNAL(connected()),this, SLOT(startTransfer()));
    12. connect(&client, SIGNAL(readyRead()),this, SLOT(startRead()));
    13. connect(&client, SIGNAL(disconnected()),this,SLOT(serverdisconnected()));
    14. }
    15.  
    16. void Client::start(QString address, quint16 port)
    17. {
    18. QHostAddress addr(address);
    19. client.connectToHost(addr, port);
    20. }
    21. void Client::startRead()
    22. {
    23. //READING DATA INTO CHARACTER ARRAY
    24. char buffer[1024] = {0};
    25. client.read(buffer,client.bytesAvailable());
    26. cout <<"server:"<< buffer << endl;
    27. qint64 si=112;
    28. //WRITING IT INTO BUFFER1
    29. QBuffer qbuffer;
    30. char ch[]="";
    31. qbuffer.open(QBuffer::ReadWrite);
    32. qbuffer.write(buffer);
    33. qbuffer.seek(0);
    34. qbuffer.read(ch,si);
    35. cout<<ch<<"\n";
    36.  
    37. int size = qbuffer.size();
    38. //SHARED MEMORY
    39. QSharedMemory sharedMemory;
    40. sharedMemory.setKey ("552");
    41. if(!sharedMemory.create(size))
    42. {
    43. cout<<"Unable to create shared memory segment.";
    44. exit(0);
    45. }
    46. if (sharedMemory.isAttached())
    47. cout<<"\nAttached.......\n";
    48. cout<<"shared mem segment key :"<<sharedMemory.key().toStdString();
    49.  
    50. //WRITING INTO SHARED MEMORY
    51. sharedMemory.lock();
    52. char *to = (char*)sharedMemory.data();
    53. const char *from = qbuffer.data().data();
    54. memcpy(to, from, qMin(sharedMemory.size(), size));
    55. sharedMemory.unlock();
    56.  
    57.  
    58. QBuffer qbuffer1;
    59. qbuffer1.open(QBuffer::ReadWrite);
    60. //WRITING DATA INTO BUFFER2
    61. sharedMemory.lock();
    62. qbuffer1.setData((char*)sharedMemory.constData(), sharedMemory.size());
    63. sharedMemory.unlock();
    64. sharedMemory.detach();
    65. const char *cha=qbuffer1.data().data() ;
    66. cout<<"\nread data:"<<cha<<endl;
    67. }
    68. void Client::startTransfer()
    69. {
    70. cout<<"connected\n";
    71. QString str="hi";
    72. string str1;
    73. cout<<"client:";
    74. cin>>str1;
    75. str=QString::fromStdString(str1);
    76. client.write(qPrintable(str));
    77. }
    78. void Client::serverdisconnected()
    79. {
    80. cout<<"\n--------------Server disconnected!!!! try again-------------------\n";
    81. client.close();
    82. exit(0);
    83. }
    To copy to clipboard, switch view to plain text mode 
    thanks in advance. pls do help me, it is urgent
    Last edited by wysota; 18th January 2009 at 11:11. Reason: missing [code] tags

Similar Threads

  1. Memory debugging in windows
    By txandi in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2009, 13:45
  2. Replies: 4
    Last Post: 29th December 2008, 19:50
  3. How to clear shared memory?
    By THRESHE in forum Qt Programming
    Replies: 4
    Last Post: 22nd May 2008, 18:28
  4. Memory leak weirdness
    By Darhuuk in forum General Programming
    Replies: 10
    Last Post: 10th January 2008, 18:51
  5. saving a c string of variable length in a shared memory?
    By nass in forum General Programming
    Replies: 4
    Last Post: 3rd January 2007, 14:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.