Xcode 5.0.1中未报告分段错误

时间:2013-11-03 16:48:31

标签: c++ segmentation-fault xcode5

我一直在调试代码,很难发现我的一个错误。 我已声明和数组像

char* Cdiff[320];

然而,当在Xcode 5.0.1中运行应用程序时,它会崩溃代码的其他部分(据我所知)与该数组没有任何关系。

我正在使用的代码示例是

...
...
//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++;
        }

        ...
        ...

    }
}         

我忘记在索引的计算中使用blockSize所以在块的每次迭代中它都从第0个位置开始写入。

任何人都可以解释我如何/为什么不正确报告Xcode这些seg_faults?我实际上必须测试我的代码并在linux上调试它,以便能够捕获该错误。 Xcode中有一个类似于Valgrid的工具可以帮我调试吗?

1 个答案:

答案 0 :(得分:1)

如果您的代码访问不属于它或不存在的内存,您将只会遇到段错误。因为CDiff在堆上,所以很可能在你的进程拥有并且有权访问之前和之后都有内存,但尚未分配。因此,没有段错误是有意义的。 (它也可能是为某些其他变量分配给你的内存,所以你要覆盖该变量,但直到稍后它才会显示。)

您可以启用malloc scribbling and guard malloc以帮助查找其中一些问题。您还可以使用Instrumentsclang static analyzer