2

I'm developing a server-side application using Fluxor and the project now includes six Actions with a corresponding number of Reducers and Effects. At completion I expect there will be 20+ Actions with associated Reducers and Effects.

Fluxor's state management is working well but as I build out the project I've started getting the following InvalidOperationException randomly:

System.InvalidOperationException
  HResult=0x80131509
  Message=DisposableCallback with Id "StateSubscriber.Subscribe" was not disposed.
  Source=Fluxor
  StackTrace:
   at Fluxor.DisposableCallback.Finalize()

The Exception isn't raised within my code - it just pops up. The StackTrace doesn't give me a whole lot to go on and I'm a bit stuck with how I should go about diagnosing this issue and correcting it.

When the Exception occurs my application crashes and then, more often than not, immediately displays the Exception again on start-up. A Rebuild All seems to clear it but I suspect there's a Garbage Collection / Memory Management issue going on.

In my code I'm not explicitly disposing of any Fluxor objects and was expecting that would be handled within the Fluxor Framework. However, the "StateSubscriber.Subscribe" makes me wonder if I should be disposing of Fluxor objects. Can someone give me some best practice guidance on that?

I'm using Fluxor 3.1.1 and see 3.2 has just been released. I'll upgrade to that and see if there's any change. In the meantime any ideas about how to diagnose this issue would be appreciated.

Peter
  • 47
  • 6
  • Without the code we cannot help you – agua from mars Jul 29 '20 at 06:21
  • 1
    The question does provide enough information for help to be provided, the question should not be closed. – Peter Morris Jul 29 '20 at 10:26
  • If this happens in tests it could be because of shared test context. If so, it is fixed by implementing IDisposable and disposing the test context in the Dispose() method. https://xunit.net/docs/shared-context – eharbitz Mar 03 '23 at 12:17

1 Answers1

1

FluxorComponent automatically subscribes to state by overriding OnInitialized.

The main culprit of this error is calling the wrong base method when overriding a method, or not calling base when overriding Dispose; for other reasons see: https://github.com/mrpmorris/Fluxor/blob/master/Docs/disposable-callback-not-disposed.md

Neil
  • 7,482
  • 6
  • 50
  • 56
Peter Morris
  • 20,174
  • 9
  • 81
  • 146
  • The only place I explicitly subscribe to a state is in my Index page's "protected override async Task OnAfterRenderAsync(bool firstRender)" event. When firstRender is True, I subscribe to state's StateChanged event. Is that not necessary? I'm not overriding Dispose anywhere. – Peter Jul 29 '20 at 09:27
  • 1
    Descend from FluxorComponent and you don't need to subscribe to any state manually at all, it will all subscribe / unsubscribe automatically. I would say your existing problem is that you are subscribing in OnAfterRenderAsync and never unsubscribing. – Peter Morris Jul 29 '20 at 10:25
  • 1
    Apologies for the delay @Peter. Yes, the problem is resolved now thanks. You were correct in that I wasn't unsubscribing and my code was a bit ... "tangled" :) – Peter Aug 19 '20 at 21:33
  • In my case it was caused by Mismatch Between Async/Sync Initialization methods; i.e. if you use `OnInitializedAsync` you should call `base.OnInitializedAsync()` and not the `base.OnInitialized()`. – Neil May 12 '21 at 05:33