Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/19/11 21:32:06 (13 years ago)
Author:
mkommend
Message:

#1559: Fixed memory leak in SymbolicExpressionGrammar.

Location:
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammar.cs

    r6233 r6443  
    2121
    2222using System;
     23using System.Linq;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
    2526using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     27using System.Collections.Generic;
    2628
    2729namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
     
    111113    #endregion
    112114
     115    [StorableHook(HookType.AfterDeserialization)]
     116    private void AfterDeserialization() {
     117      foreach (ISymbol symbol in symbols.Values)
     118        RegisterSymbolEvents(symbol);
     119    }
    113120    [StorableConstructor]
    114121    protected SymbolicExpressionGrammar(bool deserializing) : base(deserializing) { }
    115122    protected SymbolicExpressionGrammar(SymbolicExpressionGrammar original, Cloner cloner)
    116123      : base(original, cloner) {
     124      foreach (ISymbol symbol in Symbols)
     125        RegisterSymbolEvents(symbol);
     126
    117127      programRootSymbol = cloner.Clone(original.programRootSymbol);
    118128      startSymbol = cloner.Clone(original.StartSymbol);
     
    153163    }
    154164
     165    protected override void AddSymbol(ISymbol symbol) {
     166      base.AddSymbol(symbol);
     167      RegisterSymbolEvents(symbol);
     168    }
     169    protected override void RemoveSymbol(ISymbol symbol) {
     170      DeregisterSymbolEvents(symbol);
     171      base.RemoveSymbol(symbol);
     172    }
     173
    155174    public event EventHandler ReadOnlyChanged;
    156175    protected virtual void OnReadOnlyChanged() {
     
    166185    }
    167186    #endregion
     187
     188    #region symbol events
     189    protected virtual void RegisterSymbolEvents(ISymbol symbol) {
     190      symbol.NameChanging += new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging);
     191      symbol.NameChanged += new EventHandler(Symbol_NameChanged);
     192    }
     193    protected virtual void DeregisterSymbolEvents(ISymbol symbol) {
     194      symbol.NameChanging -= new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging);
     195      symbol.NameChanged -= new EventHandler(Symbol_NameChanged);
     196    }
     197
     198    private void Symbol_NameChanging(object sender, CancelEventArgs<string> e) {
     199      if (symbols.ContainsKey(e.Value)) e.Cancel = true;
     200    }
     201    private void Symbol_NameChanged(object sender, EventArgs e) {
     202      ISymbol symbol = (ISymbol)sender;
     203      string oldName = symbols.Where(x => x.Value == symbol).First().Key;
     204      string newName = symbol.Name;
     205
     206      symbols.Remove(oldName);
     207      symbols.Add(newName, symbol);
     208
     209      var subtreeCount = symbolSubtreeCount[oldName];
     210      symbolSubtreeCount.Remove(oldName);
     211      symbolSubtreeCount.Add(newName, subtreeCount);
     212
     213      List<string> allowedChilds;
     214      if (allowedChildSymbols.TryGetValue(oldName, out allowedChilds)) {
     215        allowedChildSymbols.Remove(oldName);
     216        allowedChildSymbols.Add(newName, allowedChilds);
     217      }
     218
     219      for (int i = 0; i < GetMaximumSubtreeCount(symbol); i++) {
     220        if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(oldName, i), out allowedChilds)) {
     221          allowedChildSymbolsPerIndex.Remove(Tuple.Create(oldName, i));
     222          allowedChildSymbolsPerIndex.Add(Tuple.Create(newName, i), allowedChilds);
     223        }
     224      }
     225
     226      foreach (var parent in Symbols) {
     227        if (allowedChildSymbols.TryGetValue(parent.Name, out allowedChilds))
     228          if (allowedChilds.Remove(oldName))
     229            allowedChilds.Add(newName);
     230
     231        for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) {
     232          if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, i), out allowedChilds))
     233            if (allowedChilds.Remove(oldName)) allowedChilds.Add(newName);
     234        }
     235      }
     236
     237      ClearCaches();
     238    }
     239    #endregion
    168240  }
    169241}
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs

    r6233 r6443  
    8181      cachedMinExpressionDepth = new Dictionary<string, int>();
    8282    }
    83     [StorableHook(HookType.AfterDeserialization)]
    84     private void AfterDeserialization() {
    85       foreach (ISymbol symbol in symbols.Values)
    86         RegisterSymbolEvents(symbol);
    87     }
    8883
    8984    protected SymbolicExpressionGrammarBase(SymbolicExpressionGrammarBase original, Cloner cloner)
     
    9388      cachedMinExpressionDepth = new Dictionary<string, int>();
    9489
    95 
    9690      symbols = original.symbols.ToDictionary(x => x.Key, y => (ISymbol)cloner.Clone(y.Value));
    9791      symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>(original.symbolSubtreeCount);
     
    10498      foreach (var element in original.allowedChildSymbolsPerIndex)
    10599        allowedChildSymbolsPerIndex.Add(element.Key, new List<string>(element.Value));
    106 
    107       foreach (ISymbol symbol in symbols.Values)
    108         RegisterSymbolEvents(symbol);
    109100    }
    110101
     
    122113
    123114    #region protected grammar manipulation methods
    124     protected void AddSymbol(ISymbol symbol) {
     115    protected virtual void AddSymbol(ISymbol symbol) {
    125116      if (ContainsSymbol(symbol)) throw new ArgumentException("Symbol " + symbol + " is already defined.");
    126       RegisterSymbolEvents(symbol);
    127117      symbols.Add(symbol.Name, symbol);
    128118      symbolSubtreeCount.Add(symbol.Name, Tuple.Create(0, 0));
     
    130120    }
    131121
    132     private void RegisterSymbolEvents(ISymbol symbol) {
    133       symbol.NameChanging += new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging);
    134       symbol.NameChanged += new EventHandler(Symbol_NameChanged);
    135     }
    136     private void DeregisterSymbolEvents(ISymbol symbol) {
    137       symbol.NameChanging -= new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging);
    138       symbol.NameChanged -= new EventHandler(Symbol_NameChanged);
    139     }
    140 
    141     private void Symbol_NameChanging(object sender, CancelEventArgs<string> e) {
    142       if (symbols.ContainsKey(e.Value)) e.Cancel = true;
    143     }
    144     private void Symbol_NameChanged(object sender, EventArgs e) {
    145       ISymbol symbol = (ISymbol)sender;
    146       string oldName = symbols.Where(x => x.Value == symbol).First().Key;
    147       string newName = symbol.Name;
    148 
    149       symbols.Remove(oldName);
    150       symbols.Add(newName, symbol);
    151 
    152       var subtreeCount = symbolSubtreeCount[oldName];
    153       symbolSubtreeCount.Remove(oldName);
    154       symbolSubtreeCount.Add(newName, subtreeCount);
    155 
    156       List<string> allowedChilds;
    157       if (allowedChildSymbols.TryGetValue(oldName, out allowedChilds)) {
    158         allowedChildSymbols.Remove(oldName);
    159         allowedChildSymbols.Add(newName, allowedChilds);
    160       }
    161 
    162       for (int i = 0; i < GetMaximumSubtreeCount(symbol); i++) {
    163         if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(oldName, i), out allowedChilds)) {
    164           allowedChildSymbolsPerIndex.Remove(Tuple.Create(oldName, i));
    165           allowedChildSymbolsPerIndex.Add(Tuple.Create(newName, i), allowedChilds);
    166         }
    167       }
    168 
    169       foreach (var parent in Symbols) {
    170         if (allowedChildSymbols.TryGetValue(parent.Name, out allowedChilds))
    171           if (allowedChilds.Remove(oldName))
    172             allowedChilds.Add(newName);
    173 
    174         for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) {
    175           if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, i), out allowedChilds))
    176             if (allowedChilds.Remove(oldName)) allowedChilds.Add(newName);
    177         }
    178       }
    179 
    180       ClearCaches();
    181     }
    182 
    183     protected void RemoveSymbol(ISymbol symbol) {
     122    protected virtual void RemoveSymbol(ISymbol symbol) {
    184123      symbols.Remove(symbol.Name);
    185124      allowedChildSymbols.Remove(symbol.Name);
     
    198137        }
    199138      }
    200       DeregisterSymbolEvents(symbol);
    201139      ClearCaches();
    202140    }
     
    315253
    316254
    317     private void ClearCaches() {
     255    protected void ClearCaches() {
    318256      cachedMinExpressionLength.Clear();
    319257      cachedMaxExpressionLength.Clear();
Note: See TracChangeset for help on using the changeset viewer.