gpt4 book ai didi

c++ - 如何稳定地将 char* 转换为 TAO::String_Manager_T

转载 作者:行者123 更新时间:2023-11-30 03:29:33 27 4
gpt4 key购买 nike

我使用了 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]='\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 -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;

关于c++ - 如何稳定地将 char* 转换为 TAO::String_Manager_T<char>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45626677/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com