Home > Back-end >  Memory leak problem.
Memory leak problem.

Time:10-22

Dynamic library file defines a communication, defines four interfaces for the master station call, master now read cycle copy is needed to do a monitor data, and constantly invoke the dynamic library interface InitComm, Sendframe, Closecomm, need to constantly create and release the serial port, used in the dynamic library is the third party controls Spcomm Delphi, problems are sometimes dozens of times a problem, sometimes hundreds of times a problem, is there is memory leak fault, estimation is where memory didn't release, an error is Closecomm FreeAndnil interface call this function, please grant instruction!
The unit PC_P_Comm;

Interface

Uses the
SpComm, Windows, Forms, SysUtils, Classes, Dialogs, ScktComp, XMLDoc, XMLIntf,
IdBaseComponent IdComponent;
Type
TBaseComm=Class (TObject)//The Base Comm.
Private
Procedure AnalyseCommParamXML Str_XML: (string);
The function Check1107RecFrameEnd (Str_RecFrame: string) : Integer;
The function Check62056RecFrameEnd (Str_RecFrame: string) : Integer;
Procedure Delay (i_Msc: LongInt);
The function GetErrorMsg (i_ErrorCode: Integer) : string;
Public
The constructor the Create (); The phrase ";
Destructor Destroy (); Override.
The function InitComm (Str_XML: string) : Integer; Virtual; abstract;
The function SendFrame (Str_SendFrame: string; Var Str_RecFrame: string) : Integer; Virtual; abstract;
The function StopComm () : Integer; Virtual; abstract;
end;

TLocalComm=Class (TBaseComm)//The Class of The Port Comm.
Private
Comm: TComm;
Procedure CommReceiveData (Sender: TObject; Buffer: Pointer; BufferLength: Word);
Public
The constructor the Create (); The phrase ";
Destructor Destroy (); Override.
The function InitComm (Str_XML: string) : Integer; Override.
The function SendFrame (Str_SendFrame: string; Var Str_RecFrame: string) : Integer; Override.
The function StopComm () : Integer; Override.
end;
Var
Gl_Str_CommTypeNo: string;
Gl_Cls_LocalComm: TLocalComm;
//for four master station call interface
The function InitComm (P_Str_XML, P_Str_Error: PChar) : Integer; Stdcall;
The function SendFrame (P_Str_SendFrame, P_Str_RecFrame P_Str_Error: PChar; I_NewBaudrate: Integer=0) : Integer; Stdcall;
The function StopComm () : Integer; Stdcall;
The function CloseComm () : Integer; Stdcall;

Implementation

//Initial the Communication (External Interface)
The function InitComm (P_Str_XML, P_Str_Error: PChar) : Integer; Stdcall;
Var
Str_CommTypeNo: string;
The begin
//create the object
Gl_Cls_LocalComm:=TLocalComm. Create;
Result:=Gl_Cls_LocalComm InitComm (Str_InXML);
If Result<0 then
The begin
Str_Error:=Gl_Cls_LocalComm GetErrorMsg (Result);
StrPCopy (P_Str_Error Str_Error);
The Exit;
end;
End
//Close the communication (External Interface)
The function CloseComm () : Integer; Stdcall;
The begin
Result:=0;
If not Assigned (Gl_Cls_LocalComm) then
The begin
Result:=1;
The Exit;
end;
//the memory leak error (error free memory)
FreeAndNil (Gl_Cls_LocalComm);
end;
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Base Comm Class -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//Base Creation
The constructor TBaseComm. The Create ();
The begin
Inherited the Create ();
Gl_B_ReceivedEnd:=False;
Gl_B_CheckRecFrameEnd:=False;
end;
//Destroy the base class
Destructor TBaseComm. Destroy ();
The begin
Inherited the Destroy ();
end;
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the Local Communication -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//initialization the communication
The constructor TLocalComm. The Create ();
The begin
Inherited the Create ();
Comm:=TComm. Create (nil);
Comm. OnReceiveData:=CommReceiveData;
Gl_B_CheckRecFrameEnd:=False;
end;
//destory the and free the comm
Destructor TLocalComm. Destroy ();
The begin
Try
Comm. StopComm;
Comm. Free;
Except,
end;
Application. ProcessMessages;
Inherited Destroy;
end;
//initialization the comm 's prameters
The function TLocalComm. InitComm (Str_XML: string) : Integer;
The begin
Result:=0;
Comm.Com mName:='//./+ 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;
The Exit;
end;
end;

CodePudding user response:

Such a long code, you have to specify where is leak,,

CodePudding user response:

Please see comments section

CodePudding user response:

Is your Sendframe Closecomm do synchronous processing, to ensure the Sendframe treatment after operation Closecomm again,

CodePudding user response:

Looks like there is no problem,,

CodePudding user response:

Seems to look not to come out step through it and see at the time of release, objects exist? It doesn't exist, where exactly is released?

CodePudding user response:

Write object instantiated in unit initialization.
Only allowed to instantiate a.

Initialization
Gl_Cls_LocalComm:=TLocalComm. Create;

Finalization
If assigned (Gl_Cls_LocalComm) then FreeAndNil (Gl_Cls_LocalComm);
nullnull
  • Related