Reputation: 6623
I've been debugging a code where it was pretty hard to spot one of my errors. I've declared and array like
char* Cdiff[320];
Nevertheless, when running the application in Xcode 5.0.1 it crashes on other part of the code that (according to me) doesn't have anything to do with that array.
A sample of the code I'm using is
...
...
//patchSize = 4, blockSize = 10
uchar *Cdiff = new uchar[(patchSize*patchSize)*2 * blockSize];
// FOR EACH BLOCK OF PATCHES (there are 'blockSize' patches in one block)
for (uint iBlock = 0; iBlock < nBlocks; iBlock++)
{
// FOR EACH PATCH IN THE BLOCK
for(uint iPatch = iBlock*blockSize; iPatch < (iBlock*blockSize)+blockSize; iPatch++)
{
// GET THE POSITION OF THE upper-left CORNER(row, col) AND
// STORE THE COORDINATES OF THE PIXELS INSIDE THE CURRENT PATCH (only the current patch)
uint iPatchV = (iPatch*patchStep)/camRef->getWidth();
uint iPatchH = (iPatch*patchStep)%camRef->getWidth();
for (uint pRow = iPatchV, pdRow = 0; pRow < iPatchV+patchSize; pRow++, pdRow++)
{
for (uint pCol = iPatchH, pdCol = 0; pCol < iPatchH+patchSize; pCol++, pdCol++)
{
patchPos.push_back(Pixel(pCol, pRow));
}
}
// GET THE RIGHT AND DOWN NEIGHBORS TO COMPUTE THE DIFFERENCES
uint offset = 0;
for (Pixel p : patchPos)
{
uint r = p.getY();
uint c = p.getX();
uchar pixelV = ((uchar*)camRef->getData())[r*imageW+c];
uint cRightNeighbor = c+patchStep;
uchar pixelVrightP = 0;
if (cRightNeighbor < imageW)
{
pixelVrightP = abs(pixelV - ((uchar*)camRef->getData())[r*imageW+cRightNeighbor]);
}
uint rDownNeighbor = r+patchStep;
uchar pixelVbelowP = 0;
if (rDownNeighbor < imageH)
{
pixelVbelowP = abs(pixelV - ((uchar*)camRef->getData())[rDownNeighbor*imageW+c]);
}
//---This is the right way to compute the index.
int checking = (iPatch%blockSize)*(patchSize*patchSize)*2 + offset;
//---This lines should throw a seg_fault but they don't
Cdiff[iPatch*(patchSize*patchSize)*2 + offset] = pixelVrightP;
Cdiff[iPatch*(patchSize*patchSize)*2 + offset+(patchSize*patchSize)] = pixelVbelowP;
offset++;
}
...
...
}
}
I was forgetting to use blockSize
in the computation of the index so in every iteration of the block it starts writing from the 0th position.
Can anyone explain me how/why is not reporting Xcode these kind of seg_faults properly? I actually had to test my code and debug it on linux so to be able to catch that error. Is there a tool in Xcode, similar to Valgrid, that can help me debugging?
Upvotes: 0
Views: 228
Reputation: 26395
You'll only get a segfault if your code accesses memory that does not belong to it or that does not exist. Because CDiff
is on the heap, it's likely that there is memory before and after it that your process owns and has access to, but which hasn't been allocated yet. So it makes sense that there's no segfault. (It could also be memory that has been allocated to you for some other variable, so you were overwriting that variable, but it didn't show up until later.)
You can turn on malloc scribbling and guard malloc to help find some of these issues. You can also use Instruments and the clang static analyzer.
Upvotes: 1