How to delete TTetheringManager properly?


I am trying to get into app tethering to realize communication between Firemonkey applications but have some problems using it. Especially creating and deleting a TDataModule which contains a TTetheringManager seems not to work as I expect.

I created a new cross-platform Firemonkey app an added a DataModule. Compiler is bcc32c (CLANG C++11). On the DataModule I put a TTetheringManager and a TTetheringAppProfile component and set the property Manager of TetheringAppProfile1 to TetheringManager1.

In the constructor an destructor of the DataModule I do the following:

__fastcall TDataModule1::TDataModule1(TComponent* Owner)
: TDataModule(Owner)

__fastcall TDataModule1::~TDataModule1()

On the main form I placed a Button an added the following code in the OnClick event:

TDataModule1* DataModule = new TDataModule1(NULL);
delete DataModule;

Sometimes deleting DataModule causes an invalid pointer operation in System.pas It seems that there is a thread cancelled which is trying to free some memory:

:7702c54f KERNELBASE.RaiseException + 0x58
:0040E151 System::TObject::FreeInstance(Self=????)
:0040E151 System::TObject::FreeInstance(Self=????)
:004D7F7C System::Classes::ThreadProc(Thread=:0293BA00)
:0040FF1A System::ThreadWrapper(Parameter=:02966B20)
:753e338a kernel32.BaseThreadInitThunk + 0x12
:776d9902 ntdll.RtlInitializeExceptionChain + 0x63
:776d98d5 ntdll.RtlInitializeExceptionChain + 0x36

When I split the creating and deleting to two different button event handlers it works fine, but this is not what I need…

I tried the following things to solve this, without success:

Wait for the OnEndManagersDiscovery event while calling Application->ProcessMessages() before deleting DataModule
Call Sleep(0) before deleting DataModule
Call Sleep(0) in destructor of DataModule
Created similar project with Delphi to exclude C++Builder bug. Same behaviour as in C++ application.

I’m working on this for many hours now but have no idea why this shouldn’t work…


Calling the contructor and destructor from different button OnClick handlers does also not work allways. It depends on how fast the delete button is clicked.

The discovering procedure is done in a seperate thread. I think there is a problem with synchronizing this thread. CancelDiscoverManagers doesn’t seem do finish the discovering process entirely.

Comments are closed.