C和Go之间的互操作性问题涉及到C.free()函数。

huangapple go评论94阅读模式
英文:

C and Go interoperability issue involving C.free()

问题

我有一个Go函数,它包装了lib_proc.h中的proc_name(pid,...)函数。

这是完整的C原型

int proc_name(int pid, void * buffer, uint32_t buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);

可以在这里找到它:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/libproc.h(至少在我的系统上)。

以下是Go代码:

package goproc

/*
#include "libproc.h"

int call_proc_name(int pid, char *name, int name_size) {
  return proc_name(pid, name, name_size);
}
*/
import "C"
import "unsafe"
import "strings"

type DarwinProcess struct {
	Process
}

func (DarwinProcess) NameOf(pid int) string {
	name := C.CString(strings.Repeat("\x00", 1024))
	defer C.free(unsafe.Pointer(name))
	nameLen := C.call_proc_name(C.int(pid), name, C.int(1024))
	var result string

	if (nameLen > 0) {
		result = C.GoString(name);
	} else {
		result = ""
	}

	return result;
}

除非删除C.free(unsafe.Pointer(...))import "unsafe"语句,否则此代码将无法编译。
DarwinProcess::NameOf(pid)方法只适用于Mac OS X,如果从代码中删除C.free(...),它实际上可以工作。

go build之后,我得到以下错误消息:
could not determine kind of name for C.free(这是整个编译器输出)。

对我来说,删除C.free(...)是不可接受的,我必须找到如何正确释放使用C.CString()分配的内存的方法。

我感到困惑,因为根据文档,一切都做得正确。我在这里和网络上都没有找到解决方案。

英文:

I've a Go function that wraps the proc_name(pid,...) function from lib_proc.h.

This is the complete C prototype:
<pre><code>
<!-- language: lang-c -->
int proc_name(int pid, void * buffer, uint32_t buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
</code></pre>

that can be found here /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/libproc.h (at least on my system).

It follows Go code:
<pre><code>
<!-- language: lang-go -->
package goproc

/*
#include &quot;libproc.h&quot;
 
int call_proc_name(int pid, char *name, int name_size) {
  return proc_name(pid, name, name_size);
}
*/
import &quot;C&quot;
import &quot;unsafe&quot;
import &quot;strings&quot;

type DarwinProcess struct {
	Process
}

func (DarwinProcess) NameOf(pid int) string {
    name := C.CString(strings.Repeat(&quot;\x00&quot;, 1024))
    defer C.free(unsafe.Pointer(name))
    nameLen := C.call_proc_name(C.int(pid), name, C.int(1024))
    var result string

	if (nameLen &gt; 0) {
		result = C.GoString(name);
	} else {
		result = &quot;&quot;
	}

	return result;
}

</code></pre>

This code won't compile unless the call to C.free(unsafe.Pointer(...)) and import &quot;unsafe&quot; clause are removed.
DarwinProcess::NameOf(pid) method is meant to work only on Mac OS X and it actually works if C.free(...) is removed from code.

In its actual form after go build I get the following error message:
could not determine kind of name for C.free (and nothing more, this is the whole compiler output).

Removing C.free(...) is not acceptable to me, I must find how to properly free the memory allocated with C.CString().

I'm puzzled since, according to documentation, everything is done properly. I have not been able to find a solution nor searching here nor on the web.

答案1

得分: 21

libproc.h没有包含stdlib.h,其中声明了free()函数。因此,编译器无法解析该名称。在我在cgo注释块的开头添加了#include <stdlib.h>之后,你的代码在我的系统上成功构建了。

英文:

libproc.h doesn't include stdlib.h, where free() is declared. As such, the compiler cannot resolve the name. After I added #include &lt;stdlib.h&gt; at the beginning of the cgo comment block your code built successfully on my system.

huangapple
  • 本文由 发表于 2015年5月27日 19:34:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/30481268.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定