获取 memPartFree 错误 vxWorks

Getting memPartFree error vxWorks

我正在尝试在 RTP 之间通信向量,一切正常,但我收到 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 被停止了!!帮助!!

我的猜测是,由于您发送的是 vector<test> vec 的 10 个字节,所以您发送的是 vector class.

的内部结构

虽然看起来您发送的是 struct test,但我假设您发送的是指向向量第一个元素的内部指针。发送方在成功发送消息后释放内存,导致接收方在收到消息后尝试释放已释放的内存(发送方和接收方都在同一内存上工作)。

这可以通过在离开函数之前在发送者或接收者任务的末尾放置一个 while (1) taskDelay(1); 来测试。这会阻止其中之一释放内存。然后 memPartFree 错误不应该出现如果我是正确的(因为 memroy 没有被释放两次)。

要解决此问题,请执行以下操作:

  1. #define SIZE_OF_EACH_MESSAGE 10 更改为 #define SIZE_OF_EACH_MESSAGE sizeof(struct test) 以避免将来结构大小发生变化时出现问题...
  2. 将每个元素 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);.
  3. 使用临时 struct test Obj; 变量接收数据 int MsgQStatus = msgQReceive(MsgQ_ID, (char*)&Obj, (SIZE_OF_EACH_MESSAGE * ONE_MESSAGE), WAIT_FOR_EVER);
  4. 成功接收数据后,将其推送到接收向量的末尾 (vec.push_back(Obj);)。