2013-04-16 62 views
1

我遇到過xutilityunhandled exceptionxutility未處理的異常錯誤

if (_Myproxy != 0) 
    { // proxy allocated, drain it 
    _Lockit _Lock(_LOCK_DEBUG); 

    for (_Iterator_base12 **_Pnext = &_Myproxy->_Myfirstiter; 
     *_Pnext != 0; *_Pnext = (*_Pnext)->_Mynextiter) 
     (*_Pnext)->_Myproxy = 0; <------- unhandled exception here 
    _Myproxy->_Myfirstiter = 0; 
} 

我沒有控制。這unhandled exception火車從

std::string BinarySearchFile::readT(long filePointerLocation, long sizeOfData) 
{ 
    try{ 
      if(binary_search_file){ 
       std::string data; 
       binary_search_file.seekp(filePointerLocation); 
       binary_search_file.seekg(filePointerLocation); 
       binary_search_file.read(reinterpret_cast<char *>(&data), sizeOfData); 

       return data; <------- branch into xutility and subsequent unhandled exception 

      }else if(binary_search_file.fail()){ 
       throw CustomException("Attempt to read attribute error"); 
      } 

    } 
    catch(CustomException &custom_exception){ // Using custom exception class 
      std::cout << custom_exception.what() << std::endl; 
    } 

} 

通常莖,該return將着手

std::string BinarySearchFile::read_data(long filePointerLocation, long sizeOfData){ 
    return readT(filePointerLocation, sizeOfData); 
} 

並隨後返回到原來的呼叫

attributeValue = data_file->read_data(index, size); 

我在做什麼錯?

+1

「未處理的異常」是一個非常絕望的診斷,你需要告訴你對它的瞭解。調試器向您顯示有關異常的詳細信息。一般來說,避免認爲這與xutility有關,你的程序破壞堆是標準的解釋。 –

+0

從我上面發佈的代碼(代碼),你認爲會損壞堆?我只是將二進制數據讀入'std :: string'並將其返回給調用者。 – Mushy

+0

你在某處毀了內存。你的[testcase](http://sscce.org)在哪裏? –

回答

2

data當您嘗試讀取字符串時,該字符串爲空。這會在某處破壞內存。

您應該添加data.resize(sizeOfData)分配空間,然後讀取到其緩衝區

binary_search_file.read(&data[0], sizeOfData); 
          ^^^ 

沒有到字符串對象本身。

+0

正確。 OP將'data'視爲一些現成的字節集合,但事實並非如此。這實際上是一個複雜的對象,內部和外部「包含」任何字節。 –

+0

是的,這有效,但沒有按照預期。我將不得不問另一個有關爲什麼二進制數據讀取不正確的問題。我使用'binary_search_file.write'(reinterpret_cast (&attribute),attribute.length()* 2)寫入文件「packard(三個尾部空格)」;'但是通過'binary_search_file.read(&data [0],sizeOfData );'XXX packard XXX',其中X是不需要的數據。 – Mushy