Dispose is called multiple times when unregister service.

SyntaxEditor for WPF Forum

Posted 2 months ago by Sunshine - Appeon
Version: 21.1.2
Avatar

I implemented a diagnostic tagger class by inheriting CollectionTagger<ISquiggleTag> and IDisposable.

Then register it through syntaxEditor.Document.Language.RegisterService(tagger).

In the obtained service, I can see that my tagger is only registered once.

But when I unregister service, tagger.Dispose() will be called twice. Is this normal?

This is the code to unregister service:

var types = syntaxEditor.Document.Language.GetAllServiceTypes();

foreach (var type in types)
{
    syntaxEditor.Document.Language.UnregisterService(type);
}

It will be called more times in the actual project, I don’t quite understand what caused it.

Comments (7)

Posted 2 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

Thanks for writing.  We are investigating the behavior of this and will get back to you.  It may take several extra days since some of our SyntaxEditor development staff is on vacation.

In the meantime, can you give us more information on what you are doing in your tagger that you want to dispose?  Also please tell us when you would ideally like to see the tagger dispose itself.


Actipro Software Support

Posted 2 months ago by Sunshine - Appeon
Avatar

I have registered multiple CodeDocumentTaggerProvider or TextViewTaggerProvider. Dispose() will be called many times when unregister service, and there is no rule to deduce it for the time being.

Looking forward to your reply.

Posted 2 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

It's basically doing a tag aggregator refresh each time a language service is added/removed.  Part of that refresh process is removing taggers and then reacquiring new ones, in case a new tagger should be added to the aggregator or one should no longer be included.  The bug is that when it's removing the taggers, it's calling Dispose on them if they are IDisposable.  

The questions in our previous reply that we'd still like you to answer are:

  • Why are you implementing IDisposable on your tagger, and what specifically are you doing in the tagger's Dispose method?  We're trying to learn more about the scenario so that we can best decide how to alter the current logic.
  • When would you ideally like to see the tagger dispose itself?


Actipro Software Support

Posted 2 months ago by Sunshine - Appeon
Avatar

(1)I registered some events on tagger, such as IEditorView.TextAreaLayout. Then I need to unregister the event when closing the Editor, otherwise it may cause some memory leaks.

(2)I hope that when the Editor is closed, all services can be recycled normally.

At present, we found that opening too many Editors will cause memory leaks.

This may be caused by the fact that we subscribed to some events but were not released

Posted 2 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

All taggers have a Close method that is called when they should close, either from a SyntaxEditor view being closed or the document's syntax language being changed.  TaggerBase has a virtual OnClosed method it calls from its Close method implementation.  For cleaning up event ties, you should override OnClosed instead of using Dispose.

If you do that then you should not really have memory leaks.  And to be extra sure, set your document.Language property to SyntaxLanguage.PlainText when you are totally done using its SyntaxEditor instance.


Actipro Software Support

Posted 2 months ago by Sunshine - Appeon
Avatar

It means that Tagger's Close() needs to be called manually, right?
There is another question. When is the SyntaxEditor.ViewClosed event triggered? I can't find a way to close the view.

Posted 2 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

As long as your tagger called its TaggerBase base class with the parameter isForLanguage set to true, it will get its Close() method called automatically if you do this line at the time you no longer use your editor:

editor.Document.Language = SyntaxLanguage.PlainText;

When isForLanguage is true, the tagger watches for the document's syntax language to be changed and if it does change, the tagger knows to close itself.  Therefore, putting your cleanup code in an OnClosed() override should work.

[Modified 2 months ago]


Actipro Software Support

The latest build of this product (v21.1.3) was released 16 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.