BCB6+sql2000,我開了80個執行緒,讀串口資料,然后保存到資料庫,500毫秒寫資料一次,每個串口一個表,在開始的幾個小時內,記憶體未見明顯增長,但是在運行20小時后,機器記憶體占用過高,致使機器直接死掉。
請問,這是怎么回事??
大俠們幫幫忙。
uj5u.com熱心網友回復:
//---------------------------------------------------------------------------#include <vcl.h>
#pragma hdrstop
#include "ThreadReceiveData.h"
#include "ClassComm.h"
#include <Classes.hpp>
#include "ClassConfigInfoRW.h"
#include "main.h"
#include "stdio.h"
//#include <ADODB.hpp>
//#include <DB.hpp>
#pragma package(smart_init)
//---------------------------------------------------------------------------
// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall TThreadReceiveData::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//---------------------------------------------------------------------------
__fastcall TThreadReceiveData::TThreadReceiveData(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
__fastcall TThreadReceiveData::TThreadReceiveData(DEVCONFIGINFO thrd_devConfigInfo,int thrd_devIndex,bool CreateSuspended)
: TThread(CreateSuspended)
{
FreeOnTerminate = true;
ClassComm=new TClassComm();
devConfigInfo=thrd_devConfigInfo;
devIndex=thrd_devIndex;
int portnum,baudrate,parity,stopbits;
portnum =devConfigInfo.PCInfo[devIndex].portnum;
baudrate =devConfigInfo.PCInfo[devIndex].baudrate;
parity =devConfigInfo.PCInfo[devIndex].parity;
stopbits =devConfigInfo.PCInfo[devIndex].stopbits;
ClassComm->InitPort(portnum,baudrate,parity,stopbits);
}
//---------------------------------------------------------------------------
void __fastcall TThreadReceiveData::Execute()
{
//---- Place thread code here ----
unsigned char receive_buf[2048];
unsigned char history_receive_buf[2048];
unsigned short count,len;
AnsiString str_tablename;
AnsiString str_data[2];
AnsiString str_sql;
str_data[0]="";
str_data[1]="";
//----------------------------------------------------------------------
CoInitialize(NULL);
ADOConnection= new TADOConnection(ADOConnection);
ADOCommand= new TADOCommand(ADOCommand);
ADOConnection->ConnectionString="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=JZMSDB;Data Source=ZHANGSONG";
ADOConnection->LoginPrompt=false;
ADOCommand->Connection=ADOConnection;
ADOConnection->Open();
str_tablename="Table_Data"+IntToStr(devIndex)+"_"+Now().FormatString("YYYYMMDDhhmmss");
str_sql="CREATE TABLE "+str_tablename+"(ID bigint identity(1,1) not null primary key,recordTime DATETIME,sdata char(32))";
ADOCommand->CommandText=str_sql;
ADOCommand->Execute();
//----------------------------------------------------------------------
len=0;
ReceiveData.receiveCnt[0]=0;
ReceiveData.receiveCnt[1]=0;
do{
ReadWriteGuard->Acquire();
count=ClassComm->ReceiveData(receive_buf);
ReadWriteGuard->Release();
for(int i=0;i<count;i++)
{
history_receive_buf[i+len]=receive_buf[i];
}
len+=count;
//----------------------------------------------------------
int k=0;
while(len >= 8)
{
if(history_receive_buf[k]==0xFF)
{
if(k==7)
{
str_data[history_receive_buf[0]&0x01]="";
for(int dindex=0;dindex<8;dindex++)
{
ReceiveData.databuf[history_receive_buf[0]&0x01][dindex]=history_receive_buf[dindex];
str_data[history_receive_buf[0]&0x01]+= HexToAnsiString(history_receive_buf[dindex]);
}
ReceiveData.receiveCnt[history_receive_buf[0]&0x01]++;
col_1=10;
row_1=2*devIndex+1+(history_receive_buf[0]&0x01);
col_2=11;
row_2=2*devIndex+1+(history_receive_buf[0]&0x01);
ss[0]= str_data[history_receive_buf[0]&0x01];
ss[1]= ReceiveData.receiveCnt[history_receive_buf[0]&0x01];
Synchronize(DisplayData);
//------------------------------保存資料----------------------------------------------
str_sql="insert into "+str_tablename+"(recordTime,sdata) VALUES('"+Now().FormatString("YYYY-MM-DD hh:mm:ss")+"','"+str_data[history_receive_buf[0]&0x01]+"')";
ADOCommand->CommandText=str_sql;
ADOCommand->Execute();
}
k++;
for(int i=0;i<len-k;i++)
{
history_receive_buf[i]=history_receive_buf[i+k];
}
len -= k;
k=0;
}
else
{
k++;
}
Sleep(2);
}
Sleep(2);
}while(!Terminated);
//ADOConnection->Close();
ClassComm->ClosePort();
delete ClassComm;
ClassComm=NULL;
//CoUninitialize();
}
//---------------------------------------------------------------------------
void __fastcall TThreadReceiveData::DisplayData()
{
MainForm->AdvStringGrid->Cells[col_1][row_1]= ss[0];
MainForm->AdvStringGrid->Cells[col_2][row_2]= ss[1];
//MainForm->AdvStringGrid->Refresh();
}
uj5u.com熱心網友回復:
代碼見上面,在線等uj5u.com熱心網友回復:
用codegaurd檢查記憶體漏洞uj5u.com熱心網友回復:
你開了80個執行緒,你有幾個串口,這么多執行緒一起創建的么?是否存在多個執行緒一起掃描同一個串口?
是否存在資料丟失的情況?
你的這種寫法存在機制問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/137556.html
標籤:數據庫及相關技術
下一篇:新人做網路爬蟲,求各位指點
