0

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)
{
    TetheringManager1->DiscoverManagers();
}
//---------------------------------------------------------------------------

__fastcall TDataModule1::~TDataModule1()
{
    TetheringManager1->CancelDiscoverManagers();
}
//---------------------------------------------------------------------------

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...

Edit

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.

Kerem
  • 429
  • 5
  • 20
  • The Code there is C++. You mean you also tried similarly with Pascal+Delphi and saw same issue? – Warren P Jun 20 '16 at 14:49
  • Yes, the behaviour is also reproducible with Delphi. I edited the post and hope that it is more clear now. – Kerem Jun 20 '16 at 14:58
  • You say putting the destructor on a separate button works. Have you tried putting it on a normally disabled timer, that is enabled on the second line of you code? – Dsm Jun 21 '16 at 08:36
  • This has the same effect as clicking on the "delete" button. If I set the `Interval` of the timer to a lower value (100 ms) the error occurs again. So putting the destructor on a seperate button only works if the the button is not clicked immediately after creaton. I will update my question with this information. – Kerem Jun 21 '16 at 09:03

0 Answers0