Opened 12 months ago

Last modified 12 months ago

#2863 new defect

SingleObjectiveBasicProblem causes a virtual method call in the constructor

Reported by: abeham Owned by:
Priority: medium Milestone: HeuristicLab 3.3.16
Component: Optimization Version:
Keywords: Cc:

Description

In the constructor several operators are added. Each call to Operators.Add causes the OperatorsChanged event to fire which in turn calls OnOperatorsChanged - a virtual method.

In a derived class thus one could see code being executed before its constructor is finished.

Change History (1)

comment:1 Changed 12 months ago by abeham

Discussion with mkommend:

  • The same may happen with OnEncodingChanged -> (SingleObjective)BasicProblem should get a protected constructor that takes an encoding
  • The error can hardly be avoided, we would need to provide a special method to add operators that would temporarily suspend event firing. This is not a very transparent solution however and thus doesn't really prevent someone experiencing such an issue.
  • A clean, but also cumbersome, solution would be to remove the virtual designation of OnOperatorsChanged and require each subclass to register its own events.
  • We also discussed adding another parameter to the protected constructor that takes an IEnumerable<IItem> which is added to the operators and which is concatenated by each layer. This parameter has to be enabled in all constructors up to Problem. However this may not support all cases.

The issue mostly occurs when a problem defines own operators that need to be wired to one of the problem's own parameters.

We agreed on using a workaround in the problem for the meantime. A change in the way initial operators are defined is tricky.

Note: See TracTickets for help on using tickets.