Matlab 与 C# 混合编程中,如果使用 C# 提供的 Marshal.AllocHGlobal 和 Marshal.FreeHGlobal,则会出现如下的错误提示:
Abnormal termination at .\src\mem\alignment.cpp line 333:
The pointer passed to 'vector_check' is invalid
and does not appear to have come from any of the following routines:
vector_malloc, vector_calloc, vector_realloc
mxMalloc*, mxCalloc*, mxRealloc*
This suggests one of the following has happened:
- the pointer has already been freed
- the pointer came from an incompatible allocator (e.g. new, malloc, utMalloc)
- the pointer didn't come from any allocator (e.g. the stack, uninitialized me
mory)
- a memory corruption destroyed the pointer or its header
错误原因在于第二点,内存分配器不兼容,改用 Matlab 提供的 mxMalloc 和 mxFree 就可以了,但是 C# 的复制函数 Marshal.Copy 仍然可用。
Matlab 与 C# 混合编程中分配内存和释放内存都必须使用 Matlab 提供的函数 mxMalloc 和 mxFree。
代码根据 《 MATLAB C# Book》 改写,但是有些小问题,特此记录。
阅读(2348) | 评论(0) | 转发(0) |