第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何釋放不安全。在 cgo 中使用包裝器的指針?

如何釋放不安全。在 cgo 中使用包裝器的指針?

Go
人到中年有點(diǎn)甜 2022-09-26 20:15:39
我該如何創(chuàng)建一個(gè)包裝器來(lái)釋放不安全的人。指針在我的代碼中?這是我的 cgo 代碼://export clientpyfunc 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 FreeCBytefunc FreeCByte(b *unsafe.Pointer) {    C.free(unsafe.Pointer(b))}似乎我無(wú)法釋放python代碼中的內(nèi)存。我正在創(chuàng)建一個(gè)包裝器,以便我可以在python中釋放內(nèi)存,而不是在go內(nèi)部釋放內(nèi)存,這樣我就不會(huì)有懸空的指針。這是我的蟒蛇代碼:from ctypes import cdllimport ctypes, 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=[]):    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如何釋放不安全的指針?
查看完整描述

1 回答

?
心有法竹

TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊

釋放 Go 中的,而不是指向 的指針。unsafe.Pointerunsafe.Pointer


//export FreeCByte

func FreeCByte(b unsafe.Pointer) {

    C.free(b)

}

評(píng)論:我仍然收到內(nèi)存泄漏。– 喬頓


問(wèn)題中的代碼無(wú)法編譯和運(yùn)行。我修復(fù)了您的代碼以進(jìn)行編譯和運(yùn)行,并添加了一些調(diào)試代碼。顯然,內(nèi)存正在被釋放。


$ go version

go 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 --version

Python 3.9.5

$ python3 test.py

proxy: []

Go: cclientpy: C.CBytes: 0x1a7e420

Go: FreeCByte: 0x1a7e420

Go: FreeCByte: double free 0x1a7e420

free(): double free detected in tcache 2

Aborted (core dumped)

$ cat test.go


package main


import (

    "encoding/binary"

    "fmt"

    "os"

    "unsafe"

)


/*

#include <stdlib.h>

*/

import "C"


func UrlGet(s, headers, p string) []byte {

    return nil

}


const debug = true


//export cclientpy

func cclientpy(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)))

    cbytes := C.CBytes(append(length, request...))


    if debug {

        fmt.Fprintln(os.Stderr, "Go: cclientpy: C.CBytes:", cbytes)

    }


    return cbytes

}


//export FreeCByte

func FreeCByte(b unsafe.Pointer) {

    if debug {

        fmt.Fprintln(os.Stderr, "Go: FreeCByte:", b)

    }


    C.free(b)


    if debug {

        // tests to see if already freed, should fail

        // free(): double free detected in tcache 2

        // Aborted (core dumped)

        fmt.Fprintln(os.Stderr, "Go: FreeCByte: double free", b)

        C.free(b)

    }

}


func main() {}


$ cat test.py


from ctypes import cdll

import ctypes, chardet as 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

    

newrequest(path='argpath', lister={}, proxy=[])



查看完整回答
反對(duì) 回復(fù) 2022-09-26
  • 1 回答
  • 0 關(guān)注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)