Delphi的"Invalid pointer operation"異常的解決辦法 收藏
今天用Delphi編寫(xiě)了個(gè)dll,用到了TStringList來(lái)傳遞多參數(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;
然而在應(yīng)用程序中調(diào)用的時(shí)候,卻出了點(diǎn)問(wèn)題:整個(gè)運(yùn)行過(guò)程是有效的,并且參數(shù)也都傳遞成功,但是當(dāng)結(jié)束了該API函數(shù)執(zhí)行后每次都會(huì)彈出“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)遇到同樣問(wèn)題的同志也不在少數(shù)。順藤摸瓜,終于在Delphi聯(lián)機(jī)幫助里找到了下面的描述:
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中如果一個(gè)動(dòng)態(tài)庫(kù)導(dǎo)出函數(shù)以參數(shù)形式傳遞或者返回了一個(gè)長(zhǎng)字符串或者動(dòng)態(tài)數(shù)組(無(wú)論是直接傳遞還是包含在一個(gè)對(duì)象中),那么動(dòng)態(tài)庫(kù)以及其調(diào)用程序都必須得引用ShareMem單元。同樣,如果程序或者庫(kù)通過(guò)New或者GetMem申請(qǐng)了內(nèi)存,并且該內(nèi)存在另一個(gè)模塊中通過(guò)Dispose或FreeMem釋放,也得這么做。務(wù)必把ShareMem放在程序或者庫(kù)所有引用單元之首。
ShareMem是BORLANDMM.dll內(nèi)存管理單元接口,它允許模塊間共享動(dòng)態(tài)申請(qǐng)內(nèi)存。BORLANDMM.dll必須與使用了改單元的程序或者Dll一塊兒部署。當(dāng)程序或者動(dòng)態(tài)庫(kù)使用了ShareMem,其內(nèi)存管理將會(huì)由BORLANDMM.dll接管。
注意:
Linux下使用glibc的malloc來(lái)管理共享內(nèi)存?!?/div>
上述問(wèn)題僅在windows里才有啊,呵呵,于是在dll里把Uses子句的top 1增加ShareMem引用,另外在應(yīng)用程序的Project域的Uses子句第一個(gè)引用也加上ShareMem。最后編譯,運(yùn)行,沒(méi)有看到哪個(gè)煩人的異常提示了:P
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。