简体   繁体   English

使用char指针函数和std :: string调用线程会产生不同的结果

[英]Calling thread with char pointer function and std::string produces different results

I have a function that returns a char pointer called loop_p and I call it many times on my main_thread like this to pass it to the py_embed thread: 我有一个函数返回一个称为loop_p的char指针,并在main_thread上多次调用它,将其传递给py_embed线程:

HANDLE handle;
SENDTOPY *cmd=new SENDTOPY();

char* msg=loop_p(ac);
char *argv[4]={"PythonPlugIn2","bridge","test_callsign",msg};
cmd->argc=4;
for(int i = 0; i < NUM_ARGUMENTS; i++ )
{
    cmd->argv[i] = argv[i];
}

handle=(HANDLE) _beginthread(py_embed,0,(void*)cmd);}

where SENDTOPY is a struct: 其中SENDTOPY是结构:

typedef struct{
    int argc;
    char *argv[4];
}SENDTOPY;

The message it sent to python like this and python receives it well: 像这样发送给python和python的消息接收良好:

SENDTOPY *arg=(SENDTOPY*)data;
pArgs2=Py_BuildValue("(s)",arg->argv[4]);
pValue2 = PyObject_CallObject(pFunc, pArgs2);

In order to avoid having memory allocation problems i modified the loop_p function to a function that returns a std::string . 为了避免出现内存分配问题,我将loop_p函数修改为一个返回std::string的函数。 I then call that string in the main_thread with some modifications: 然后,我通过一些修改在main_thread调用该字符串:

...

std::string msg_python=loop_p(ac);
const char * msg2=msg_python.data();

char *argv[3]={"PythonPlugIn2","bridge","test_callsign"};
cmd->argc=3;
cmd->msg=msg2;
for(...
 ...

and i modify the struct SENDTOPY to this: 我将SENDTOPY结构修改为:

typedef struct{
    int argc;
    char *argv[3];
        const char* msg;
}SENDTOPY;

I print it to a textfile in the main_thread and the message before and after the modifications are equal. 我将其打印到main_thread的文本文件中,修改前后的消息相同。 But in the py_embed thread the const char is no longer what is was, is just a bunch of gibberish. 但是在py_embed线程中,const char不再是原来的,只是一堆乱码。 What am I doing wrong? 我究竟做错了什么?

Thank you in advance. 先感谢您。

Edit: loop_p code 编辑: loop_p代码

std::string CNewDisplay::loop_p(int ac){

std::string res("Number of Aircrafts\nHour of simulation\n\n");

for (...
                    ....
        //Route
        textfile<<fp.GetRoute()<<endl;
        std::string route=fp.GetRoute();
        std::replace(route.begin(),route.end(),' ',',');
        res+=route;
        res.append(",\n");

        res.append("\n\n");


        };

return res;

}

It appears to me that you are storing a pointer to the internal guts of a temporary string object created on the stack. 在我看来,您正在存储一个指向在堆栈上创建的临时字符串对象的内部胆量的指针。 If you make string static, then the string's guts will remain valid throughout program execution, and you can safely store pointer to string guts: 如果将字符串设为静态,则字符串的胆量将在整个程序执行期间保持有效,并且可以安全地存储指向字符串胆量的指针:

static std::string msg_python;       // survives beyond local scope

msg_python=loop_p(ac);               // set string to loop_p return value 
const char *msg2=msg_python.c_str(); // get ptr each time since it could change

Also, ensure that you use .c_str() to get your c-style char string pointer so that you are assured the string is null-terminated. 另外,请确保使用.c_str()获取c样式的char字符串指针,以确保该字符串以null终止。 Using .data() does not guarantee null termination. 使用.data()不能保证终止为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM