Delphi的"Invalid pointer operation"異常的解決辦法 收藏
今天用Delphi編寫了個dll,用到了TStringList來傳遞多參數(shù),如下: view plaincopy to clipboardprint?
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; 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; 然而在應用程序中調(diào)用的時候,卻出了點問題:整個運行過程是有效的,并且參數(shù)也都傳遞成功,但是當結束了該API函數(shù)執(zhí)行后每次都會彈出“Invalid pointer operation”的異常。調(diào)用代碼如下:
view plaincopy to clipboardprint?
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; 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發(fā)現(xiàn)遇到同樣問題的同志也不在少數(shù)。順藤摸瓜,終于在Delphi聯(lián)機幫助里找到了下面的描述:
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中如果一個動態(tài)庫導出函數(shù)以參數(shù)形式傳遞或者返回了一個長字符串或者動態(tài)數(shù)組(無論是直接傳遞還是包含在一個對象中),那么動態(tài)庫以及其調(diào)用程序都必須得引用ShareMem單元。同樣,如果程序或者庫通過New或者GetMem申請了內(nèi)存,并且該內(nèi)存在另一個模塊中通過Dispose或FreeMem釋放,也得這么做。務必把ShareMem放在程序或者庫所有引用單元之首。
ShareMem是BORLANDMM.dll內(nèi)存管理單元接口,它允許模塊間共享動態(tài)申請內(nèi)存。BORLANDMM.dll必須與使用了改單元的程序或者Dll一塊兒部署。當程序或者動態(tài)庫使用了ShareMem,其內(nèi)存管理將會由BORLANDMM.dll接管。
注意: Linux下使用glibc的malloc來管理共享內(nèi)存?!?/div> 上述問題僅在windows里才有啊,呵呵,于是在dll里把Uses子句的top 1增加ShareMem引用,另外在應用程序的Project域的Uses子句第一個引用也加上ShareMem。最后編譯,運行,沒有看到哪個煩人的異常提示了:P
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/derryzhang/archive/2009/08/05/4412436.aspx
|
|