WinForm でシリアル通信の送受信用の LED インジケーターとして使用するコントロールが見つからないため、ラベルから独自のユーザー定義インジケーターを作成しました。基本的には、ラベルの色を、受信用に黒からライムに、送信用に黒から赤に設定および再設定を繰り返します。そのクラスは次のとおりです。しかし、私の .NET プログラムは数時間実行され、完全にクラッシュしたようです。クラッシュ エラーの詳細を表示すると、Windows はそれをclr20r3 errorとして報告します。Fedora Linux でプログラムを作成および開発したときに、同様の問題が発生しました。フォーム上の私のシリアル通信インジケータは、どういうわけかメモリ リークを引き起こし、プログラムをクラッシュさせました。それを削除すると、問題なく動作しました。
では、ラベルの背景色の設定とリセットを数秒以内に繰り返すことでメモリ リークが発生する可能性はありますか?
namespace SerialLED;
interface
uses
  System.Collections.Generic,
  System.Windows.Forms,
  System.Drawing.*,
  System.Text;
type
  TheLED = public class(Label)
  private
  protected
  public
    constructor;
  end;
  TSerialIndicator = public class
  private
    method TxTimerEvent(Sender:System.Object; e:System.EventArgs);
    method RxTimerEvent(Sender:System.Object; e:System.EventArgs);
  public
    Txlight:TheLED;
    Rxlight:TheLED;
    TxTimer:System.Timers.Timer;
    RxTimer:System.Timers.Timer;
    constructor(mform:Form);
    method Transmit;
    method Receive;
  end;
implementation
method TSerialIndicator.Transmit;
begin
  TxLight.BackColor := Color.Red;
  if TxTimer.Enabled = false then
     TxTimer.Enabled:=true;
end;
method TSerialIndicator.Receive;
begin
  RxLight.BackColor := Color.Lime;
  if RxTimer.Enabled=false then
    RxTimer.Enabled:=true;
end;
method TSerialIndicator.RxTimerEvent(Sender:System.Object; e:System.EventArgs);
begin
    RxLight.BackColor := Color.Black;
    RxTimer.Enabled:=false;
end;
method TSerialIndicator.TxTimerEvent(Sender:System.Object; e:System.EventArgs);
begin
    TxLight.BackColor := Color.Black;
    TxTimer.Enabled:=false;
end;
constructor TSerialIndicator(mform:Form);
begin
    RxLight := new TheLED;
    TxLight := new TheLED;
    TxLight.AutoSize := false;
    RxLight.AutoSize := false;
    TxLight.BorderStyle := BorderStyle.Fixed3D;
    RxLight.BorderStyle := BorderStyle.Fixed3D;
    TxLight.Location := new point(52,163);
    RxLight.Location := new point(82,163);
    TxLight.Width := 20;
    TxLight.Height := 20;
    RxLight.Width :=20;
    RxLight.Height := 20;
    mform.Controls.Add(RxLight);
    mform.Controls.Add(TxLight);
    RxTimer := new System.Timers.Timer;
    TxTimer := new System.Timers.Timer;
    RxTimer.Interval:=50;
    TxTimer.Interval:=50;
    RxTimer.Enabled:=false;
    TxTimer.Enabled:=false;
    RxTimer.Elapsed += new System.Timers.ElapsedEventHandler(@RxTimerEvent);
    TxTimer.Elapsed += new System.Timers.ElapsedEventHandler(@TxTimerEvent);
    RxLight.BackColor := Color.Black;
    TxLight.BackColor := Color.Black;
end;
constructor TheLED;
begin
  self.DoubleBuffered:=true;
end;
これは、winform でどのように見えるかです:
