2009-07-16 134 views
1

我是一個相當新的程序員,所以請耐心等待。我使用VC++ 2008「msvcp90d.dll」中的未處理的異常?

我正在從我的節目這個錯誤時,Z projection.exe

在0x68bce2ba(msvcp90d.dll)未處理的異常:0000005:訪問衝突寫入位置0x00630067。

電腦然後把我帶到這個頁面的代碼,看起來相當混亂,我絕對沒寫。它指出,這部分代碼爲有問題的代碼(由「< - 計算機指向該行」 maerked):

public: 
_CRTIMP2_PURE static size_t __CLRCALL_OR_CDECL _Getcat(const facet ** = 0, 
    const locale * = 0) 
{ // get category value, or -1 if no corresponding C category 
    return ((size_t)(-1)); 
} 

_CRTIMP2_PURE void __CLR_OR_THIS_CALL _Incref() 
{ // safely increment the reference count 
    _BEGIN_LOCK(_LOCK_LOCALE) 
     if (_Refs < (size_t)(-1)) 
      ++_Refs;  <-computer points to this line 
    _END_LOCK() 
} 

_CRTIMP2_PURE facet *__CLR_OR_THIS_CALL _Decref() 
{ // safely decrement the reference count, return this when dead 
    _BEGIN_LOCK(_LOCK_LOCALE) 
    if (0 < _Refs && _Refs < (size_t)(-1)) 
     --_Refs; 
    return (_Refs == 0 ? this : 0); 
    _END_LOCK() 
} 

我已經把範圍縮小到代碼在我自己的程序行很可能導致崩潰(代碼四號線開始以「指數」,還階段= 1):

stringstream index; 
string fileName = ""; 
index.str("");// 

index << setw(3) << setfill('0') << stage - 1; 

fileName = "positive Z topography-" + index.str() + ".txt"; 

這令我感到困惑的事情是,我複製並粘貼此代碼從另一個程序中我寫道,已經運行成百上千時間沒有任何麻煩,所以永遠。

編輯:這裏是整個代碼。

#include <iostream> 
#include <fstream> 
#include <sstream> 
#include <string> 
#include <iomanip> 

using namespace std; 

int main() 
{ 
    int dim = 100; 
    int steps = 9; //step 0 = 1, steps = actual steps + 1 
    int spread = 5; //number of points averaged to get a slope, must be an odd number 
    int halfSpread = (spread - 1)/2; //redefine for eazier use in program (better efficency) 

    char * partMap = new char [dim * dim * dim]; // cad data 

    // positive and negitive denote the x direction the check is moving in. Positive is 0 -> x, negitive is x -> 0. 
    unsigned short int * positiveProjection = new unsigned short int [dim * dim]; //projection arrays 
    unsigned short int * negitiveProjection = new unsigned short int [dim * dim]; 

    unsigned short int * negitiveThickness = new unsigned short int [dim * dim]; 

    double * negitiveXGradient = new double [dim * dim]; 
    double * negitiveYGradient = new double [dim * dim]; 

    stringstream index; 
    string fileName; 

    ifstream txtFile; 
    txtFile.open("3D CAD Part.txt"); 
    txtFile.read(partMap, dim * dim * dim); 
    txtFile.close(); 


    for (int stage = 1; stage < steps; stage++) 
    { 

     cout << "stage " << stage << endl; 

     //z axis projections 
     //projection order is along x then along y, during each step, along z in both directions 

     int k = 0; // z axis loop variable 

     for (int j = 0; j < dim; j++) 
     { 
      for (int i = 0; i < dim; i++) 
      { 
       k = 0; 

       while ((k != dim) && partMap[dim * ((dim - 1 - k) + dim * i) + j] < stage) 
        k++; 
       positiveProjection[dim * k + j] = k; 

       k = dim; 

       while ((k != 0) && partMap[dim * ((dim - 1 - (k - 1)) + dim * i) + j] < stage) 
        k--; 
       negitiveProjection[dim * k + j] = i; 

       while ((k != 0) && partMap[dim * ((dim - 1 - (k - 1)) + dim * i) + j] >= stage) 
        k--; 
       negitiveThickness[dim * k + j] = negitiveProjection[dim * k + j] - k; 
      } 
     } 

     // negitive dz/dx gradient 
     for (int j = 0; j < dim; j++) 
     { 

      //first loop to handle the first edge gradients 
      for (int i = 0; i < halfSpread; i++) 
       negitiveXGradient[(j * dim) + i] = (double(negitiveProjection[(j * dim) + halfSpread + i]) - double(negitiveProjection[j * dim]))/(halfSpread + i); // untested 

      //second loop to handle the main middle section 
      for (int i = halfSpread; i < dim - halfSpread; i++) 
       negitiveXGradient[(j * dim) + i] = (double(negitiveProjection[(j * dim) + i + halfSpread]) - double(negitiveProjection[(j * dim) + i - halfSpread]))/ (spread - 1); // untested 

      //third loop to handle the end edge gradients 
      for (int i = dim - halfSpread; i < dim; i++) 
       negitiveXGradient[(j * dim) + i] = (double(negitiveProjection[(j * dim) + dim - 1]) - double(negitiveProjection[j * dim + i - halfSpread]))/((dim - 1) - i + halfSpread); // untested 
     } 

     // negitive dz/dy gradient 
     for (int i = 0; i < dim; i++) 
     { 
      //first loop to handle the first edge gradients 
      for (int j = 0; j < halfSpread; j++) 
       negitiveYGradient[(j * dim) + i] = (double(negitiveProjection[((j + halfSpread) * dim) + i]) - double(negitiveProjection[i]))/(halfSpread + j); // untested 

      //second loop to handle the main middle section 
      for (int j = halfSpread; j < dim - halfSpread; j++) 
       negitiveYGradient[(j * dim) + i] = (double(negitiveProjection[((j + halfSpread) * dim) + i]) - double(negitiveProjection[((j - halfSpread) * dim) + i]))/ (spread - 1); // untested 

      //third loop to handle the end edge gradients 
      for (int j = dim - halfSpread; j < dim; j++) 
       negitiveYGradient[(j * dim) + i] = (double(negitiveProjection[(dim * (dim - 1)) + i]) - double(negitiveProjection[((j - halfSpread) * dim) + i]))/((dim - 1) - j + halfSpread); // untested 
     } 

     fileName = ""; // reset string and stringstream 
     index.str("");// 

     index << setw(3) << setfill('0') << stage - 1; // set index, index is -1 of stage due to the program structure 

     fileName = "positive Z topography-" + index.str() + ".txt"; 

     ofstream outputFile1(fileName.c_str(), std::ios::binary | std::ios::out); 
     outputFile1.write(reinterpret_cast<const char*>(positiveProjection), streamsize(dim * dim * sizeof(unsigned short int))); 
     outputFile1.close(); 

     fileName = "negitive Z topography-" + index.str() + ".txt"; 

     ofstream outputFile2(fileName.c_str(), std::ios::binary | std::ios::out); 
     outputFile2.write(reinterpret_cast<const char*>(negitiveProjection), streamsize(dim * dim * sizeof(unsigned short int))); 
     outputFile2.close(); 

     fileName = "negitive Z thickness-" + index.str() + ".txt"; 

     ofstream outputFile4(fileName.c_str(), std::ios::binary | std::ios::out); 
     outputFile4.write(reinterpret_cast<const char*>(negitiveThickness), streamsize(dim * dim * sizeof(unsigned short int)));  
     outputFile4.close(); 

     fileName = "negitive Z X gradient-" + index.str() + ".txt"; 

     ofstream outputFile5(fileName.c_str(), std::ios::binary | std::ios::out); 
     outputFile5.write(reinterpret_cast<const char*>(negitiveXGradient), streamsize(dim * dim * sizeof(double))); 
     outputFile5.close(); 

     fileName = "negitive Z Y gradient-" + index.str() + ".txt"; 

     ofstream outputFile6(fileName.c_str(), std::ios::binary | std::ios::out); 
     outputFile6.write(reinterpret_cast<const char*>(negitiveYGradient), streamsize(dim * dim * sizeof(double))); 
     outputFile6.close(); 
    } 
} 

