| View previous topic :: View next topic |
| Author |
Message |
Bo Berglund Guest
|
Posted: Sat Sep 27, 2003 1:47 pm Post subject: Why acces violation at end of program using TServerSocket?? |
|
|
I am trying to write a socket server for remote usage of local RS232
ports. So I have created a server class that creates a TServerSocket
in the constructor and then I start listening on a separate Start
call.
My problem is that although the basic setup works I am always getting
an access violation when I close my program. Why does this happen?
If I execute the Start method and then the Stop method but there is no
connection done (and there is no handler created) I still get a
violation at program end.
If I just start then exit my program then there is no error. It
appears that doing a TServerSocket.Open sets the system up for access
violation....
Even though I am doing TServerSocket.Close (in the Stop method) I
still get the violation.
Any help appreciated!
/Bo
Below is a shortened version of the class code:
TPortServer = class(TObject)
private
FOwner: TObject;
FServerSocket: TServerSocket;
procedure OnClientConnect(Sender: TObject; Socket:
TCustomWinSocket);
procedure AddPortHandler(Socket: TCustomWinSocket);
procedure SetPort(Port: integer);
public
constructor Create;
destructor Destroy; override;
property ServerPort: integer read FServerPort write SetPort;
function Start: Boolean;
function Stop: Boolean;
end;
constructor TPortServer.Create;
begin
FServerSocket := TServerSocket.Create(NIL);
FServerSocket.OnClientConnect := OnClientConnect;
FServerPort := PORTSERVER_PORT;
end;
destructor TPortServer.Destroy;
begin
if Assigned(FServerSocket) then
begin
if FServerSocket.Active then
FServerSocket.Active := false;
FServerSocket.Free;
end;
inherited;
end;
function TPortServer.Start: Boolean;
begin
if FServerSocket.Active then
Result := true
else
begin
try
FServerSocket.Port := FServerPort;
FServerSocket.Open;
Result:= True;
except
on E: Exception do
begin
LogError('PortServer - Exception on start: ' + E.Message);
Result:= False;
end;
end;
end;
end;
function TPortServer.Stop: Boolean;
begin
try
FServerSocket.Close;
while FServerSocket.Active do
Application.ProcessMessages;
Result:= True;
except
on E: Exception do
begin
LogError('PortServer - Exception on stop: ' + E.Message);
Result:= False;
end;
end;
end;
procedure TPortServer.OnClientConnect(Sender: TObject; Socket:
TCustomWinSocket);
var
PortHandl: TPortHandler;
begin
AddPortHandler(Socket);
end;
|
|
| Back to top |
|
 |
Bo Berglund Guest
|
Posted: Sun Sep 28, 2003 7:34 am Post subject: Re: Why acces violation at end of program using TServerSocke |
|
|
Disregard post!
I fixed it by inheriting from TComponent instead of TObject.
And I had a buggy Registry read there too.
Sorry for wasting bandwidth!
/Bo
|
|
| Back to top |
|
 |
|