Search code examples
c++acedata-distribution-servicetaoopendds

How to stable convert char* to TAO::String_Manager_T<char>


I used OpenDDS's (DDS_HOME/tests/DCPS/Messenger) Publisher and Subscriber example.

In that source code, I just changed very simple code. I want to put the message in message.text. But, it doesn't work. Actually, when I input my word first time, It works well. But, it doesn't work when I next input word. I think it is related to memory allocation issues.

So, how can I convert char* to TAO::String_Manager_T<char> type?

Here is my part of source code and error message:

Writer.cpp:

Messenger::Message message;
message.subject_id = 99;

DDS::InstanceHandle_t handle = message_dw->register_instance(message);

static struct sigaction act;
act.sa_handler=ctrlCfunc;
sigaction(SIGINT,&act,NULL);
/* We are all initialized, just service communications                  */

char userInput[255];
doIt=1;

message.from         = "Comic Book Guy";
message.subject      = "Review";
message.text         = "Worst. Movie. Ever.";
message.count        = 0;

while(doIt) {

    fgets(userInput, sizeof(userInput), stdin);
    userInput[sizeof(userInput)-1]='\0';

    message.text=userInput;

  DDS::ReturnCode_t error;
  do {
    error = message_dw->write(message, handle);
  } while (error == DDS::RETCODE_TIMEOUT);

  if (error != DDS::RETCODE_OK) {
    ACE_ERROR((LM_ERROR,
               ACE_TEXT("%N:%l: svc()")
               ACE_TEXT(" ERROR: write returned %d!\n"), error));
  }

  message.count++;
}

Publisher side error:

# ./publisher -DCPSInfoRepo 210.107.212.75:12345 -DCPSConfigFile rtps_uni.ini
Starting publisher
(7352|140545985509184) NOTICE: using DCPSInfoRepo value from command option (overrides value if it's in config file).
Starting publisher with 1 args
(7352|140545985509184) WARNING: Could not find FQDN. Using "127.0.0.1" as fully qualified hostname, please correct system configuration.
Reliable DataWriter
Creating Writer
Starting Writer
testbug0
testbug1
*** Error in `./publisher': free(): invalid pointer: 0x00007fd35d37dcb0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7fd3677427e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7fd36774b37a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7fd36774f53c]
/home/user/Desktop/OpenDDS-3.11/ACE_wrappers/lib/libTAO.so.2.2a_p12(_ZN5CORBA11string_freeEPc+0x2c)[0x7fd36878e1dc]
./publisher[0x4063dc]
./publisher[0x409f91]
./publisher[0x4094e7]
/home/user/Desktop/OpenDDS-3.11/ACE_wrappers/lib/libACE.so.6.2a_p12(_ZN13ACE_Task_Base7svc_runEPv+0x54)[0x7fd3683ccebc]
/home/user/Desktop/OpenDDS-3.11/ACE_wrappers/lib/libACE.so.6.2a_p12(_ZN18ACE_Thread_Adapter8invoke_iEv+0x113)[0x7fd3683cd52d]
/home/user/Desktop/OpenDDS-3.11/ACE_wrappers/lib/libACE.so.6.2a_p12(_ZN18ACE_Thread_Adapter6invokeEv+0xc7)[0x7fd3683cd3d1]
/home/user/Desktop/OpenDDS-3.11/ACE_wrappers/lib/libACE.so.6.2a_p12(ace_thread_adapter+0x2b)[0x7fd368338cb7]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x76ba)[0x7fd367a9c6ba]
/lib/x86_64-linux-gnu/libc.so.6(clone+0x6d)[0x7fd3677d23dd]
======= Memory map: ========
00400000-0040f000 r-xp 00000000 08:01 140986                             /home/user/Desktop/OpenDDS-3.11/tests/DCPS/Messenger/publisher
0060e000-0060f000 r--p 0000e000 08:01 140986                             /home/user/Desktop/OpenDDS-3.11/tests/DCPS/Messenger/publisher
0060f000-00610000 rw-p 0000f000 08:01 140986                             /home/user/Desktop/OpenDDS-3.11/tests/DCPS/Messenger/publisher
00610000-00611000 rw-p 00000000 00:00 0
00667000-00787000 rw-p 00000000 00:00 0                                  [heap]
7fd33c000000-7fd33c022000 rw-p 00000000 00:00 0
7fd33c022000-7fd340000000 ---p 00000000 00:00 0
7fd344000000-7fd344022000 rw-p 00000000 00:00 0
7fd344022000-7fd348000000 ---p 00000000 00:00 0
7fd348000000-7fd348022000 rw-p 00000000 00:00 0
7fd348022000-7fd34c000000 ---p 00000000 00:00 0
7fd34c000000-7fd34c022000 rw-p 00000000 00:00 0
7fd34c022000-7fd350000000 ---p 00000000 00:00 0
7fd350000000-7fd350022000 rw-p 00000000 00:00 0
7fd350022000-7fd354000000 ---p 00000000 00:00 0
7fd354000000-7fd354022000 rw-p 00000000 00:00 0

Subscriber side error:

# ./subscriber -DCPSConfigFile rtps_uni.ini
(9370|140579829053248) WARNING: Could not find FQDN. Using "127.0.0.1" as fully qualified hostname, please correct system configuration.
Transport is RELIABLE
Reliable DataReader
DataReaderListener.cpp:151: INFO: on_subscription_matched()
SampleInfo.sample_rank = 0
SampleInfo.instance_state = 1
ERROR: Repeat Message: subject    = Review
         subject_id = 99
         from       = Comic Book Guy
         count      = 0
         text       = testbug0

ERROR: Invalid message.text

Solution

  • I finally solved my problem.

    I use message.text= CORBA::string_dup(userInput);

    instead of message.text= userInput;