Необработанное исключение в "msvcp90d.dll"? - PullRequest
1 голос
/ 16 июля 2009

Я довольно новый программист, поэтому, пожалуйста, потерпите меня. Я использую VC ++ 2008.

Я получаю эту ошибку из моей программы:

Необработанное исключение в 0x68bce2ba (msvcp90d.dll) в z projection.exe: 0xC0000005: Место записи нарушения прав доступа 0x00630067.

Затем компьютер выводит меня на эту страницу кода, которая выглядит довольно запутанно и что-то, что я точно не написал. Он указывает на этот раздел кода как код, вызывающий сбой (отображается как «<-компьютер указывает на эту строку»): </p>

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()
}

Я сузил его до строки кода в моей собственной программе, которая, вероятно, вызывает сбой (4-я строка кода, начинающаяся с «index», также stage = 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 ++.

О, кроме того, избавьте себя от необходимости проходить через петли ... они должны быть в порядке ... там как дюжина петель, вам не нужно беспокоиться о том, что происходит в них, они довольно безопасно У меня проблемы только с последним куском.

Ответы [ 2 ]

3 голосов
/ 17 июля 2009

Даже если проблемы возникают с последним блоком, это не обязательно означает, что в ваших циклах нет ошибок. Если вы выходите за пределы какого-либо из ваших массивов, вы можете не получить никаких сведений об этом, пока позже.

В первом внутреннем while цикле у вас есть

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

, что означает, что потенциально k может иметь значение dim после завершения цикла while. Следующая строка тогда делает

positiveProjection[dim * k + j] = k;

, который выходит за границы в positiveProjection для любого j, так как вы пытаетесь индексировать с помощью dim*dim + j, и он имеет только измерение dim*dim.

1 голос
/ 16 июля 2009

Я довольно новый программист, поэтому, пожалуйста, потерпите меня на этом.

Ok. Обещаю не смеяться над тобой;)

Затем компьютер подводит меня к этому. страница кода

Вы, вероятно, имеете в виду, что отладчик 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;
}

Итак, чтобы помочь вам, нам нужно увидеть больше вашего кода ...

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...