Delphi的"Invalid pointer operation"异常的解决办法

    今天用Delphi编写了个dll,用到了TStringList来传递多参数,如下:

   

    然而在应用程序中调用的时候,却出了点问题:整个运行过程是有效的,并且参数也都传递成功,但是当结束了该API函数执行后每次都会弹出“Invalid pointer operation”的异常。调用代码如下:

   

    在google里搜索Invalid pointer value发现遇到同样问题的同志也不在少数。顺藤摸瓜,终于在Delphi联机帮助里找到了下面的描述:

    On Windows, if a DLL exports routines that pass long strings or dynamic arrays as parameters or function results (whether directly or nested in records or objects), then the DLL and its client applications (or DLLs) must all use the ShareMem unit. The same is true if one application or DLL allocates memory with New or GetMem which is deallocated by a call to Dispose or FreeMem in another module. ShareMem should always be the first unit listed in any program or library uses clause where it occurs.

ShareMem is the interface unit for the BORLANDMM.DLL memory manager, which allows modules to share dynamically allocated memory. BORLANDMM.DLL must be deployed with applications and DLLs that use ShareMem. When an application or DLL uses ShareMem, its memory manager is replaced by the memory manager in BORLANDMM.DLL.

Note

Linux uses glibc's malloc to manage shared memory.

    【2010-6-3:翻译一下,供大家参考:

    Windows中如果一个动态库导出函数以参数形式传递或者返回了一个长字符串或者动态数组(无论是直接传递还是包含在一个对象中),那么动态库以及其调用程序都必须得引用ShareMem单元。同样,如果程序或者库通过New或者GetMem申请了内存,并且该内存在另一个模块中通过Dispose或FreeMem释放,也得这么做。务必把ShareMem放在程序或者库所有引用单元之首。

ShareMem是BORLANDMM.dll内存管理单元接口,它允许模块间共享动态申请内存。BORLANDMM.dll必须与使用了改单元的程序或者Dll一块儿部署。当程序或者动态库使用了ShareMem,其内存管理将会由BORLANDMM.dll接管。

注意:
Linux下使用glibc的malloc来管理共享内存
。】

    上述问题仅在windows里才有啊,呵呵,于是在dll里把Uses子句的top 1增加ShareMem引用,另外在应用程序的Project域的Uses子句第一个引用也加上ShareMem。最后编译,运行,没有看到哪个烦人的异常提示了:P

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 18
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值