When and where to call RemoveHandler in VB.NET?

I am working on a VB.NET windows forms project in .NET 1.1. And I have this type of architecture is very simplified.

Public MustInherit Class BaseTestLogic Private _TimerPoll As Timer Public Sub New(ByVal sym As SymbolFileMng, ByVal cfg As LampTestConfig, ByVal daas As DaasManager, ByVal mcf As Elux.Wg.Lpd.MCFs.VMCF) AddHandler _TimerPoll.Tick, AddressOf TimerPoll_Tick End Sub End Class Public Class SpecificTestLogic Inherits BaseTestLogic End Class 

Depending on the type of test I am doing, I create an instance of a specific test derived from BaseTestLogic . But I found that after hundreds of object creation, I might have a StackOverflow exception.

I checked my code and saw that I forgot to remove the handler in Timer Tick. The question is where and when to remove hadler correctly?

Do I need to implement the IDisposable interface in the base class and RemoveHandler in Dispose ?

+1
source share
5 answers

You can continue to remove the handler when calling Dispose, but the purist will say that "you should not abuse IDisposable for purposes other than disposing of unmanaged resources."

Another option is to remove the handler using the Finalize method.

You may also feel comfortable removing the handler in several different places, if that makes sense in your design. Removing an already deleted handler will not cause any problems - if the event is not a custom event, and its AddHandler / RemoveHandler implementations do not correspond to the behavior of non-standard events (this is just to use [Delegate] .CombineDelegate / [Delegate]. Delete). Just don't tell your purist friends about it; they will not abide.

0
source

If you add them to the constructor, it would be correct to remove them in Dispose, but this, of course, depends on your design.

Here is a question with information about when you need to worry about deleting them.
AddHandler / RemoveHandler is not disposed of correctly

0
source

This is an old question, but I came to it through a web search, so it still remains a useful question about timers. The corresponding answer was not given to your UNDERLYING problem. Answers to your question were offered, but the question was wrong. Your question should be: How to disable the timer so that the event no longer fires?

Dim WithEvents timer1 as a new timer ()

(then add timer1_Elapsed to your code)

This solves the problem of taking care of IDisposable or Finalize, because the Timer event handler is managed for you. When you no longer need a timer, set Enabled = False or call the Stop () method to prevent it from ticking.

0
source

My first thought is that your actual problem can do little with adding and removing event handlers. StackOverflowException means that you have a number of functions that create an infinite loop of recursive calls. The code you sent does not show anywhere what could have happened, but the trace of the exception stack should indicate a violation code.

Based on your comment about creating a derived type test, I wonder if you can post more code from the constructor in the base class.

-1
source

This is strange, but in my case the finalizer is never called, what could be the reason?

Or was GC.SupressFinalize () called somewhere in your code?

-1
source

Source: https://habr.com/ru/post/897420/


All Articles