定義了一個通信動態庫檔案,定義4個介面供主臺呼叫,主臺現在做一個監控程式需要回圈抄讀資料,且不停的呼叫這個動態庫介面InitComm,Sendframe,Closecomm,需要不停的創建和釋放串口,在這個動態庫中使用的是Delphi的第三方控制元件Spcomm,問題的是有時候回圈幾十次出問題,有時候回圈幾百次出問題,都報的是記憶體泄露那里的錯,估計是哪里記憶體沒釋放掉,報錯地方都是CloseComm介面中呼叫FreeAndnil這個函式,請高手賜教!
unit PC_P_Comm;
interface
Uses
SpComm,Windows,Forms,SysUtils,Classes,Dialogs,ScktComp,XMLDoc,XMLIntf,
IdBaseComponent, IdComponent;
type
TBaseComm=Class(TObject) //The Base Comm;
private
procedure AnalyseCommParamXML(Str_XML:string);
function Check1107RecFrameEnd(Str_RecFrame:string):Integer;
function Check62056RecFrameEnd(Str_RecFrame:string):Integer;
procedure Delay(i_Msc:LongInt);
function GetErrorMsg(i_ErrorCode:Integer):string;
public
constructor Create();overload;
destructor Destroy();override;
function InitComm(Str_XML:string):Integer;virtual;abstract;
function SendFrame(Str_SendFrame:string;var Str_RecFrame:string):Integer;virtual;abstract;
function StopComm():Integer;virtual;abstract;
end;
TLocalComm=Class(TBaseComm) //The Class of Port Comm;
private
Comm:TComm;
procedure CommReceiveData(Sender: TObject; Buffer: Pointer;BufferLength: Word);
public
constructor Create();overload;
destructor Destroy();override;
function InitComm(Str_XML:string):Integer;override;
function SendFrame(Str_SendFrame:string;var Str_RecFrame:string):Integer;override;
function StopComm():Integer;override;
end;
var
Gl_Str_CommTypeNo:string;
Gl_Cls_LocalComm:TLocalComm;
//供主臺呼叫的四個介面
function InitComm(P_Str_XML,P_Str_Error:PChar):Integer;stdcall;
function SendFrame(P_Str_SendFrame,P_Str_RecFrame,P_Str_Error:PChar;i_NewBaudrate:Integer=0):Integer;stdcall;
function StopComm():Integer;stdcall;
function CloseComm():Integer;stdcall;
implementation
//Initial the Communication(External Interface)
function InitComm(P_Str_XML,P_Str_Error:PChar):Integer;stdcall;
var
Str_CommTypeNo:string;
begin
//創建物件
Gl_Cls_LocalComm:=TLocalComm.Create;
Result:=Gl_Cls_LocalComm.InitComm(Str_InXML);
if Result<0 then
begin
Str_Error:=Gl_Cls_LocalComm.GetErrorMsg(Result);
StrPCopy(P_Str_Error,Str_Error);
Exit;
end;
end
//Close the communication(External Interface)
function CloseComm():Integer;stdcall;
begin
Result:=0;
if not Assigned(Gl_Cls_LocalComm) then
begin
Result:=-1;
Exit;
end;
//報記憶體泄露錯誤處(釋放記憶體出錯)
FreeAndNil(Gl_Cls_LocalComm);
end;
//-------------------------------Base Comm Class--------------------------------
//Base Creation
constructor TBaseComm.Create();
begin
Inherited Create();
Gl_B_ReceivedEnd:=False;
Gl_B_CheckRecFrameEnd:=False;
end;
//Destroy base class
destructor TBaseComm.Destroy();
begin
Inherited Destroy();
end;
//------------------------------Local Communication-----------------------------
//initialization the communication
constructor TLocalComm.Create();
begin
Inherited Create();
Comm:=TComm.Create(nil);
Comm.OnReceiveData:=CommReceiveData;
Gl_B_CheckRecFrameEnd:=False;
end;
//destory and free the comm
destructor TLocalComm.Destroy();
begin
try
Comm.StopComm;
Comm.Free;
except
end;
Application.ProcessMessages;
Inherited Destroy;
end;
//initialization the comm's prameters
function TLocalComm.InitComm(Str_XML:string):Integer;
begin
Result:=0;
Comm.CommName:='//./'+Gl_Str_CommName;
Comm.BaudRate:=Gl_i_Baudrate;
Comm.ByteSize:=TEumn_ByteSize[Gl_i_ByteSizeIndex];
Comm.StopBits:=TEumn_StopBits[Gl_i_StopBitsIndex];
Comm.Parity:=TEumn_Parity[Gl_i_ParityIndex];
Comm.ReadIntervalTimeout:=Gl_i_ReadIntervalTimeout;
//Start
Comm.Inx_XonXoffFlow:=False;
Comm.Outx_XonXoffFlow:=False;
Comm.StopComm;
try
Comm.StartComm;
Sleep(100);
except
Result:=con_CommERCode_InitCommFailed;
Comm.StopComm;
Exit;
end;
end;
uj5u.com熱心網友回復:
這么長的代碼,你得指明是哪里泄露吧。。uj5u.com熱心網友回復:
請看注釋部分uj5u.com熱心網友回復:
你的Sendframe,Closecomm是不是要做同步處理,要保證Sendframe處理完后再操作Closecomm。uj5u.com熱心網友回復:
看上去好像沒有問題。。uj5u.com熱心網友回復:
貌似看不出來 單步除錯一下,看看在釋放時,物件是不是存在?不存在的話,到底是在哪兒釋放的?uj5u.com熱心網友回復:
把物件的實體化寫在單元的初始化里面.只允許實體化一次.
initialization
Gl_Cls_LocalComm:=TLocalComm.Create;
finalization
if assigned(Gl_Cls_LocalComm) then FreeAndNil(Gl_Cls_LocalComm);
uj5u.com熱心網友回復:
Gl_Cls_LocalComm.Free;不行嘛?轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/143619.html
標籤:網絡通信/分布式開發
下一篇:Win7能安裝delphi7嗎
