如何稳定地将 char* 转换为 TAO::String_Manager_T<char>
How to stable convert char* to TAO::String_Manager_T<char>
我使用了 OpenDDS 的 (DDS_HOME/tests/DCPS/Messenger) 发布者和订阅者示例。
在该源代码中,我只是更改了非常简单的代码。
我想把消息放在 message.text
中。但是,它不起作用。
实际上,当我第一次输入单词时,效果很好。
但是,当我下一个输入单词时它不起作用。
我认为这与内存分配问题有关。
那么,如何将 char*
转换为 TAO::String_Manager_T<char>
类型?
这是我的部分源代码和错误信息:
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]='[=12=]';
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 -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 -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
终于解决了我的问题
我用message.text= CORBA::string_dup(userInput);
而不是message.text= userInput;
我使用了 OpenDDS 的 (DDS_HOME/tests/DCPS/Messenger) 发布者和订阅者示例。
在该源代码中,我只是更改了非常简单的代码。
我想把消息放在 message.text
中。但是,它不起作用。
实际上,当我第一次输入单词时,效果很好。
但是,当我下一个输入单词时它不起作用。
我认为这与内存分配问题有关。
那么,如何将 char*
转换为 TAO::String_Manager_T<char>
类型?
这是我的部分源代码和错误信息:
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]='[=12=]';
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 -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 -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
终于解决了我的问题
我用message.text= CORBA::string_dup(userInput);
而不是message.text= userInput;