如何释放不安全。在 cgo 中使用包装器的指针?

我该如何创建一个包装器来释放不安全的人。指针在我的代码中?这是我的 cgo 代码:


//export clientpy

func clientpy(url *C.char, headersfrompy *C.char, proxy *C.char) unsafe.Pointer {

    s := C.GoString(url)

    headers := C.GoString(headersfrompy)

    p := C.GoString(proxy)


    request := UrlGet(s, headers, p)


    length := make([]byte, 8)


    binary.LittleEndian.PutUint64(length, uint64(len(request)))

    return C.CBytes(append(length, request...))

}


//export FreeCByte

func FreeCByte(b *unsafe.Pointer) {

    C.free(unsafe.Pointer(b))

}

似乎我无法释放python代码中的内存。我正在创建一个包装器,以便我可以在python中释放内存,而不是在go内部释放内存,这样我就不会有悬空的指针。


这是我的蟒蛇代码:


from ctypes import cdll

import ctypes, cchardet, json

from bs4 import BeautifulSoup


lib = cdll.LoadLibrary("./test.so")

lib.cclientpy.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p]

lib.cclientpy.restype = ctypes.POINTER(ctypes.c_ubyte * 8)

""" THERE IS A BIG MEMORY LEAK, BEWARE """


free = lib.free

free.argtypes = [ctypes.POINTER(ctypes.c_ubyte * 8)]


def newrequest(path, lister={}, proxy=[]):

    try:

        print(f"proxy: {proxy}")

        ptr = lib.cclientpy(path.encode("utf-8"), str(lister).encode("utf-8"), str(proxy).encode("utf-8"))

        length = int.from_bytes(ptr.contents, byteorder="little")

        data = bytes(ctypes.cast(ptr,

                                 ctypes.POINTER(ctypes.c_ubyte * (8 + length))

                                 ).contents[8:])

        #free(ptr)

        lib.FreeCByte(ptr)

        print(f'bytes: {bytes(ctypes.cast(ptr,ctypes.POINTER(ctypes.c_ubyte * (8 + length))).contents[8:])}')


        return data

    except:

        pass

如何释放不安全的指针?


人到中年有点甜
浏览 127回答 1
1回答

心有法竹

释放 Go 中的,而不是指向 的指针。unsafe.Pointerunsafe.Pointer//export FreeCBytefunc FreeCByte(b unsafe.Pointer) {&nbsp; &nbsp; C.free(b)}评论:我仍然收到内存泄漏。– 乔顿问题中的代码无法编译和运行。我修复了您的代码以进行编译和运行,并添加了一些调试代码。显然,内存正在被释放。$ go versiongo version devel go1.18-8214257347 Wed Sep 8 14:51:40 2021 +0000 linux/amd64$ go build -o test.so -buildmode=c-shared test.go$ python3 --versionPython 3.9.5$ python3 test.pyproxy: []Go: cclientpy: C.CBytes: 0x1a7e420Go: FreeCByte: 0x1a7e420Go: FreeCByte: double free 0x1a7e420free(): double free detected in tcache 2Aborted (core dumped)$&nbsp;$ cat test.gopackage mainimport (&nbsp; &nbsp; "encoding/binary"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "os"&nbsp; &nbsp; "unsafe")/*#include <stdlib.h>*/import "C"func UrlGet(s, headers, p string) []byte {&nbsp; &nbsp; return nil}const debug = true//export cclientpyfunc cclientpy(url *C.char, headersfrompy *C.char, proxy *C.char) unsafe.Pointer {&nbsp; &nbsp; s := C.GoString(url)&nbsp; &nbsp; headers := C.GoString(headersfrompy)&nbsp; &nbsp; p := C.GoString(proxy)&nbsp; &nbsp; request := UrlGet(s, headers, p)&nbsp; &nbsp; length := make([]byte, 8)&nbsp; &nbsp; binary.LittleEndian.PutUint64(length, uint64(len(request)))&nbsp; &nbsp; cbytes := C.CBytes(append(length, request...))&nbsp; &nbsp; if debug {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintln(os.Stderr, "Go: cclientpy: C.CBytes:", cbytes)&nbsp; &nbsp; }&nbsp; &nbsp; return cbytes}//export FreeCBytefunc FreeCByte(b unsafe.Pointer) {&nbsp; &nbsp; if debug {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintln(os.Stderr, "Go: FreeCByte:", b)&nbsp; &nbsp; }&nbsp; &nbsp; C.free(b)&nbsp; &nbsp; if debug {&nbsp; &nbsp; &nbsp; &nbsp; // tests to see if already freed, should fail&nbsp; &nbsp; &nbsp; &nbsp; // free(): double free detected in tcache 2&nbsp; &nbsp; &nbsp; &nbsp; // Aborted (core dumped)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintln(os.Stderr, "Go: FreeCByte: double free", b)&nbsp; &nbsp; &nbsp; &nbsp; C.free(b)&nbsp; &nbsp; }}func main() {}$&nbsp;$ cat test.pyfrom ctypes import cdllimport ctypes, chardet as cchardet, jsonfrom bs4 import BeautifulSouplib = cdll.LoadLibrary("./test.so")lib.cclientpy.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p]lib.cclientpy.restype = ctypes.POINTER(ctypes.c_ubyte * 8)""" THERE IS A BIG MEMORY LEAK, BEWARE """free = lib.freefree.argtypes = [ctypes.POINTER(ctypes.c_ubyte * 8)]def newrequest(path, lister={}, proxy=[]):&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; print(f"proxy: {proxy}")&nbsp; &nbsp; &nbsp; &nbsp; ptr = lib.cclientpy(path.encode("utf-8"), str(lister).encode("utf-8"), str(proxy).encode("utf-8"))&nbsp; &nbsp; &nbsp; &nbsp; length = int.from_bytes(ptr.contents, byteorder="little")&nbsp; &nbsp; &nbsp; &nbsp; data = bytes(ctypes.cast(ptr,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ctypes.POINTER(ctypes.c_ubyte * (8 + length))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;).contents[8:])&nbsp; &nbsp; &nbsp; &nbsp; #free(ptr)&nbsp; &nbsp; &nbsp; &nbsp; lib.FreeCByte(ptr)&nbsp; &nbsp; &nbsp; &nbsp; print(f'bytes: {bytes(ctypes.cast(ptr,ctypes.POINTER(ctypes.c_ubyte * (8 + length))).contents[8:])}')&nbsp; &nbsp; &nbsp; &nbsp; return data&nbsp; &nbsp; except:&nbsp; &nbsp; &nbsp; &nbsp; pass&nbsp; &nbsp;&nbsp;newrequest(path='argpath', lister={}, proxy=[])$&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go