Delphi的"Invalid pointer operation"异常的解决办法
2009-08-05 15:43
656 查看
今天用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
function DBM_SetParam(procName: string; paramValues: TStringList = nil): Boolean;stdcall; var i: integer; begin try if (paramValues<>nil) then begin for i := 0 to paramValues.Count - 1 do begin GParameters.AddParameter; GParameters[i].Value := paramValues[i]; end; end; Result := True; except on e: Exception do begin Result := False; end; end; end;
然而在应用程序中调用的时候,却出了点问题:整个运行过程是有效的,并且参数也都传递成功,但是当结束了该API函数执行后每次都会弹出“Invalid pointer operation”的异常。调用代码如下:
procedure TForm1.Button3Click(Sender: TObject); var paramList: TStringList; begin paramList := TStringList.Create; paramList.Add('2'); if (not DBM_SetParam('GetUserName', paramList)) then begin ShowMessage('Set parameters failed!'); end; end;
在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
相关文章推荐
- 【转】Delphi的"Invalid pointer operation"异常的解决办法
- Delphi的"Invalid pointer operation"异常的解决办法 (转)
- Delphi的"Invalid pointer operation"异常的解决办法
- “Invalid pointer operation"异常的解决办法
- WIN2003+ROSE 2003的"MEM_BAD_POINTER"错误的解决办法
- DataTable表添加新行时,弹出--ID列不能为""-- 异常的解决办法
- "无法启动应用程序,工作组信息文件丢失,或是已被其他用户已独占方式打开"解决办法
- 向ACCESS中的"时间/日期"字段中插入DateTime.Now时出现“标准表达式中数据类型不匹配。”错误的解决办法
- "performSelector may cause a leak because its selector is unknown"警告原因及其解决办法
- 如何解决asp.net中使用webbrowser是出现的"无法实例化ActiveX 控件"的异常"
- "A duplicate insert block exists for class......"的解决办法
- "library not found for - "解决办法
- "library not found for - "解决办法
- "error while loading shared libraries: xxx.so.x"错误的原因和解决办法
- "error while loading shared libraries: xxx.so.x" 错误的原因和解决办法
- Oracle10g安装过程中"无法确定主机的IP地址时产生该异常错误" :解决办法(转)
- iphone"此证书是由未知颁发机构签名的"的解决办法
- "空格在每个浏览器中显示的距离都不一样"的解决办法
- 复制DataTable时出现"该行已经属于另一个表"异常的解决办法
- Oracle10g安装过程中"无法确定主机的IP地址时产生该异常错误" :解决办法