Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. C++/Qt signals and dynamic memory operation

C++/Qt signals and dynamic memory operation

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 2 Posters 918 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tian_qhcn
    wrote on last edited by
    #1

    I am a new user in C++ object-oriented programming and Qt GUI framework. Now I got a very strange error for me, I hope someone can help one out.

    In the hearder of the main window I created a struct with three fields:

    struct structForDataReceiving {
        char *imageDataAll;
        int *index;
        qint64 *TimeStamp;
    }
    

    In the header file of the main window, I create an struct like this:

    structForDataReceiving ImageSpaceInMemory;
    

    Before receiving the data, in the main window I allocate memory like this:

    ImageSpaceInMemory.imageDataAll = new char [totalMemorySize];
    ImageSpaceInMemory.TStampSec = new qint64 [totalFrames * 2];
    ImageSpaceInMemory.imageIndex = new int [totalFrames];
    

    From an independent threads, there are two signals to send the data to the slots in the main window:

    void emitCameraFrameFinished(int number, int ki, char* pData, int XSize, int YSize, int Zsize);
    void emitCameraFrameTimeStamp(int number, unsigned long *TStampSec, long *TStampMicro, int idx);
    

    I connect these signals with two slots in the main window.
    This part works fine.

    In one slot of the main window, let's say slot 1, I do:

    std::memcpy(ImageSpaceInMemory.imageDataAll + offset, pData, size);
    

    it works well, receiving data is smooth.

    In another slot, let's say slot 2, I do

    qint64 *datePtr;
    datePtr = ImageSpaceInMemory.TStampSec;
    datePtr[k*2] = static_cast<qint64>(TStampSec[idx]); // TStampSec[idx];
    datePtr[k*2+1] = TStampMicro[idx];
    

    It also works well.

    However, if I try to do a second operation with the struct in slot 1:

    std::memcpy(ImageSpaceInMemory.imageDataAll + offset, pData, size);
    
    int *imageIndexPtr;
    imageIndexPtr = ImageSpaceInMemory.imageIndex;
    imageIndexPtr[k] = static_cast<int>(ki);
    

    The program will crash immediately.

    I just don't understand, in slot 2 this method works well, and in slot 1 the memcpy also works well. Why does the last try do not work at all?
    As a new user, I do not know better memory model/method that can handle this problem better. If you know it, I deeply appreciate it if you can share it.

    Another point is, I need to pre-allocate memory before receiving the data from a child thread.

    Could someone help me out?

    Christian EhrlicherC 1 Reply Last reply
    0
    • T tian_qhcn

      I am a new user in C++ object-oriented programming and Qt GUI framework. Now I got a very strange error for me, I hope someone can help one out.

      In the hearder of the main window I created a struct with three fields:

      struct structForDataReceiving {
          char *imageDataAll;
          int *index;
          qint64 *TimeStamp;
      }
      

      In the header file of the main window, I create an struct like this:

      structForDataReceiving ImageSpaceInMemory;
      

      Before receiving the data, in the main window I allocate memory like this:

      ImageSpaceInMemory.imageDataAll = new char [totalMemorySize];
      ImageSpaceInMemory.TStampSec = new qint64 [totalFrames * 2];
      ImageSpaceInMemory.imageIndex = new int [totalFrames];
      

      From an independent threads, there are two signals to send the data to the slots in the main window:

      void emitCameraFrameFinished(int number, int ki, char* pData, int XSize, int YSize, int Zsize);
      void emitCameraFrameTimeStamp(int number, unsigned long *TStampSec, long *TStampMicro, int idx);
      

      I connect these signals with two slots in the main window.
      This part works fine.

      In one slot of the main window, let's say slot 1, I do:

      std::memcpy(ImageSpaceInMemory.imageDataAll + offset, pData, size);
      

      it works well, receiving data is smooth.

      In another slot, let's say slot 2, I do

      qint64 *datePtr;
      datePtr = ImageSpaceInMemory.TStampSec;
      datePtr[k*2] = static_cast<qint64>(TStampSec[idx]); // TStampSec[idx];
      datePtr[k*2+1] = TStampMicro[idx];
      

      It also works well.

      However, if I try to do a second operation with the struct in slot 1:

      std::memcpy(ImageSpaceInMemory.imageDataAll + offset, pData, size);
      
      int *imageIndexPtr;
      imageIndexPtr = ImageSpaceInMemory.imageIndex;
      imageIndexPtr[k] = static_cast<int>(ki);
      

      The program will crash immediately.

      I just don't understand, in slot 2 this method works well, and in slot 1 the memcpy also works well. Why does the last try do not work at all?
      As a new user, I do not know better memory model/method that can handle this problem better. If you know it, I deeply appreciate it if you can share it.

      Another point is, I need to pre-allocate memory before receiving the data from a child thread.

      Could someone help me out?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @tian_qhcn said in C++/Qt signals and dynamic memory operation:

      Before receiving the data, in the main window I allocate memory like this:

      And where do you do your deallocation?

      Use c++ containers like e.g. std::vector<> instead leaking memory / maybe working on deallocated memory regions.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • T Offline
        T Offline
        tian_qhcn
        wrote on last edited by
        #3

        The memory is allocated in a sub-function of the mainwindow, where the data receiving slot sits.

        Christian EhrlicherC 1 Reply Last reply
        0
        • T tian_qhcn

          The memory is allocated in a sub-function of the mainwindow, where the data receiving slot sits.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @tian_qhcn I asked about the de-allocation. And when you've control over it - use c++ containers to avoid trouble.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          T 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @tian_qhcn I asked about the de-allocation. And when you've control over it - use c++ containers to avoid trouble.

            T Offline
            T Offline
            tian_qhcn
            wrote on last edited by
            #5

            @Christian-Ehrlicher Thanks a lot for your reply.
            The de-allocation is also done in the same location as allocation. Normally, I check if I need to de-allocate memory before I do the allocation. If needed, de-allocation is done first before next memory allocation.
            Anyway, I am trying your advice by using C++ containers. C++ containers seem to work. I am still testing it. I will update it later.

            Christian EhrlicherC 1 Reply Last reply
            0
            • T tian_qhcn

              @Christian-Ehrlicher Thanks a lot for your reply.
              The de-allocation is also done in the same location as allocation. Normally, I check if I need to de-allocate memory before I do the allocation. If needed, de-allocation is done first before next memory allocation.
              Anyway, I am trying your advice by using C++ containers. C++ containers seem to work. I am still testing it. I will update it later.

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @tian_qhcn said in C++/Qt signals and dynamic memory operation:

              The de-allocation is also done in the same location as allocation.

              So how do you make sure that the other thread does not access the memory you're currently de-allocating?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              T 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @tian_qhcn said in C++/Qt signals and dynamic memory operation:

                The de-allocation is also done in the same location as allocation.

                So how do you make sure that the other thread does not access the memory you're currently de-allocating?

                T Offline
                T Offline
                tian_qhcn
                wrote on last edited by
                #7

                @Christian-Ehrlicher That's very easy for me, because I just do not have a second thread that is running when I do the de-allocation and allocation.
                As a beginner, my program is very simple, and it just has the main windows thread and a child thread with QtConcurrent::run(). When the memory is being prepared, the QtConcurrent::run() is not jet started.

                Christian EhrlicherC 1 Reply Last reply
                0
                • T tian_qhcn

                  @Christian-Ehrlicher That's very easy for me, because I just do not have a second thread that is running when I do the de-allocation and allocation.
                  As a beginner, my program is very simple, and it just has the main windows thread and a child thread with QtConcurrent::run(). When the memory is being prepared, the QtConcurrent::run() is not jet started.

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @tian_qhcn said in C++/Qt signals and dynamic memory operation:

                  That's very easy for me, because I just do not have a second thread that is running when I do the de-allocation and allocation.

                  But you're doing the modification/access in a different thread according your first post:

                  From an independent threads, there are two signals to send the data to the slots in the main window:

                  So how do you know in one thread that your data is not accessed/modified in the other one?
                  Why do you need a second thread at all?

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  T 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @tian_qhcn said in C++/Qt signals and dynamic memory operation:

                    That's very easy for me, because I just do not have a second thread that is running when I do the de-allocation and allocation.

                    But you're doing the modification/access in a different thread according your first post:

                    From an independent threads, there are two signals to send the data to the slots in the main window:

                    So how do you know in one thread that your data is not accessed/modified in the other one?
                    Why do you need a second thread at all?

                    T Offline
                    T Offline
                    tian_qhcn
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher Thank you very much for your great effort. The C++ container method solved my problem perfectly.

                    1 Reply Last reply
                    1

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved