- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 CFFI 生成 DLL:
import cffi
ffibuilder = cffi.FFI()
ffibuilder.embedding_api('''
char* get_string();
''')
ffibuilder.set_source('my_plugin', '')
ffibuilder.embedding_init_code('''
from my_plugin import ffi, lib
@ffi.def_extern()
def get_string():
val = "string"
return lib.strdup(val.encode())
''')
ffibuilder.cdef('''
char *strdup(const char *);
''')
ffibuilder.compile(target='my-plugin.*', verbose=True)
我通过运行之前的脚本生成 DLL。现在,我创建了这个 C++ 代码示例来使用我的 DLL:
#include <iostream>
#include <windows.h>
typedef char* (__stdcall *get_string_t)();
int main()
{
HINSTANCE hGetProcIDDLL = LoadLibrary("my-plugin.dll");
if (!hGetProcIDDLL) {
std::cout << "could not load the dynamic library" << std::endl;
return -1;
}
get_string_t get_string = (get_string_t)GetProcAddress(hGetProcIDDLL, "get_string");
if (!get_string) {
std::cout << "could not locate the function" << std::endl;
return -1;
}
char* val = get_string();
std::cout << "Value = " << val << std::endl;
free(val); // Crash !
std::cout << "End" << std::endl;
return 0;
}
我使用 Visual Studio 2010 的编译器进行编译,当我运行我的应用程序时,它在 free
指令期间崩溃:
> cl get_string.cpp
Compilateur d'optimisation Microsoft (R) 32 bits C/C++ version 16.00.40219.01 pour 80x86
Copyright (C) Microsoft Corporation. Tous droits réservés.
get_string.cpp
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xlocale(323) : warning C4530: Gestionnaire d'exceptions C++ utilisé, mais les sémantiques de déroulement n'ont pas été activées. Spécifiez /EHsc
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:get_string.exe
get_string.obj
> get_string.exe
Value = string
我遵循 this answer 中给出的指示.我应该怎么做才能释放内存并避免我的应用程序崩溃?事实上,如果我删除 free
指令,我的应用程序运行良好,但它不是一个干净的解决方案。
最佳答案
在一个地方分配然后跨 DLL 边界释放是一种危险的做法。除非您知道自己做对了(相同的 CRT 版本等),否则请避免使用它。 Thus spake Microsoft :
When you pass C Run-time (CRT) objects such as file handles, locales, and environment variables into or out of a DLL (function calls across the DLL boundary), unexpected behavior can occur if the DLL, as well as the files calling into the DLL, use different copies of the CRT libraries.
A related problem can occur when you allocate memory (either explicitly with new or malloc, or implicitly with strdup, strstreambuf::str, and so on) and then pass a pointer across a DLL boundary to be freed. This can cause a memory access violation or heap corruption if the DLL and its users use different copies of the CRT libraries.
一个解决方案是从你的 DLL 中公开一个 free
函数,它正在转换一个分配的对象,这样客户端就可以调用你的 free
函数,或者在C++,您可以使用带有自定义删除器的智能指针来正确执行此操作。
关于python - 当我释放由 CFFI 生成的 DLL 分配的 char* 时,为什么我的应用程序会崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55265547/
好的,我尝试了这种从外国翻译过来的方法,它确实有效我在我的库中的 structs.lisp 文件中定义了这些,它在我所有其他依赖项之前首先加载 (cffi:defcstruct (cv-size :c
我正在尝试使用来自 SBCL 的 BLAS/LAPACK 库(特别是试图让 LLA 包运行)。我在加载 BLAS 共享库时遇到了很多麻烦;最终我发现它无法加载其依赖库。最终我能够通过手动加载它的所有依
有一种方法可以用字典初始化结构: fooData= {'y': 1, 'x': 2} fooStruct = ffi.new("foo_t*", fooData) fooBuffer = ffi.bu
我想使用 stat(2)来自 Common Lisp。 我已经定义了 stat 函数使用的结构: (cffi:defctype mode_t :unsigned-int) (cffi:defctype
在普通的 lisp 中,是否有推荐的外来类型的命名约定? (和一般类型)?例如: (cffi:defctype glyph-index-t :uint32) (cffi:defcstruct Poin
假设我在 cffi 下定义了一个枚举: from cffi import FFI ffi = FFI() ffi.cdef('typedef enum {RANDOM, IMMEDIATE, SEAR
IplImage struct documentation描述了 IplROI* roi 槽,它似乎是指向核心 types_c.h 头文件中定义的 IplROI struct 的指针: typede
我使用 cffi 模块来包装一个简单的 C 代码。问题是,我需要添加一个标志以使其编译 (std=c99)。目前我有类似的东西: from cffi import FFI ffibuilder = F
这是一个 C 语言的函数: union Example { int number; void *pointer; }; void return_a_value (union Examp
我是 Common Lisp 的新手,并对其进行了一些实验。我正在努力获得对 Windows 剪贴板的一些访问权限,然后我找到了这个引用: https://groups.google.com/foru
我实际上正在编写一个带有 cffi 模块的 python 程序来测试我的 C/ASM 库,我设法让它工作。但我不知道如何访问字符串中间字符的地址以将其传递给我的 lib 函数。例如: def my_b
我的最终目标是编译系统 FLEXI-TRIVIAL-DIRED ( http://common-lisp.net/project/ftd/ ),但我遇到了麻烦,因为我在任何地方都找不到所需的软件包之一
如果我使用 cffi.FFI.new 创建一个新结构,如何将其传递给具有相同结构定义的不同 FFI 中的函数? 我有一个基本的 C 结构,我通过 cffi 包在 Python 中使用它,我想将其传递给
我正在尝试使用 pip3 在 fedora23 工作站上安装 cffi。但我收到以下错误。 pip3 install cffi Collecting cffi Using cached cffi-
我想使用 cffi(如果必须,甚至使用 ctypes)从 Linux 上的 Python 3 访问 C ABI。该 API 由许多 .so 文件实现(我们称它们为 libA.so、libB.so 和
我想公开一个对象的缓冲区协议(protocol),就像in this example Cython 文档,但是我需要使用 CFFI 来执行此操作而且我找不到任何公开缓冲区协议(protocol)的示例
我正在学习如何将用 C 编写的代码包含到 Python 中,因为我有一个用于 Microchip 设备的 API,这对于我希望通过为其添加一个 Python 包装器来让我将来的生活更轻松,这将使我能够
我的C++代码是 StructureEx* obj; // structure functionEx((void**)&obj); 我的职能是 int functionEx(void** obj);
尝试在我的 linux 机器上的 python 3.4.3 上安装 cffi-1.7.0 源代码。出现以下错误。 $ python setup.py 安装 cc1:错误:无法识别的命令行选项“-Wno
我有一些 C 库,我想使用 CFFI 在 Python 中访问。构建库后,我得到了 2 个文件:$HOME/libcint/include/cint.h 和 $HOME/libcint/lib/lib
我是一名优秀的程序员,十分优秀!