好了,一切都很好,直到代碼的最後一塊,我開始我的輸出結果的硬盤驅動器。我使用VC++中的斷點和最後一個斷點在錯誤發生之前成功通過(我發佈的第二個代碼塊)。顯然,HTML也不喜歡我的C++代碼。

哦,還有,省下經過循環的麻煩......他們應該沒問題......有十幾個循環在那裏,你不必擔心他們發生了什麼,他們非常安全。我只有最後一塊的問題。

+0

這是代碼從您的程序中原樣複製嗎? – 2009-07-16 19:11:23

+0

這幾乎和我的程序一樣,除了我之前聲明的stringstream索引和字符串文件名外。 有沒有變數擔心,所有你需要知道的變量是階段= 1(我說)。 上面的大塊代碼不是我的。我沒有第一個線索爲什麼它存在或有什麼問題只有我的調試器說它不好... – Faken 2009-07-16 19:42:49

+0

這段代碼是類的一部分,索引和文件名是成員嗎?這是我認爲索引會員數據無效的唯一原因。除非你在函數中有更早的代碼,你可以發佈整個例程嗎? – 2009-07-16 19:56:58

回答

3

即使出現問題與最後一個塊到出現不一定意味着你的循環沒有錯誤。如果你在任何一個陣列上跳出界限,你可能會在以後再看到它。

在第內部while循環中,您有

while ((k != dim) && partMap[dim * ((dim - 1 - k) + dim * i) + j] < stage) 
    k++; 

這意味着可能在與while循環結束k能有值dim。然後下一行確實

positiveProjection[dim * k + j] = k; 

將出界的positiveProjection任何j,因爲你試圖用指標和dim*dim + j它只有尺寸dim*dim

1

我是一個相當新的程序員,所以請 熊我在這個。

好的。我保證不取笑你;)

計算機,然後把我帶到這個 頁面的代碼

你可能意味着Visual Studio調試器爲您帶來這條線。您可以使用「堆棧跟蹤」功能來查找發生問題的確切位置:單擊菜單調試 - > Windows - >調用堆棧。

但是,我在代碼中找不到任何錯誤。這個簡單的應用程序完美的作品:

int main() 
{ 

    std::stringstream index; 
    std::string fileName = ""; 
    index.str("");// 
    int stage = 1; 
    index << std::setw(3) << std::setfill('0') << stage - 1; 
    fileName = "positive Z topography-" + index.str() + ".txt"; 
    std::cout << "Done with test.\n"; 
    return 0; 
} 

因此,爲了幫助你,我們需要看到更多的代碼...