多线程读串口并写数据库死机的问题,大侠们帮帮我

delphi57 2013-07-16 08:42:39
BCB6+sql2000,我开了80个线程,读串口数据,然后保存到数据库,500毫秒写数据一次,每个串口一个表,在开始的几个小时内,内存未见明显增长,但是在运行20小时后,机器内存占用过高,致使机器直接死掉。
请问,这是怎么回事??
大侠们帮帮忙。
...全文
1000 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
关工 2013-07-17
  • 打赏
  • 举报
回复
你开了80个线程,你有几个串口,这么多线程一起创建的么? 是否存在多个线程一起扫描同一个串口? 是否存在数据丢失的情况? 你的这种写法存在机制问题。
我不懂电脑 2013-07-17
  • 打赏
  • 举报
回复
用codegaurd检查内存漏洞
delphi57 2013-07-16
  • 打赏
  • 举报
回复
代码见上面,在线等
delphi57 2013-07-16
  • 打赏
  • 举报
回复
//--------------------------------------------------------------------------- #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(); }

1,178

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 数据库及相关技术
社区管理员
  • 数据库及相关技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