“msvcp90d.dll”中未处理的异常?
我是一个相当新的程序员,所以请耐心等待。 我正在使用 VC++ 2008。
我的程序出现此错误:
zproject.exe 中 0x68bce2ba (msvcp90d.dll) 处出现未处理的异常:0xC0000005:写入位置 0x00630067 时出现访问冲突。
然后计算机将我带到这一页代码,它看起来相当混乱,而且绝对不是我写的。 它指出这部分代码是有问题的代码(由“<-计算机指向这一行”标记):
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++ 代码。
哦,还有,省去你自己穿过循环的麻烦……它们应该没问题……那里有大约十几个循环,你不需要担心它们里面发生了什么,它们非常安全。 我只对最后一块有问题。
I'm a fairly new programmer so please bear with me on this. I am using VC++ 2008.
I am getting this error from my program:
Unhandled exception at 0x68bce2ba (msvcp90d.dll) in z projection.exe: 0xC0000005: Access violation writing location 0x00630067.
The computer then brings me to this page of code which looks rather confusing and something I definitely did not write. It points to this section of code as being the offending code (maerked by "<-computer points to this line"):
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()
}
I have narrowed it down to the line of code in my own program that is likely causing the crash (4th line of code starting with "index", also stage = 1):
stringstream index;
string fileName = "";
index.str("");//
index << setw( 3 ) << setfill( '0' ) << stage - 1;
fileName = "positive Z topography-" + index.str() + ".txt";
The thing that baffles me is that I copied and pasted this code out of another program i wrote that has run hundreds of times without any trouble what so ever.
Edit: here is the entire code.
#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();
}
}
Ok, everything is fine until the last chunk of code where I start to output my results to hard drive. I used the breakpoints in VC++ and the last breakpoint to pass through successfully before an error is at the point mentioned earlier (the second block of code I posted). And apparently HTML doesn't like my c++ code either.
Oh, also, save yourself the trouble of going through the loops...they should be fine...there's like a dozen loops in there, you don't need to worry about whats going on in them, they are pretty safe. I only have problems with the last chunk.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使最后一个块出现问题并不一定意味着循环中没有错误。 如果您超出任何阵列的范围,您可能直到稍后才会收到任何指示。
在第一个内部
while
循环中,这意味着当完成
while
时,k
可能具有值dim
> 循环。 然后,下一行将超出任何
j
的positiveProjection
范围,因为您尝试使用dim*dim + j
进行索引它只有维度dim*dim
。Even though the problems appears to arise with the last chunk doesn't necessarily mean that there are no errors in your loops. If you go out of bounds on any of your arrays you may not get any indication about it until later on.
In the first interior
while
loop you havewhich means that potentially
k
could have the valuedim
when finished with thewhile
loop. The next line then doeswhich will go out of bounds in
positiveProjection
for anyj
since you're trying to index withdim*dim + j
and it only has dimensiondim*dim
.好的。 我保证不会取笑你;)
您可能是说 Visual Studio 调试器将您带到这一行。 您可以使用“堆栈跟踪”功能来查找出现问题的确切位置:单击菜单“调试”->“调试”。 窗口 -> 调用堆栈。
但是,我在您的代码中找不到任何错误。 这个简单的应用程序运行得很好:
所以为了帮助您,我们需要查看您的更多代码......
Ok. I promise not to make fun of you ;)
You probably mean that the Visual Studio debugger brings you to this line. You can use the 'stack trace' feature to find the exact location where things go wrong: click menu Debug -> Windows -> Call Stack.
However, I can't find anything wrong in your code. This simple application works perfectly:
So in order to help you, we need to see more of your code...