当电话/GSM 模块意外断开时,导致内存泄漏的原因是什么,尽管没有执行任何任务。以及我该如何处理。在我的应用程序中,手机通过数据线和 gsmcomm 库/AT-COMMANDS 连接。当用户使用提供的连接/断开按钮连接或断开电话并且应用程序工作正常时,我已经处理了连接的打开和关闭。但是如果手机在没有点击断开按钮的情况下意外断开(例如用户拔掉数据线),应用程序的大小在 RAM 上会增加。我了解连接保持打开状态,但没有执行任何任务。我如何在我的应用程序中处理这个问题。
这里是连接和断开电话的电话/GSM模块的代码。GsmCommMain 来自 GSMComm 库,用于连接手机的 GSM 模块。
连接手机的代码
// pohone connect
#region
try
{
Comm_Port = Convert.ToInt16(capdeviceid.ToString().Substring(3));
Comm_BaudRate = Convert.ToInt32(MaxBaudRate.ToString());
Comm_TimeOut = Convert.ToInt32(timeoutsec.ToString());
}
catch (Exception)
{
MessageBox.Show("Error Converting COM Port Settings Values", "Check COM Port Values", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
MessageBox.Show("Please Redetect the Phone or the phone is already connected.");
return;
}
comm = new GsmCommMain(Comm_Port, Comm_BaudRate, Comm_TimeOut);
try
{
comm.Open();
if (comm.IsConnected())
{
//pictureBox3.Image = imageList1.Images[1];
MessageBox.Show("Connected Successfully To GSM Phone / Modem...!!!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
PhoneConnectionLabel.Text = captionObj + " Connected";
this.Text = "From " + captionObj.ToString();
sendSMSToolStripMenuItem.Enabled = true;
disconnectToolStripMenuItem1.Enabled = true;
}
}
catch (Exception E2)
{
MessageBox.Show(E2.Message);
MessageBox.Show("Error While Connecting To GSM Phone / Modem", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
//dataGridView3.ClearSelection();
}
#endregion
断开电话的代码
try
{
if (!comm.IsConnected())
{
MessageBox.Show("No Phone Connected", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
else
{
comm.Close();
this.Text = "Form1";
PhoneConnectionLabel.Text = "No Phone Connected";
sendSMSToolStripMenuItem.Enabled = false;
disconnectToolStripMenuItem1.Enabled = false;
}
}
catch (Exception No_Conn)
{
MessageBox.Show("No Phone Connected!!\r\n\n" + No_Conn, "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}