16

C++调用Go方法的字符串传递问题及解决方案

 3 years ago
source link: http://www.cnblogs.com/huaweiyun/p/13998446.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

摘要: C++调用Go方法时,字符串参数的内存管理需要由Go侧进行深度值拷贝。

现象

在一个APP技术项目中,子进程按请求加载Go的ServiceModule,将需要拉起的ServiceModule信息传递给Go的Loader,存在C++调用Go方法,传递字符串的场景。

方案验证时,发现有奇怪的将std::string对象的内容传递给Go方法后,在Go方法协程中取到的值与预期不一致。

经过一段时间的分析和验证,终于理解问题产生的原因并给出解决方案,现分享如下。

背景知识

  1. Go有自己的内存回收GC机制,通过make等申请的内存不需要手动释放。
  2. C++中为std::string变量赋值新字符串后,.c_str()和.size()的结果会联动变化,尤其是.c_str()指向的地址也有可能变化。
  3. go build -buildmode=c-shared .生成的.h头文件中定义了C++中Go的变量类型的定义映射关系,比如GoString、GoInt等。其中GoString实际是一个结构体,包含一个字符指针和一个字符长度。

原理及解释

通过代码示例方式解释具体现象及原因,详见注释

C++侧代码:

//
    // Created by w00526151 on 2020/11/5.
    //
     
    #include <string>
    #include <iostream>
    #include <unistd.h>
    #include "libgoloader.h"
     
    /**
     * 构造GoString结构体对象
     * @param p
     * @param n
     * @return
     */
    GoString buildGoString(const char* p, size_t n){
        //typedef struct { const char *p; ptrdiff_t n; } _GoString_;
        //typedef _GoString_ GoString;
        return {p, static_cast<ptrdiff_t>(n)};
    }
     
    int main(){
        std::cout<<"test send string to go in C++"<<std::endl;
     
        std::string tmpStr = "/tmp/udsgateway-netconftemplateservice";
        printf("in C++ tmpStr: %p, tmpStr: %s, tmpStr.size:%lu \r\n", tmpStr.c_str(), tmpStr.c_str(), tmpStr.size());
        {
            //通过new新申请一段内存做字符串拷贝
            char *newStrPtr = NULL;
            int newStrSize = tmpStr.size();
            newStrPtr = new char[newStrSize];
            tmpStr.copy(newStrPtr, newStrSize, 0);
     
            //调用Go方法,第一个参数直接传std::string的c_str指针和大小,第二个参数传在C++中单独申请的内存并拷贝的字符串指针,第三个参数和第一个一样,但是在go代码中做内存拷贝保存。
            //调用Go方法后,通过赋值修改std::string的值内容,等待Go中新起的线程10s后再将三个参数值打印出来。
            LoadModule(buildGoString(tmpStr.c_str(), tmpStr.size()), buildGoString(newStrPtr, newStrSize), buildGoString(tmpStr.c_str(),tmpStr.size()));
            //修改tmpStr的值,tmpStr.c_str()得到的指针指向内容会变化,tmpStr.size()的值也会变化,Go中第一个参数也会受到影响,前几位会变成新字符串内容。
            //由于在Go中int是值拷贝,所以在Go中,第一个参数的长度没有变化,因此实际在Go中已经出现内存越界访问,可能产生Coredump。
            tmpStr = "new string";
            printf("in C++ change tmpStr and delete newStrPtr, new tmpStr: %p, tmpStr: %s, tmpStr.size:%lu \r\n", tmpStr.c_str(), tmpStr.c_str(), tmpStr.size());
            //释放新申请的newStrPtr指针,Go中对应第二个string变量内存也会受到影响,产生乱码。
            // 实际在Go中,已经在访问一段在C++中已经释放的内存,属于野指针访问,可能产生Coredump。
            delete newStrPtr;
        }
        pause();
    }

Go侧代码:

package main
     
    import "C"
    import (
        "fmt"
        "time"
    )
     
    func printInGo(p0 string, p1 string, p2 string){
        time.Sleep(10 * time.Second)
        fmt.Printf("in go function, p0:%s size %d, p1:%s size %d, p2:%s size %d", p0, len(p0), p1, len(p1), p2, len(p2))
    }
     
    //export LoadModule
    func LoadModule(name string, version string, location string) int {
        //通过make的方式,新构建一段内存来存放从C++处传入的字符串,深度拷贝防止C++中修改影响Go
        tmp3rdParam := make([]byte, len(location))
        copy(tmp3rdParam, location)
        new3rdParam := string(tmp3rdParam)
        fmt.Println("in go loadModule,first param is",name,"second param is",version, "third param is", new3rdParam)
        go printInGo(name, version, new3rdParam);
        return 0
    }

Go侧代码通过-buildmode=c-shared的方式生成libgoloader.so及libgoloader.h供C++编译运行使用

    go build -o libgoloader.so -buildmode=c-shared .

程序执行结果:

test send string to go in C++
    in C++ tmpStr: 0x7fffe1fb93f0, tmpStr: /tmp/udsgateway-netconftemplateservice, tmpStr.size:38 
    # 将C++的指针传给Go,一开始打印都是OK的
    in go loadModule,first param is /tmp/udsgateway-netconftemplateservice second param is /tmp/udsgateway-netconftemplateservice third param is /tmp/udsgateway-netconftemplateservice
    # 在C++中,将指针指向的内容修改,或者删掉指针
    in C++ change tmpStr and delete newStrPtr, new tmpStr: 0x7fffe1fb93f0, tmpStr: new string, tmpStr.size:10 
    # 在Go中,参数1、参数2对应的Go string变量都受到了影响,参数3由于做了深度拷贝,没有受到影响。
    in go function, p0:new string eway-netconftemplateservice size 38, p1:        p���  netconftemplateservice size 38, p2:/tmp/udsgateway-netconftemplateservice size 38

结论

  • 结论: C++调用Go方法时,字符串参数的内存管理需要由Go侧进行深度值拷贝。即参数三的处理方式
  • 原因: 传入的字符串GoString,实际是一个结构体,第一个成员p是一个char*指针,第二个成员n是一个int长度。

在C++代码中,任何对成员p的char*指针的操作,都将直接影响到Go中的string对象的值。

只有通过单独的内存空间开辟,进行独立内存管理,才可以避免C++中的指针操作对Go的影响。

ps:不在C++中进行内存申请释放的原因是C++无法感知Go中何时才能真的已经没有对象引用,无法找到合适的时间点进行内存释放。

本文分享自华为云社区《C++调用Go方法的字符串传递问题及解决方案》,原文作者:王芾。

点击关注,第一时间了解华为云新鲜技术~


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK