Reputation: 29
I am trying communicating vector's in between RTP's all is well but i am getting memPartFree error!!
#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;
}
}
and here is the output :
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
i have no idea why i am getting memPartFree error and due to this my RTP is getting stopped!! HELP!!
Upvotes: 1
Views: 3260
Reputation: 3207
My guess is that since you are sending 10 bytes of vector<test> vec
that you are sending internals of the vector
class.
Although it looks like you're sending your struct test
I assume that you're sending an internal pointer to the first element of the vector instead. Your sender frees the memory after it successfully sent the messages causing the receiver to try to free already freed memory after the message was received (both sender and receiver work on the same memory).
This can be tested by putting a while (1) taskDelay(1);
at the end of either the sender or receiver task before leaving the function. This inhibits one of them to free the memory. Then the memPartFree
error shouldn't be appearing if I'm correct (since the memroy is not freed twice).
To solve this do the following:
#define SIZE_OF_EACH_MESSAGE 10
to #define SIZE_OF_EACH_MESSAGE sizeof(struct test)
to avoid trouble if the size of your struct changes in the future...struct test
into the message queue instead of 10 bytes starting at &vec
(e.g. &(vec[0])
) so your msgQSend
call should look something like this: int MsgQStatus = msgQSend(MsgQ_ID, (char*)&(vec[0]),(SIZE_OF_EACH_MESSAGE * ONE_MESSAGE), WAIT_FOR_EVER, MSG_PRI_NORMAL);
.struct test Obj;
variable int MsgQStatus = msgQReceive(MsgQ_ID, (char*)&Obj, (SIZE_OF_EACH_MESSAGE * ONE_MESSAGE), WAIT_FOR_EVER);
.vec.push_back(Obj);
). Upvotes: 1