2013-11-03 16 views
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的工具可以幫助我進行調試?

回答

1

如果代碼訪問不屬於它的內存或不存在的內存,則只會發生段錯誤。由於CDiff位於堆上,因此您的進程擁有並可以訪問但前後尚未分配內存的前後可能存在內存。所以它是有道理的,沒有段錯誤。 (它也可能是爲其他變量分配給您的內存,因此您覆蓋了該變量,但直到後期才顯示出來。)

您可以打開malloc scribbling and guard malloc以幫助查找其中的某些變量的問題。您也可以使用Instrumentsclang static analyzer

+0

如果你還沒有升級到特立獨行者,你可以使用valgrind。否則,你可以在虛擬機上運行一些linux發行版並運行valgrind。 – bmargulies

+0

我已經升級到小牛隊。但是我可以使用Valgrind進行調試,並追蹤Xcode中很難跟蹤的錯誤。 – BRabbit27

相關問題