I am programming a multi-threaded chat server application. Thus critical section are very necessary. I just made a wrapper class for threaded tree view. It works perfectly in the client but the server raises an AV.
There are two forms, FormServer (which practically does almost no processing except for gui painting) and the DataModuleServer which does all the heavy lifting.
The FormServer gets created first.
Actually the threaded tree view is inside another wrapper class TRoomTree.
TDataModuleServer.Create
procedure TDataModuleServer.cr(Sender: TObject);
begin
Rooms := TRoomTree.Create (FormServer.tvRooms);
tvRooms : the visual treeview
TRoomTree.Create
constructor TRoomTree.Create (TV : TTreeView);
begin
if Assigned (TV) then
fTreeView.Create (TV)
else
exit;
fTreeView : the threaded tree view;
TThreadTreeView.Create
constructor TThreadTreeView.Create (TreeView : TTreeView = nil);
begin
fLock := TCriticalSection.Create;
if Assigned (TreeView) then
fTreeView := TreeView
else
fTreeView := TTreeView.Create (nil);
end;
fTreeView : the normal tree view;
Now the TCriticalSection.Create raises an AV exception in ntdll.dll.
Call Stack
:76cac41f KERNELBASE.RaiseException + 0x58
:0040469c NotifyNonDelphiException + $1C
:77ecb42b ; ntdll.dll
uThreadTreeView.TThreadTreeView.Create($26B4300)
uRoomTree.TRoomTree.Create($26B4300)
uServer.TDataModuleServer.cr($26A48B0)
Classes.TDataModule.DoCreate
Classes.TDataModule.AfterConstruction
System.@AfterConstruction($26A48B0)
Classes.TDataModule.Create(???)
Forms.TApplication.CreateForm(???,(no value))
PlayburnServer.PlayburnServer
:773833aa kernel32.BaseThreadInitThunk + 0x12
:77ea9ef2 ntdll.RtlInitializeExceptionChain + 0x63
:77ea9ec5 ntdll.RtlInitializeExceptionChain + 0x36
Any help will be greatly appreciated.
In
TRoomTree.Create, this line of codeis responsible for your access violation. That’s because
fTreeViewhas not been initialised before you attempt to call a method on it.Delphi instances are created like this:
I’m not sure about your threading model, but I trust you are aware that VCL controls must only be accessed from the GUI thread.