Home > Backend Development > Golang > Mysterious error: 'Go pointer to cgo argument points to unpinned Go pointer'

Mysterious error: 'Go pointer to cgo argument points to unpinned Go pointer'

PHPz
Release: 2024-02-06 10:48:04
forward
662 people have browsed it

神秘错误:“cgo 参数的 Go 指针指向未固定的 Go 指针”

Question content

I am getting this error and I don't understand what it means even after reading the documentation. In fact, I did think I understood it, but it just didn't fit the way it happened to me. There doesn't seem to be any other information about this specific issue elsewhere on the internet.

For me, it happens in this function (not my library, which seems to work fine in a lot of places, so that's more weird).

To better understand the error, I modified it locally so that each parameter of the C function was constructed separately and saw which part triggered the error:

But it turns out the error occurs on the line under the cursor, so I think it's entirely related to the C.lmdbgo_mdb_cursor_get1() call.

I thought the error meant that I was passing a Go pointer to a C function and that I could work around it by wrapping the Go pointer with a unsafe.Pointer() call, but that's not possible because it already Completion exists and all arguments to the relevant C function are already C types, not Go types.

Update: After some more digging, I found that the actual panic() is issued at line 689 of /src/runtime/cgocall.go of:

Therefore cgoIsGoPointer() returns true and isPinned() returns false.

What should I do to "fix" the Go pointer? Or is this the wrong question?

UPDATE: After further digging, I found that the parameter causing the problem is c.txn.key, which is beyond my control and appears to be set to new (C.MDB_val).


Correct answer


According to https://github.com/PowerDNS/lmdb-go/issues /28 This question was actually raised by Caused by using the Go new() constructor to create a C pointer and then passing it to the C function. These should be created using C.malloc().

Apparently this problem has never appeared to anyone else, because no one has made the same mistake I made when using this library: using the same transaction from multiple goroutines, which is explicitly prohibited by LMDB (well, LMDB says threads, but maybe my goroutine is spawning new threads). According to wojas:

The above is the detailed content of Mysterious error: 'Go pointer to cgo argument points to unpinned Go pointer'. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template