2016-03-25 108 views
1

私はRTPの間にベクターの通信を試みていますが、すべて正常ですが、私はmemPartFreeエラーが発生しています!ここ memPartFreeエラーを取得するvxWorks

#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が停止なっている理由はわかりません!!助けて!!

答えて

0

vectorクラスの内部を送信するvector<test> vecの10バイトを送信しているので、私の推測によると思います。

struct testを送信しているように見えますが、代わりにベクターの最初の要素に内部ポインタを送信しているとします。送信者は、メッセージを正常に送信した後にメモリを解放し、受信者がメッセージを受信した後に解放されたメモリを解放しようとします(送信者と受信者の両方が同じメモリ上で動作します)。

これは、機能を離れる前に、送信者または受信者のタスクの最後にwhile (1) taskDelay(1);を置くことによってテストできます。これにより、そのうちの1人がメモリを解放できなくなります。その後、私が正しい場合にはmemPartFreeエラーが表示されないはずです(memroyが2回解放されないため)。将来的にあなたの構造体の変化の大きさならば、トラブルを避けるために#define SIZE_OF_EACH_MESSAGE sizeof(struct test)

  1. 変更#define SIZE_OF_EACH_MESSAGE 10 ...
  2. メッセージに各要素struct testのデータを入れて:これは、次の操作を行い解決するために

    &vec(例:&(vec[0]))から始まる10バイトではなく、msgQSendコールはint MsgQStatus = msgQSend(MsgQ_ID, (char*)&(vec[0]),(SIZE_OF_EACH_MESSAGE * ONE_MESSAGE), WAIT_FOR_EVER, MSG_PRI_NORMAL);のようになります。

  3. 一時変数int MsgQStatus = msgQReceive(MsgQ_ID, (char*)&Obj, (SIZE_OF_EACH_MESSAGE * ONE_MESSAGE), WAIT_FOR_EVER);を使用してデータを受信します。
  4. データを正常に受信したら、それを受信機ベクトル(vec.push_back(Obj);)の最後にプッシュします。