0
我一直在調試一個代碼,很難發現我的一個錯誤。 我已經聲明和數組一樣Xcode 5.0.1中沒有報告分段錯誤
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++;
}
...
...
}
}
我忘記在指數的計算中它開始從第0位寫入塊的每一次迭代使用blockSize
左右。
任何人都可以解釋我/爲什麼不正確報告Xcode這些seg_faults?我實際上不得不測試我的代碼並在Linux上進行調試,以便能夠捕獲該錯誤。 Xcode中是否有類似於Valgrid的工具可以幫助我進行調試?
如果你還沒有升級到特立獨行者,你可以使用valgrind。否則,你可以在虛擬機上運行一些linux發行版並運行valgrind。 – bmargulies
我已經升級到小牛隊。但是我可以使用Valgrind進行調試,並追蹤Xcode中很難跟蹤的錯誤。 – BRabbit27