我正在尝试在RTP之间传递矢量' s一切都很好,但我得到了memPartFree错误!
#include <iostream>
#include <taskLib.h>
#include <cstdlib>
#include <vector>
#include <msgQLib.h>
using namespace std;
#define TEN_BYTES 10
#define HUNDERED_BYTES 100
#define THOUSAND_BYTES 1000
#define SIZE_OF_EACH_MESSAGE 10
#define ONE_MESSAGE 1
#define TEN_MESSAGE 10
#define HUNDERED_MESSAGE 100
#define WAIT_FOR_EVER -1
void sender();
void receiver();
struct test
{
short int num1;
short int num2;
short int num3;
short int num4;
short int num5;
};
MSG_Q_ID MsgQ_ID;
int main()
{
cout<<__FUNCTION__<<endl;
MsgQ_ID = msgQCreate(ONE_MESSAGE,SIZE_OF_EACH_MESSAGE,MSG_Q_FIFO);
taskSpawn("receiver",150,VX_FP_TASK,10000,FUNCPTR (receiver),0,0,0,0,0,0,0,0,0,0);
taskSpawn("sender",150,VX_FP_TASK,10000,FUNCPTR (sender),0,0,0,0,0,0,0,0,0,0);
cout<<"wait here"<<endl;
}
void sender()
{
cout<<__FUNCTION__<<endl;
vector<test> vec;
test Obj;
while((vec.size() * SIZE_OF_EACH_MESSAGE) != TEN_BYTES)
{
Obj.num1 = rand();
Obj.num2 = rand();
Obj.num3 = rand();
Obj.num4 = rand();
Obj.num5 = rand();
vec.push_back(Obj);
}
cout<<"Size of vector to be sent "<<vec.size()<<endl;
vector<test>::iterator it;
for(it = vec.begin();it!=vec.end();it++)
{
cout<<"Send Data:"<<endl;
cout<<it->num1<<"\t"<<it->num2<<"\t"<<it->num3<<"\t"<<it->num4<<"\t"<<it->num5<<endl;
}
int MsgQStatus = msgQSend(MsgQ_ID,(char*)&vec,(SIZE_OF_EACH_MESSAGE * ONE_MESSAGE),WAIT_FOR_EVER,MSG_PRI_NORMAL);
cout<<"Status of MsgQ Send:"<<MsgQStatus<<endl;
}
void receiver()
{
cout<<__FUNCTION__<<endl;
vector<test> vec;
vec.reserve(ONE_MESSAGE);// to initialize the vector otherwise it's size will become random
int MsgQStatus = msgQReceive(MsgQ_ID,(char*)&vec,(SIZE_OF_EACH_MESSAGE * ONE_MESSAGE),WAIT_FOR_EVER);
cout<<"Status of MsgQ Receive:"<<MsgQStatus<<endl;
vector<test>::iterator it;
cout<<"size of the received vector"<<vec.size()<<endl;
for(it = vec.begin();it!=vec.end();it++)
{
cout<<"Received data:"<<endl;
cout<<it->num1<<"\t"<<it->num2<<"\t"<<it->num3<<"\t"<<it->num4<<"\t"<<it->num5<<endl;
}
}
这是输出:
main
receiver
sender
Size of vector to be sent 1
Send Data:
7403 -19371 19159 -10975 -24349
Status of MsgQ Send:0
Status of MsgQ Receive:10
size of the received vector1
Received data:
7403 -19371 19159 -10975 -24349
memPartFree: invalid block 0xff873850 in partition 0xff8593a0
我不知道为什么我会收到memPartFree错误,因此我的RTP正在停止! HELP !!
答案 0 :(得分:0)
我的猜测是,因为您发送的是vector<test> vec
的10个字节,而您正在发送vector
类的内部。
虽然看起来你正在发送你的struct test
我假设你正在发送一个指向矢量第一个元素的内部指针。您的发件人在成功发送邮件后释放内存,导致接收者在收到邮件后尝试释放已释放的内存(发件人和收件人都在同一内存上工作)。
可以通过在离开函数之前将while (1) taskDelay(1);
放在发送方或接收方任务的末尾来测试。这抑制了其中一个释放内存。如果我是正确的,那么memPartFree
错误就不会出现(因为memroy没有被释放两次)。
要解决此问题,请执行以下操作:
#define SIZE_OF_EACH_MESSAGE 10
更改为#define SIZE_OF_EACH_MESSAGE sizeof(struct test)
以避免在未来结构大小发生变化时出现问题... struct test
的数据放入消息队列,而不是从&vec
开始的10个字节(例如&(vec[0])
),这样您的msgQSend
调用应该看起来像这样:int MsgQStatus = msgQSend(MsgQ_ID, (char*)&(vec[0]),(SIZE_OF_EACH_MESSAGE * ONE_MESSAGE), WAIT_FOR_EVER, MSG_PRI_NORMAL);
。struct test Obj;
变量int MsgQStatus = msgQReceive(MsgQ_ID, (char*)&Obj, (SIZE_OF_EACH_MESSAGE * ONE_MESSAGE), WAIT_FOR_EVER);
vec.push_back(Obj);
)。