C++/Qt signals and dynamic memory operation
-
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?
-
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?
@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.
-
The memory is allocated in a sub-function of the mainwindow, where the data receiving slot sits.
@tian_qhcn I asked about the de-allocation. And when you've control over it - use c++ containers to avoid trouble.
-
@tian_qhcn I asked about the de-allocation. And when you've control over it - use c++ containers to avoid trouble.
@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-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.@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?
-
@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?
@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-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.@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? -
@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?@Christian-Ehrlicher Thank you very much for your great effort. The C++ container method solved my problem perfectly.