@TOC
前言
刚进公司,需要学开发需要的新框架及现成框架,完成自己的需求。其中会遇到很多卡死等bug,Visual Studio每次执行到这个类中会进入执行catch、finally等,从而导致上位机软件卡死,总结一下,以供后面参考学习,查看。肯定大家都有一定差异啦,大家作为参考哦,与诸君共勉。
一、下面代码有bug,导致上位机软件一直卡
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| using ... using System.IO;
namespace ....ChipFile { public class ChipxxxOEFile : ChipxxxOEFileBase { public ChipxxxOEFile() : base() { }
#region 公共方法 public override bool SaveSpecialChipOE(string fileName) { byte[] OEData = null;
#region 写入文件 FileStream dbtFile = null; try { if (!Directory.Exists(Path.GetDirectoryName(fileName))) { Directory.CreateDirectory(Path.GetDirectoryName(fileName)); } dbtFile = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite); dbtFile.Write(OEData, 0, OEData.Length); return true; } catch (System.Exception e) { System.Diagnostics.Trace.WriteLine(e.Message); return false; } finally { if (dbtFile != null) { dbtFile.Close(); } } #endregion } public override bool SaveAsOETableFile(string filename, ref ScanBoardProperty scanBdProp) {
SaveSpecialChipOE(ConstValue.TEMP_OE_FILE);
if (File.Exists(ConstValue.TEMP_OE_FILE)) { using (FileStream fs = new FileStream(ConstValue.TEMP_OE_FILE, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { scanBdProp.OEData = new byte[fs.Length];
fs.Read(scanBdProp.OEData, 0, scanBdProp.OEData.Length); } } return true; } #endregion } }
|
上面代码片段主要的问题似乎出现在SaveSpecialChipOE方法中。SaveSpecialChipOE方法试图将一些数据写入文件,但存在几个潜在的问题点,这些问题可能会导致异常和性能问题。
- 未初始化的
OEData变量: 代码中OEData变量被声明了但没有初始化或赋值,这将导致NullReferenceException。根据需求,需要确保在写入文件之前,OEData已经被正确初始化和赋值。
- 异常处理: 异常处理使用的是
System.Diagnostics.Trace.WriteLine(e.Message);,这不会阻止异常的传播。考虑到希望在出现异常时避免软件卡死,应该确保在所有可能出现异常的地方进行适当的异常处理。
- 资源管理: 使用
FileStream和其他IDisposable资源时,推荐使用using语句。这样可以保证即使发生异常,资源也能被正确地释放。
- 文件路径处理: 代码中直接使用
Path.GetDirectoryName(fileName),在fileName是根目录文件时(比如”C:\example.txt”),这会返回null。因此,在创建目录之前需要检查null。
下面是进行了一些修改后的版本代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| using ... using System; using System.IO;
namespace ....ChipFile { public class ChipxxxOEFile : ChipxxxOEFileBase { public ChipxxxOEFile() : base() { }
#region 公共方法 public override bool SaveSpecialChipOE(string fileName) { byte[] OEData = PrepareOEData();
try { string directoryName = Path.GetDirectoryName(fileName); if (!string.IsNullOrEmpty(directoryName) && !Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); }
using (FileStream dbtFile = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { dbtFile.Write(OEData, 0, OEData.Length); } return true; } catch (Exception e) { System.Diagnostics.Trace.WriteLine(e.Message); return false; } }
public override bool SaveAsOETableFile(string filename, ref ScanBoardProperty scanBdProp) { if (SaveSpecialChipOE(ConstValue.TEMP_OE_FILE)) { using (FileStream fs = new FileStream(ConstValue.TEMP_OE_FILE, FileMode.Open, FileAccess.Read)) { scanBdProp.OEData = new byte[fs.Length]; fs.Read(scanBdProp.OEData, 0, scanBdProp.OEData.Length); } return true; } return false; } #endregion
private byte[] PrepareOEData() { return new byte[0]; } } }
|
巨人的肩膀
- 周志明老师的凤凰架构
- C#高级编程
- C#函数式编程
- 叩响C#之门
- 组内各位前辈的指导