Jump to content
Chris Pim

Help needed - ANR on Android app UI refresh

Recommended Posts

Hello all,

We've just deployed an update to our FMX app on the Play Store, but we're seeing a worrying number of ANRs reported in the Play Store vitals area. We've changed a lot in this latest update including much heavier use of styles from style books. We also can't reproduce the ANRs on any of our 10 Android test devices as it's only impacting < 1% of our user case (around 600 instances from 200,000 installs).

I have a clear call stack (below) which is showing that the ANR is a block on a monitor when trying to apply the styles to my UI elements, loading the resource specifically.

Does anyone have any ideas of how this could theoretically be caused?

 

We do use a lot of threads so my first thing to check was that we weren't trying to update the UI from a thread without Synchronize or Queue but I can't find any case where we aren't.

 

Any suggestions for what I could look for would be very welcome!

 

Thanks

 

Call stack:

(System::Sysutils::WaitForSyncWaitObj(void*, unsigned int)+100)
(System::Sysutils::WaitOrSignalObj(void*, void*, unsigned int)+96)
(System::TMonitor::Enter(unsigned int)+528)
(System::Rtti::TPoolToken::TPoolToken()+120)
(System::Rtti::EnsurePoolToken(System::DelphiInterface<System::IInterface>*)::DoCreate(void*)+52)
(System::Rtti::EnsurePoolToken(System::DelphiInterface<System::IInterface>*)+32)
(System::Rtti::TRttiContext::KeepContext()+104)
(System::Classes::BeginGlobalLoading()+20)
(Fmx::Controls::TStyleContainer::LoadStyleResource(System::Classes::TStream*)+40)
(Fmx::Controls::TStyleContainer::CreateStyleResource(System::UnicodeString)+92)
(Fmx::Controls::TStyleContainer::FindStyleResource(System::UnicodeString, bool)+36)
(Fmx::Controls::TStyledControl::LookupStyleObject(...)::LookupStyleInObject(void*, Fmx::Types::TFmxObject*, System::UnicodeString)+88)
(Fmx::Controls::TStyledControl::LookupStyleObject(...)::LookupStyle(void*, Fmx::Types::TFmxObject*)+84)
(Fmx::Controls::TStyledControl::LookupStyleObject(...)+336)
(Fmx::Controls::TStyledControl::GetStyleObject(bool)+236)
(Fmx::Controls::TStyledControl::GetStyleObject()+28)
(Fmx::Controls::TStyledControl::ApplyStyleLookup()+124)
(Fmx::Controls::Presentation::TPresentedControl::ApplyStyleLookup()+52)
(Fmx::Controls::TStyledControl::PrepareForPaint()+32)
(Fmx::Controls::TControl::PrepareForPaint()+656)
(Fmx::Forms::TCustomForm::PrepareForPaint()+504)
(Fmx::Forms::TCustomForm::PaintRects(System::Types::TRectF const*, int)+76)
(Fmx::Platform::Ui::Android::TFormRender::Render()+124)
(Fmx::Platform::Ui::Android::TAndroidWindowHandle::TSurfaceViewListener::surfaceChanged(...)+56)
(System::Rtti::Invoke(void*, System::DynamicArray<System::Rtti::TValue>, System::Typinfo::TCallConv, System::Typinfo::TTypeInfo*, bool, bool)+556)
(System::Rtti::TRttiInstanceMethodEx::DispatchInvoke(System::Rtti::TValue const&, System::Rtti::TValue const*, int)+1328)
(System::Rtti::TRttiMethod::Invoke(System::TObject*, System::Rtti::TValue const*, int)+84)
(Androidapi::Jnibridge::dispatchToNative2(...)+692)
at com.embarcadero.rtl.ProxyInterface.dispatchToNative2 (Native method)

Share this post


Link to post

RTTI had (and possibly still has) issues with thread safety when acquiring and releasing pool token. 

 

Unless you are using Windows application with dynamically loaded packages, the best option to avoid those issues is to call TRttiContext.KeepContext; in initialization section of some base unit and TRttiContext.DropContext; in finalization section. This will create pool token and keep it alive during lifetime of you application, avoiding thread safety issues, and it will also speed up RTTI operations. 

  • Like 3
  • Thanks 1

Share this post


Link to post

Thanks Dalija, that's very interesting solution I hadn't considered. I'll implement this and see what happens.

Share this post


Link to post

Sorry for the delay in responding Dalija, I added the KeepContext and the exception disappeared. Thank you for your help 😁

Share this post


Link to post
1 hour ago, Chris Pim said:

Sorry for the delay in responding Dalija, I added the KeepContext and the exception disappeared.

No worries. I am glad you were able to resolve the issue.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×