From c6e069fad77e54867fc85f9d48a2b9a07adcb493 Mon Sep 17 00:00:00 2001 From: niksedk Date: Sun, 2 Nov 2014 21:28:42 +0100 Subject: [PATCH] More fixes for resource leaks --- src/Forms/Main.cs | 299 +++++++++++++------------ src/Forms/VobSubOcr.cs | 46 ++-- src/Logic/SubtitleFormats/Cavena890.cs | 26 ++- src/Logic/Utilities.cs | 6 +- 4 files changed, 198 insertions(+), 179 deletions(-) diff --git a/src/Forms/Main.cs b/src/Forms/Main.cs index 405c930d2..795f1bd2b 100644 --- a/src/Forms/Main.cs +++ b/src/Forms/Main.cs @@ -9700,14 +9700,15 @@ namespace Nikse.SubtitleEdit.Forms int packedId = tsParser.SubtitlePacketIds[0]; if (tsParser.SubtitlePacketIds.Count > 1) { - var subChooser = new TransportStreamSubtitleChooser(); - _formPositionsAndSizes.SetPositionAndSize(subChooser); - subChooser.Initialize(tsParser, fileName); - if (subChooser.ShowDialog(this) == DialogResult.Cancel) - return false; - packedId = tsParser.SubtitlePacketIds[subChooser.SelectedIndex]; - _formPositionsAndSizes.SavePositionAndSize(subChooser); - subChooser.Dispose(); + using (var subChooser = new TransportStreamSubtitleChooser()) + { + _formPositionsAndSizes.SetPositionAndSize(subChooser); + subChooser.Initialize(tsParser, fileName); + if (subChooser.ShowDialog(this) == DialogResult.Cancel) + return false; + packedId = tsParser.SubtitlePacketIds[subChooser.SelectedIndex]; + _formPositionsAndSizes.SavePositionAndSize(subChooser); + } } var subtitles = tsParser.GetDvbSubtitles(packedId); @@ -9900,12 +9901,14 @@ namespace Nikse.SubtitleEdit.Forms } else { - var subtitleChooser = new MatroskaSubtitleChooser(); - subtitleChooser.Initialize(mp4SubtitleTracks); - if (subtitleChooser.ShowDialog(this) == DialogResult.OK) + using (var subtitleChooser = new MatroskaSubtitleChooser()) { - LoadMp4Subtitle(fileName, mp4SubtitleTracks[subtitleChooser.SelectedIndex]); - return true; + subtitleChooser.Initialize(mp4SubtitleTracks); + if (subtitleChooser.ShowDialog(this) == DialogResult.OK) + { + LoadMp4Subtitle(fileName, mp4SubtitleTracks[subtitleChooser.SelectedIndex]); + return true; + } } return false; } @@ -10189,11 +10192,13 @@ namespace Nikse.SubtitleEdit.Forms } else if (subtitleList.Count > 1) { - MatroskaSubtitleChooser subtitleChooser = new MatroskaSubtitleChooser(); - subtitleChooser.Initialize(subtitleList); - if (subtitleChooser.ShowDialog(this) == DialogResult.OK) + using (var subtitleChooser = new MatroskaSubtitleChooser()) { - LoadMatroskaSubtitle(subtitleList[subtitleChooser.SelectedIndex], fileName, false); + subtitleChooser.Initialize(subtitleList); + if (subtitleChooser.ShowDialog(this) == DialogResult.OK) + { + LoadMatroskaSubtitle(subtitleList[subtitleChooser.SelectedIndex], fileName, false); + } } } else @@ -12781,11 +12786,13 @@ namespace Nikse.SubtitleEdit.Forms } else { - var subtitleChooser = new MatroskaSubtitleChooser(); - subtitleChooser.Initialize(mp4SubtitleTracks); - if (subtitleChooser.ShowDialog(this) == DialogResult.OK) + using (var subtitleChooser = new MatroskaSubtitleChooser()) { - sub = LoadMp4SubtitleForSync(mp4SubtitleTracks[0]); + subtitleChooser.Initialize(mp4SubtitleTracks); + if (subtitleChooser.ShowDialog(this) == DialogResult.OK) + { + sub = LoadMp4SubtitleForSync(mp4SubtitleTracks[0]); + } } } } @@ -17924,11 +17931,13 @@ namespace Nikse.SubtitleEdit.Forms private void PlainTextToolStripMenuItemClick(object sender, EventArgs e) { - var exportText = new ExportText(); - exportText.Initialize(_subtitle, _fileName); - if (exportText.ShowDialog() == DialogResult.OK) + using (var exportText = new ExportText()) { - ShowStatus(Configuration.Settings.Language.Main.SubtitleExported); + exportText.Initialize(_subtitle, _fileName); + if (exportText.ShowDialog() == DialogResult.OK) + { + ShowStatus(Configuration.Settings.Language.Main.SubtitleExported); + } } } @@ -18620,132 +18629,134 @@ namespace Nikse.SubtitleEdit.Forms private void toolStripMenuItemAlignment_Click(object sender, EventArgs e) { - var f = new AlignmentPicker(); - f.TopMost = true; - f.StartPosition = FormStartPosition.Manual; - f.Left = Cursor.Position.X - 150; - f.Top = Cursor.Position.Y - 75; - if (f.ShowDialog(this) == DialogResult.OK) + using (var f = new AlignmentPicker()) { - string tag = string.Empty; - var format = GetCurrentSubtitleFormat(); - if (format.GetType() == typeof(SubStationAlpha)) + f.TopMost = true; + f.StartPosition = FormStartPosition.Manual; + f.Left = Cursor.Position.X - 150; + f.Top = Cursor.Position.Y - 75; + if (f.ShowDialog(this) == DialogResult.OK) { - //1: Bottom left - //2: Bottom center - //3: Bottom right - //9: Middle left - //10: Middle center - //11: Middle right - //5: Top left - //6: Top center - //7: Top right - switch (f.Alignment) + string tag = string.Empty; + var format = GetCurrentSubtitleFormat(); + if (format.GetType() == typeof (SubStationAlpha)) { - case ContentAlignment.BottomLeft: - tag = "{\\a1}"; - break; - case ContentAlignment.BottomCenter: - tag = "{\\a2}"; - break; - case ContentAlignment.BottomRight: - tag = "{\\a3}"; - break; - case ContentAlignment.MiddleLeft: - tag = "{\\a9}"; - break; - case ContentAlignment.MiddleCenter: - tag = "{\\a10}"; - break; - case ContentAlignment.MiddleRight: - tag = "{\\a11}"; - break; - case ContentAlignment.TopLeft: - tag = "{\\a5}"; - break; - case ContentAlignment.TopCenter: - tag = "{\\a6}"; - break; - case ContentAlignment.TopRight: - tag = "{\\a7}"; - break; - } - } - else - { - //1: Bottom left - //2: Bottom center - //3: Bottom right - //4: Middle left - //5: Middle center - //6: Middle right - //7: Top left - //8: Top center - //9: Top right - switch (f.Alignment) - { - case ContentAlignment.BottomLeft: - tag = "{\\an1}"; - break; - case ContentAlignment.BottomCenter: - if (format.GetType() == typeof(SubRip)) - tag = string.Empty; - else - tag = "{\\an2}"; - break; - case ContentAlignment.BottomRight: - tag = "{\\an3}"; - break; - case ContentAlignment.MiddleLeft: - tag = "{\\an4}"; - break; - case ContentAlignment.MiddleCenter: - tag = "{\\an5}"; - break; - case ContentAlignment.MiddleRight: - tag = "{\\an6}"; - break; - case ContentAlignment.TopLeft: - tag = "{\\an7}"; - break; - case ContentAlignment.TopCenter: - tag = "{\\an8}"; - break; - case ContentAlignment.TopRight: - tag = "{\\an9}"; - break; - } - } - if (_subtitle.Paragraphs.Count > 0 && SubtitleListview1.SelectedItems.Count > 0) - { - SubtitleListview1.SelectedIndexChanged -= SubtitleListview1_SelectedIndexChanged; - MakeHistoryForUndo(string.Format(_language.BeforeAddingTagX, tag)); - - var indexes = new List(); - foreach (ListViewItem item in SubtitleListview1.SelectedItems) - indexes.Add(item.Index); - - SubtitleListview1.BeginUpdate(); - foreach (int i in indexes) - { - if (_subtitleAlternate != null && Configuration.Settings.General.AllowEditOfOriginalSubtitle) + //1: Bottom left + //2: Bottom center + //3: Bottom right + //9: Middle left + //10: Middle center + //11: Middle right + //5: Top left + //6: Top center + //7: Top right + switch (f.Alignment) { - Paragraph original = Utilities.GetOriginalParagraph(i, _subtitle.Paragraphs[i], _subtitleAlternate.Paragraphs); - if (original != null) - { - SetAlignTag(original, tag); - SubtitleListview1.SetAlternateText(i, original.Text); - } + case ContentAlignment.BottomLeft: + tag = "{\\a1}"; + break; + case ContentAlignment.BottomCenter: + tag = "{\\a2}"; + break; + case ContentAlignment.BottomRight: + tag = "{\\a3}"; + break; + case ContentAlignment.MiddleLeft: + tag = "{\\a9}"; + break; + case ContentAlignment.MiddleCenter: + tag = "{\\a10}"; + break; + case ContentAlignment.MiddleRight: + tag = "{\\a11}"; + break; + case ContentAlignment.TopLeft: + tag = "{\\a5}"; + break; + case ContentAlignment.TopCenter: + tag = "{\\a6}"; + break; + case ContentAlignment.TopRight: + tag = "{\\a7}"; + break; } - SetAlignTag(_subtitle.Paragraphs[i], tag); - SubtitleListview1.SetText(i, _subtitle.Paragraphs[i].Text); } - SubtitleListview1.EndUpdate(); + else + { + //1: Bottom left + //2: Bottom center + //3: Bottom right + //4: Middle left + //5: Middle center + //6: Middle right + //7: Top left + //8: Top center + //9: Top right + switch (f.Alignment) + { + case ContentAlignment.BottomLeft: + tag = "{\\an1}"; + break; + case ContentAlignment.BottomCenter: + if (format.GetType() == typeof (SubRip)) + tag = string.Empty; + else + tag = "{\\an2}"; + break; + case ContentAlignment.BottomRight: + tag = "{\\an3}"; + break; + case ContentAlignment.MiddleLeft: + tag = "{\\an4}"; + break; + case ContentAlignment.MiddleCenter: + tag = "{\\an5}"; + break; + case ContentAlignment.MiddleRight: + tag = "{\\an6}"; + break; + case ContentAlignment.TopLeft: + tag = "{\\an7}"; + break; + case ContentAlignment.TopCenter: + tag = "{\\an8}"; + break; + case ContentAlignment.TopRight: + tag = "{\\an9}"; + break; + } + } + if (_subtitle.Paragraphs.Count > 0 && SubtitleListview1.SelectedItems.Count > 0) + { + SubtitleListview1.SelectedIndexChanged -= SubtitleListview1_SelectedIndexChanged; + MakeHistoryForUndo(string.Format(_language.BeforeAddingTagX, tag)); - ShowStatus(string.Format(_language.TagXAdded, tag)); - ShowSource(); - RefreshSelectedParagraph(); - SubtitleListview1.SelectedIndexChanged += SubtitleListview1_SelectedIndexChanged; + var indexes = new List(); + foreach (ListViewItem item in SubtitleListview1.SelectedItems) + indexes.Add(item.Index); + + SubtitleListview1.BeginUpdate(); + foreach (int i in indexes) + { + if (_subtitleAlternate != null && Configuration.Settings.General.AllowEditOfOriginalSubtitle) + { + Paragraph original = Utilities.GetOriginalParagraph(i, _subtitle.Paragraphs[i], _subtitleAlternate.Paragraphs); + if (original != null) + { + SetAlignTag(original, tag); + SubtitleListview1.SetAlternateText(i, original.Text); + } + } + SetAlignTag(_subtitle.Paragraphs[i], tag); + SubtitleListview1.SetText(i, _subtitle.Paragraphs[i].Text); + } + SubtitleListview1.EndUpdate(); + + ShowStatus(string.Format(_language.TagXAdded, tag)); + ShowSource(); + RefreshSelectedParagraph(); + SubtitleListview1.SelectedIndexChanged += SubtitleListview1_SelectedIndexChanged; + } } } } diff --git a/src/Forms/VobSubOcr.cs b/src/Forms/VobSubOcr.cs index 879748381..6f3e6e4c0 100644 --- a/src/Forms/VobSubOcr.cs +++ b/src/Forms/VobSubOcr.cs @@ -7604,22 +7604,24 @@ namespace Nikse.SubtitleEdit.Forms index++; } Cursor = Cursors.Default; - VobSubOcrCharacterInspect inspect = new VobSubOcrCharacterInspect(); - inspect.Initialize(comboBoxCharacterDatabase.SelectedItem.ToString(), matches, imageSources, _binaryOcrDb); - if (inspect.ShowDialog(this) == DialogResult.OK) + using (var inspect = new VobSubOcrCharacterInspect()) { - Cursor = Cursors.WaitCursor; - if (_binaryOcrDb != null) + inspect.Initialize(comboBoxCharacterDatabase.SelectedItem.ToString(), matches, imageSources, _binaryOcrDb); + if (inspect.ShowDialog(this) == DialogResult.OK) { - _binaryOcrDb.Save(); + Cursor = Cursors.WaitCursor; + if (_binaryOcrDb != null) + { + _binaryOcrDb.Save(); + Cursor = Cursors.Default; + return; + } + _compareDoc = inspect.ImageCompareDocument; + string path = Configuration.VobSubCompareFolder + comboBoxCharacterDatabase.SelectedItem + Path.DirectorySeparatorChar; + _compareDoc.Save(path + "Images.xml"); + LoadImageCompareBitmaps(); Cursor = Cursors.Default; - return; } - _compareDoc = inspect.ImageCompareDocument; - string path = Configuration.VobSubCompareFolder + comboBoxCharacterDatabase.SelectedItem + Path.DirectorySeparatorChar; - _compareDoc.Save(path + "Images.xml"); - LoadImageCompareBitmaps(); - Cursor = Cursors.Default; } if (_binaryOcrDb != null) _binaryOcrDb.LoadCompareImages(); @@ -8148,17 +8150,19 @@ namespace Nikse.SubtitleEdit.Forms string result = OcrViaNOCR(bitmap, subtitleListView1.SelectedItems[0].Index); checkBoxPromptForUnknownWords.Checked = oldPrompt; Cursor = Cursors.Default; - var inspect = new VobSubNOcrCharacterInspect(); - bool oldCorrect = checkBoxNOcrCorrect.Checked; - checkBoxNOcrCorrect.Checked = false; - inspect.Initialize(bitmap, (int)numericUpDownNumberOfPixelsIsSpaceNOCR.Value, checkBoxRightToLeft.Checked, _nOcrDb, this); - if (inspect.ShowDialog(this) == DialogResult.OK) + using (var inspect = new VobSubNOcrCharacterInspect()) { - Cursor = Cursors.WaitCursor; - SaveNOcrWithCurrentLanguage(); - Cursor = Cursors.Default; + bool oldCorrect = checkBoxNOcrCorrect.Checked; + checkBoxNOcrCorrect.Checked = false; + inspect.Initialize(bitmap, (int) numericUpDownNumberOfPixelsIsSpaceNOCR.Value, checkBoxRightToLeft.Checked, _nOcrDb, this); + if (inspect.ShowDialog(this) == DialogResult.OK) + { + Cursor = Cursors.WaitCursor; + SaveNOcrWithCurrentLanguage(); + Cursor = Cursors.Default; + } + checkBoxNOcrCorrect.Checked = oldCorrect; } - checkBoxNOcrCorrect.Checked = oldCorrect; } private void buttonLineOcrEditLanguage_Click(object sender, EventArgs e) diff --git a/src/Logic/SubtitleFormats/Cavena890.cs b/src/Logic/SubtitleFormats/Cavena890.cs index 4739cd1e5..a448e811a 100644 --- a/src/Logic/SubtitleFormats/Cavena890.cs +++ b/src/Logic/SubtitleFormats/Cavena890.cs @@ -462,18 +462,20 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats index++; buffer[index] = 0x6F; } - else if (current == 'å') - { - buffer[index] = 0x8C; - index++; - buffer[index] = 0x61; - } - else if (current == 'Å') - { - buffer[index] = 0x8C; - index++; - buffer[index] = 0x41; - } + + // different language setting? + //else if (current == 'å') + //{ + // buffer[index] = 0x8C; + // index++; + // buffer[index] = 0x61; + //} + //else if (current == 'Å') + //{ + // buffer[index] = 0x8C; + // index++; + // buffer[index] = 0x41; + //} // ăĂ îÎ şŞ ţŢ â (romanian) else if (current == 'ă') diff --git a/src/Logic/Utilities.cs b/src/Logic/Utilities.cs index 6d491f741..efa1a919e 100644 --- a/src/Logic/Utilities.cs +++ b/src/Logic/Utilities.cs @@ -68,8 +68,10 @@ namespace Nikse.SubtitleEdit.Logic double milliseconds = 0; string videoCodec = string.Empty; - var matroskaParser = new Matroska(); - matroskaParser.GetMatroskaInfo(fileName, ref success, ref hasConstantFrameRate, ref frameRate, ref width, ref height, ref milliseconds, ref videoCodec); + using (var matroskaParser = new Matroska()) + { + matroskaParser.GetMatroskaInfo(fileName, ref success, ref hasConstantFrameRate, ref frameRate, ref width, ref height, ref milliseconds, ref videoCodec); + } if (success) { info.Width = width;