請問這段程式碼都會產生記憶體錯誤該如何解決 |
答題得分者是:taishyang
|
cat200188
中階會員 ![]() ![]() ![]() 發表:37 回覆:78 積分:68 註冊:2005-01-18 發送簡訊給我 |
請問這段程式碼都會產生記憶體錯誤該如何解決,如附件,解剖xml時都會產生記憶體錯誤
------
....... |
taishyang
站務副站長 ![]() ![]() ![]() ![]() ![]() ![]() 發表:377 回覆:5490 積分:4563 註冊:2002-10-08 發送簡訊給我 |
|
cat200188
中階會員 ![]() ![]() ![]() 發表:37 回覆:78 積分:68 註冊:2005-01-18 發送簡訊給我 |
|
taishyang
站務副站長 ![]() ![]() ![]() ![]() ![]() ![]() 發表:377 回覆:5490 積分:4563 註冊:2002-10-08 發送簡訊給我 |
|
cat200188
中階會員 ![]() ![]() ![]() 發表:37 回覆:78 積分:68 註冊:2005-01-18 發送簡訊給我 |
if (myadc->EchoService)
{ //Show(); String xml1 ="<?xml version=\"1.0\" encoding=\"UTF-8\"?> String xml; /*try { //傳送資料準備收圖 xml = EchoService->executeTransaction("UAdcDefect",IntToStr(stat),"I",xml1); } catch ( ... ) { trainf->Close() ; return; } */ xml ="<?xml version=\"1.0\" encoding=\"UTF-8\"?> xml=xml " xml=xml " xml=xml " HINSTANCE hinst,hinst1 ; hinst = LoadLibrary("xml.dll"); hinst1 = LoadLibrary("readimage.dll"); String (*readxmlvaule)(String,String); void (*readimage_h)(String,int,String); try { (FARPROC &)readxmlvaule =GetProcAddress(hinst,"readxmlvaule"); (FARPROC &)readimage_h =GetProcAddress(hinst1,"readimage_h"); } catch ( ... ) { //trainf->Close() ; FreeLibrary(hinst); FreeLibrary(hinst1); return ; } String error_code = readxmlvaule(xml,"/trx/rtn_code"); //有error 重做 if (error_code.Trim() != "0000000" ){ //trainf->Close() ; FreeLibrary(hinst); FreeLibrary(hinst1); return ; } String test_time = readxmlvaule(xml,"trx/test_time"); String lot_id = readxmlvaule(xml,"trx/lot_id"); String sheet_id = readxmlvaule(xml,"trx/sheet_id"); String chip_no = readxmlvaule(xml,"trx/chip_no"); String pox_x = readxmlvaule(xml,"trx/pox_x"); String pox_y = readxmlvaule(xml,"trx/pox_y"); String image_path = readxmlvaule(xml,"trx/image_path"); String recipe_id = readxmlvaule(xml,"trx/recipe_id"); try { TFileStream *fs = new TFileStream(ExtractFilePath(Application->ExeName) "image3\\1.jpg", fmCreate); //IdHTTP1->Connect() ; myadc->IdHTTP1->HandleRedirects = true ; myadc->IdHTTP1->Get(image_path,fs); //IdHTTP1->Disconnect() ; fs->Free() ; } catch ( ... ) { //trainf->Close() ; FreeLibrary(hinst); FreeLibrary(hinst1); return; } TJPEGImage *myjpeg = new TJPEGImage; Graphics::TBitmap *mybmp = new Graphics::TBitmap ; myjpeg->LoadFromFile("image3\\1.jpg"); mybmp->Width =myjpeg->Width ; mybmp->Height =myjpeg->Height ; mybmp->Canvas->Draw(0,0,myjpeg); if (FileExists(ExtractFilePath(Application->ExeName) "image3\\1.bmp")) { DeleteFile(ExtractFilePath(Application->ExeName) "image3\\1.bmp"); } mybmp->SaveToFile(ExtractFilePath(Application->ExeName) "image3\\1.bmp"); DeleteFile(ExtractFilePath(Application->ExeName) "image3\\1.jpg"); //拆解recipe ,當不符合離開 TReplaceFlags Flags ; Flags = Flags < String *str = split(recipe_id ,'-'); if (str[0].ToInt() < 3 ) { //trainf->Close() ; FreeLibrary(hinst); FreeLibrary(hinst1); return ; } String classstr = str[1]; String azostr = str[2]; String pepstr = str[3]; //當找不到資料離開 TLocateOptions options; //加入忽略字母大小寫,子字串符合找尋即找到 options = options << loCaseInsensitive << loPartialKey ; bool search1 = dm3->dclass->Locate("smemo",classstr,options); if (search1==false) { //trainf->Close() ; FreeLibrary(hinst); FreeLibrary(hinst1); return ; } bool search2 = dm3->dazo->Locate("azomemo",azostr,options); if (search2==false) { //trainf->Close() ; FreeLibrary(hinst); FreeLibrary(hinst1); return ; } bool search3 = dm3->dpep->Locate("pepmemo",pepstr,options); if (search3== false || dm3->dpepintr->AsString =="" ) { //trainf->Close() ; FreeLibrary(hinst); FreeLibrary(hinst1); return ; } long intr = dm3->dpepintr->AsInteger ; double chg =dm3->dpepchg->Value ; long defs = dm3->dpepdefs->AsInteger ; long eags = dm3->dpepeags->AsInteger ; String class_me = dm3->dpeppeppath->AsString; //當迷發現路逕離開 if (class_me.Trim() == "") { FreeLibrary(hinst); FreeLibrary(hinst1); return ; } /* if (!myadc->nowadc->LoadNNWeight((class_me "\\NNParameter.txt").c_str())) { train->Caption ="載入NNParameter.txt失敗!!"; return; } if(!myadc->nowadc->AllocateMemory(myadc->nowadc->m_NNPar.TrainPar.MaxTrainInputNo)) { train->Caption ="載入記憶體失敗!!"; return; } if(myadc->nowadc->BatchSimu((class_me "\\features_train.txt").c_str(),(class_me "\\classes_train.txt").c_str())) { train->Caption ="訓練資料分類完成!!目前辨識率為:" FloatToStr(myadc->nowadc->GetRecogRate()); } else { train->Caption ="訓練資料分類失敗!!"; } if (FileExists("result_train.txt")){DeleteFile("result_train.txt"); } if (FileExists("result.txt")){RenameFile("result.txt","result_train.txt"); } if (!myadc->nowadc->LoadNNWeight((class_me "\\NNParameter.txt").c_str())) { train->Caption ="載入NNParameter.txt失敗!!"; return; } if(!myadc->nowadc->AllocateMemory(myadc->nowadc->m_NNPar.TrainPar.MaxTrainInputNo)) { train->Caption ="載入記憶體失敗!!"; return; } if(myadc->nowadc->BatchSimu((class_me "\\features_test.txt").c_str(),(class_me "\\classes_test.txt").c_str())) { train->Caption ="測試資料分類完成!!目前辨識率為:" FloatToStr(myadc->nowadc->GetRecogRate()); } else { train->Caption ="測試資料分類失敗!!"; } if (FileExists("result_test.txt")){DeleteFile("result_test.txt"); } if (FileExists("result.txt")){RenameFile("result.txt","result_test.txt"); } */ FILE * features = fopen((class_me "\\features_unknown.txt").c_str(),"w"); char * types []= {"M_AREA","M_PERIMETER","M_ROUGHNESS","M_COMPACTNESS","M_FERET_ELONGATION","M_MEAN_PIXEL"}; float typev []= {0,0,0,0,0,0}; //myadc->nowblo->SetImgFormat(1); myadc->nowblo->SelectFeature((class_me "\\MyBlobFeatureListFile.txt").c_str()); myadc->nowblo->Select((class_me "\\MyFeatureSelectFile.txt").c_str()); myadc->nowblo->SetAnalyzedImgDir("Binaryimage3"); myadc->nowblo->Run((ExtractFilePath(Application->ExeName) "image3\\1.bmp").c_str(),intr,chg,0,defs); myadc->nowblo->SaveAnalysedImage("1.bmp"); int i ; for(i=0;i<=2000;i ) { } readimage_h("Binaryimage3\\1.bmp",eags,"height"); myadc->nowblo->OnlyBlobAnalysis("Binaryimage3\\1.bmp",0,defs); //取出特徵值 for(i=0;i<6;i ) { typev[i]=myadc->nowblo->GetFeatureValue(types[i]); } fprintf(features,"] ] .6f .6f .6f ]\n",(long)typev[0],(long)typev[1],typev[2],typev[3],typev[4],(long)typev[5]); fflush(features); fclose(features); if (!myadc->nowadc->LoadNNWeight((class_me "\\NNParameter.txt").c_str())) { train->Caption ="載入NNParameter.txt失敗!!"; FreeLibrary(hinst); FreeLibrary(hinst1); return; } if(!myadc->nowadc->AllocateMemory(myadc->nowadc->m_NNPar.TrainPar.MaxTrainInputNo)) { train->Caption ="載入記憶體失敗!!"; //trainf->Close() ; FreeLibrary(hinst); FreeLibrary(hinst1); return; } if (myadc->nowadc->BatchSimu((class_me "\\features_unknown.txt").c_str())) { train->Caption ="資料解析成功!!"; } else { train->Caption ="資料解析失敗!!"; //trainf->Close() ; FreeLibrary(hinst); FreeLibrary(hinst1); return ; } FILE * unknowns = fopen("result.txt","r"); while(!feof(unknowns)) { fscanf(unknowns, "d\n", &i); } fclose(unknowns); String xml2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> xml2=xml2 " xml2=xml2 " try { //傳送目前類別 xml = myadc->EchoService->executeTransaction("UAdcJudgeDefect",IntToStr(myadc->stat),"I",xml2); } catch ( ... ) { //trainf->Close() ; FreeLibrary(hinst); FreeLibrary(hinst1); return; } error_code = readxmlvaule(xml,"/trx/rtn_code"); myadc->stat ; if (error_code.Trim() != "0000000" ) { //trainf->Close() ; FreeLibrary(hinst); FreeLibrary(hinst1); return ; } FreeLibrary(hinst); FreeLibrary(hinst1); }
------
....... |
cat200188
中階會員 ![]() ![]() ![]() 發表:37 回覆:78 積分:68 註冊:2005-01-18 發送簡訊給我 |
//---------------------------------------------------------------------------
#include #pragma hdrstop #include "Unit1.h" #include "Unit2.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" Tmyadc *myadc; //--------------------------------------------------------------------------- __fastcall Tmyadc::Tmyadc(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- _di_aidiWSPortType Tmyadc::GetEchoService() { try { if (!FEchoService) { HTTPRIO1->QueryInterface(FEchoService); } } catch (Exception &exception) { } return FEchoService; } //--------------------------------------------------------------------------- String * Tmyadc::split(String ss,char k1) // 字串分割 { if (k1==' ') { // 若分隔字元為空白時,先對字串做處理 String s3; ss=ss.Trim(); // 先去除頭尾空白 for (int i=1;i<=ss.Length();i ) { if (ss.SubString(i,1)!=' ') { s3=s3 ss.SubString(i,1); }else{ if (ss.SubString(i-1,1)!= ' ') s3=s3 ss.SubString(i,1); } } ss=s3; } int n1 = 1; // 計算資料總筆數 for (int i=1; i<=ss.Length(); i ) { if (ss.SubString(i, 1) == k1) n1 ; } String *s2 = new String[n1 1];int pos; s2[0]=n1; for (int i=1; i<=n1; i ) { pos = ss.Pos(k1); if(pos == 0){ s2[i] = ss; }else{ s2[i] = ss.SubString(1, pos-1); ss = ss.SubString(pos 1, ss.Length()-1); } } return s2; } //--------------------------------------------------------------------------- void __fastcall Tmyadc::set1Click(TObject *Sender) { //Timer1->Enabled = false ; Timer1->Interval = ed1->Text.ToInt() ; //Timer1->Enabled = true ; } //--------------------------------------------------------------------------- void __fastcall Tmyadc::start1Click(TObject *Sender) { start1->Enabled = false ; end1->Enabled = true ; Timer1->Enabled = end1->Enabled ; delete HTTPRIO1 ; HTTPRIO1 = new THTTPRIO(this) ; HTTPRIO1->URL =url1->Text ; } //--------------------------------------------------------------------------- void __fastcall Tmyadc::end1Click(TObject *Sender) { start1->Enabled = true ; end1->Enabled = false ; Timer1->Enabled = end1->Enabled ; } //--------------------------------------------------------------------------- void __fastcall Tmyadc::FormCreate(TObject *Sender) { stat = 1 ; nowblo = new BlobAnalysis(); if (nowblo ==NULL) { ShowMessage("BlobAnalysis 物件創造失敗,程式將終止!!"); Application->Terminate() ; } nowadc = new ADC() ; if (nowadc ==NULL) { ShowMessage("ADC 物件創造失敗,程式將終止!!"); Application->Terminate() ; } hinst = LoadLibrary("xml.dll"); hinst1 = LoadLibrary("readimage.dll"); try { (FARPROC &)readxmlvaule =GetProcAddress(hinst,"readxmlvaule"); (FARPROC &)readimage_h =GetProcAddress(hinst1,"readimage_h"); } catch ( ... ) { ShowMessage("取得函式庫失敗,程式將終止!!"); Application->Terminate() ; } end1->Enabled = false; Timer1->Enabled = end1->Enabled; } //--------------------------------------------------------------------------- void __fastcall Tmyadc::FormClose(TObject *Sender, TCloseAction &Action) { delete nowblo ; delete nowadc ; FreeLibrary(hinst); FreeLibrary(hinst1); Action =caFree; } //--------------------------------------------------------------------------- void __fastcall Tmyadc::Timer1Timer(TObject *Sender) { if (myadc->EchoService) { String xml1 ="<?xml version=\"1.0\" encoding=\"UTF-8\"?> String xml; /*try { //傳送資料準備收圖 xml = EchoService->executeTransaction("UAdcDefect",IntToStr(stat),"I",xml1); } catch ( ... ) { return; } */ xml ="<?xml version=\"1.0\" encoding=\"UTF-8\"?> xml=xml " xml=xml " xml=xml " String error_code = readxmlvaule(xml,"/trx/rtn_code");-------->error處 //有error 重做 if (error_code.Trim() != "0000000" ) { return ; } String test_time = readxmlvaule(xml,"trx/test_time"); String lot_id = readxmlvaule(xml,"trx/lot_id"); String sheet_id = readxmlvaule(xml,"trx/sheet_id"); String chip_no = readxmlvaule(xml,"trx/chip_no"); String pox_x = readxmlvaule(xml,"trx/pox_x"); String pox_y = readxmlvaule(xml,"trx/pox_y"); String image_path = readxmlvaule(xml,"trx/image_path"); String recipe_id = readxmlvaule(xml,"trx/recipe_id"); TFileStream *fs ; try { fs = new TFileStream(ExtractFilePath(Application->ExeName) "image3\\1.jpg", fmCreate); myadc->IdHTTP1->HandleRedirects = true ; myadc->IdHTTP1->Get(image_path,fs); fs->Free() ; } catch ( ... ) { fs->Free() ; return; } TJPEGImage *myjpeg = new TJPEGImage; Graphics::TBitmap *mybmp = new Graphics::TBitmap ; myjpeg->LoadFromFile("image3\\1.jpg"); mybmp->Width =myjpeg->Width ; mybmp->Height =myjpeg->Height ; mybmp->Canvas->Draw(0,0,myjpeg); if (FileExists(ExtractFilePath(Application->ExeName) "image3\\1.bmp")) { DeleteFile(ExtractFilePath(Application->ExeName) "image3\\1.bmp"); } mybmp->SaveToFile(ExtractFilePath(Application->ExeName) "image3\\1.bmp"); DeleteFile(ExtractFilePath(Application->ExeName) "image3\\1.jpg"); //拆解recipe ,當不符合離開 TReplaceFlags Flags ; Flags = Flags < String *str = split(recipe_id ,'-'); if (str[0].ToInt() < 3 ) { return ; } String classstr = str[1]; String azostr = str[2]; String pepstr = str[3]; //當找不到資料離開 TLocateOptions options; //加入忽略字母大小寫,子字串符合找尋即找到 options = options << loCaseInsensitive << loPartialKey ; bool search1 = dm3->dclass->Locate("smemo",classstr,options); if (search1==false) { return ; } bool search2 = dm3->dazo->Locate("azomemo",azostr,options); if (search2==false) { return ; } bool search3 = dm3->dpep->Locate("pepmemo",pepstr,options); if (search3== false || dm3->dpepintr->AsString =="" ) { return ; } long intr = dm3->dpepintr->AsInteger ; double chg =dm3->dpepchg->Value ; long defs = dm3->dpepdefs->AsInteger ; long eags = dm3->dpepeags->AsInteger ; String class_me = dm3->dpeppeppath->AsString; //當迷發現路逕離開 if (class_me.Trim() == "") { return ; } FILE * features = fopen((class_me "\\features_unknown.txt").c_str(),"w"); char * types []= {"M_AREA","M_PERIMETER","M_ROUGHNESS","M_COMPACTNESS","M_FERET_ELONGATION","M_MEAN_PIXEL"}; float typev []= {0,0,0,0,0,0}; //myadc->nowblo->SetImgFormat(1); myadc->nowblo->SelectFeature((class_me "\\MyBlobFeatureListFile.txt").c_str()); myadc->nowblo->Select((class_me "\\MyFeatureSelectFile.txt").c_str()); myadc->nowblo->SetAnalyzedImgDir("Binaryimage3"); myadc->nowblo->Run((ExtractFilePath(Application->ExeName) "image3\\1.bmp").c_str(),intr,chg,0,defs); myadc->nowblo->SaveAnalysedImage("1.bmp"); int i ; for(i=0;i<=2000;i ) { } readimage_h("Binaryimage3\\1.bmp",eags,"height"); myadc->nowblo->OnlyBlobAnalysis("Binaryimage3\\1.bmp",0,defs); //取出特徵值 for(i=0;i<6;i ) { typev[i]=myadc->nowblo->GetFeatureValue(types[i]); } fprintf(features,"] ] .6f .6f .6f ]\n",(long)typev[0],(long)typev[1],typev[2],typev[3],typev[4],(long)typev[5]); fflush(features); fclose(features); if (!myadc->nowadc->LoadNNWeight((class_me "\\NNParameter.txt").c_str())) { train->Caption ="載入NNParameter.txt失敗!!"; return; } if(!myadc->nowadc->AllocateMemory(myadc->nowadc->m_NNPar.TrainPar.MaxTrainInputNo)) { train->Caption ="載入記憶體失敗!!"; return; } if (myadc->nowadc->BatchSimu((class_me "\\features_unknown.txt").c_str())) { train->Caption ="資料解析成功!!"; } else { train->Caption ="資料解析失敗!!"; return ; } FILE * unknowns = fopen("result.txt","r"); while(!feof(unknowns)) { fscanf(unknowns, "d\n", &i); } fclose(unknowns); String xml2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> xml2=xml2 " xml2=xml2 " try { //傳送目前類別 xml = myadc->EchoService->executeTransaction("UAdcJudgeDefect",IntToStr(myadc->stat),"I",xml2); } catch ( ... ) { return; } error_code = readxmlvaule(xml,"/trx/rtn_code"); myadc->stat ; if (error_code.Trim() != "0000000" ) { return ; } } } //---------------------------------------------------------------------------
------
....... |
cat200188
中階會員 ![]() ![]() ![]() 發表:37 回覆:78 積分:68 註冊:2005-01-18 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |