2015-01-04 56 views
0

下面的代碼工作正常,但是當用Valgrind的運行它會報告錯誤「大小8的無效讀」。Valgrind的報告上的boost ::文件系統大小爲8的無效讀::路徑:: PARENT_PATH()

#include <iostream> 
#include <boost/filesystem.hpp> 
#include <boost/program_options/options_description.hpp> 
#include <boost/program_options/parsers.hpp> 
#include <boost/program_options/variables_map.hpp> 

namespace po = boost::program_options; 

int main(int argc, char* argv[]) 
{ 
    po::options_description desc("Allowed options"); 
    desc.add_options() 
      ("report,r", po::value<std::string>(), "single input image"); 
    po::variables_map vm; 
    po::store(po::parse_command_line(argc, argv, desc), vm); 
    po::notify(vm); 
    if(vm.count("report")){ 
     boost::filesystem::path reportFile = boost::filesystem::path(vm["report"].as<std::string>()); 
     boost::filesystem::path reportPath = reportFile.parent_path(); 
     std::string reportParentPath = reportPath.string(); 
     std::cout << reportParentPath << std::endl; 
    } 
} 

當帶參數./boostFilesystemTest -r folder/file.html的PROGRAMM回報 '文件夾'(如預期)運行:

什麼的valgrind報告:

==23831== Memcheck, a memory error detector 
==23831== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al. 
==23831== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info 
==23831== Command: ./boostFilesystemTest -r folder/file.html 
==23831== 
folder 
==23831== Invalid read of size 8 
==23831== at 0x63CEBC0: wcscmp (in /lib64/libc-2.15.so) 
==23831== by 0x5B9E4C3: std::moneypunct<wchar_t, false>::~moneypunct() (in /usr/lib64/libstdc++.so.6.0.17) 
==23831== by 0x5B9E548: std::moneypunct<wchar_t, false>::~moneypunct() (in /usr/lib64/libstdc++.so.6.0.17)  
==23831== by 0x5B936B4: std::locale::_Impl::~_Impl() (in /usr/lib64/libstdc++.so.6.0.17)       
==23831== by 0x5B937EC: std::locale::~locale() (in /usr/lib64/libstdc++.so.6.0.17)        
==23831== by 0x636BC4E: __cxa_finalize (in /lib64/libc-2.15.so)             
==23831== by 0x5708272: ??? (in /usr/lib64/libboost_filesystem.so.1.49.0)           
==23831== by 0x400EDDE: _dl_fini (in /lib64/ld-2.15.so)               
==23831== by 0x636B8B0: __run_exit_handlers (in /lib64/libc-2.15.so)            
==23831== by 0x636B934: exit (in /lib64/libc-2.15.so)                
==23831== by 0x635545B: (below main) (in /lib64/libc-2.15.so)              
==23831== Address 0x6906668 is 0 bytes after a block of size 8 alloc'd            
==23831== at 0x4C2A147: operator new[](unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) 
==23831== by 0x5B9E1A9: std::moneypunct<wchar_t, false>::_M_initialize_moneypunct(__locale_struct*, char const*) (in /usr/lib64/libstdc++.so.6.0.17)                     
==23831== by 0x5B95BE6: std::locale::_Impl::_Impl(char const*, unsigned long) (in /usr/lib64/libstdc++.so.6.0.17) 
==23831== by 0x5B96689: std::locale::locale(char const*) (in /usr/lib64/libstdc++.so.6.0.17)      
==23831== by 0x571348E: boost::filesystem3::path::wchar_t_codecvt_facet() (in /usr/lib64/libboost_filesystem.so.1.49.0)                            
==23831== by 0x5714A34: boost::filesystem3::path::parent_path() const (in /usr/lib64/libboost_filesystem.so.1.49.0)                             
==23831== by 0x405CDF: main (main.cpp:19)                   
==23831==                           
==23831== Invalid read of size 8                      
==23831== at 0x63CEBC0: wcscmp (in /lib64/libc-2.15.so)               
==23831== by 0x5B9E3B3: std::moneypunct<wchar_t, true>::~moneypunct() (in /usr/lib64/libstdc++.so.6.0.17)   
==23831== by 0x5B9E438: std::moneypunct<wchar_t, true>::~moneypunct() (in /usr/lib64/libstdc++.so.6.0.17)   
==23831== by 0x5B936B4: std::locale::_Impl::~_Impl() (in /usr/lib64/libstdc++.so.6.0.17)       
==23831== by 0x5B937EC: std::locale::~locale() (in /usr/lib64/libstdc++.so.6.0.17) 
==23831== by 0x636BC4E: __cxa_finalize (in /lib64/libc-2.15.so) 
==23831== by 0x5708272: ??? (in /usr/lib64/libboost_filesystem.so.1.49.0) 
==23831== by 0x400EDDE: _dl_fini (in /lib64/ld-2.15.so) 
==23831== by 0x636B8B0: __run_exit_handlers (in /lib64/libc-2.15.so) 
==23831== by 0x636B934: exit (in /lib64/libc-2.15.so) 
==23831== by 0x635545B: (below main) (in /lib64/libc-2.15.so) 
==23831== Address 0x6906898 is 0 bytes after a block of size 8 alloc'd 
==23831== at 0x4C2A147: operator new[](unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) 
==23831== by 0x5B9DC19: std::moneypunct<wchar_t, true>::_M_initialize_moneypunct(__locale_struct*, char const*) (in /usr/lib64/libstdc++.so.6.0.17) 
==23831== by 0x5B95C33: std::locale::_Impl::_Impl(char const*, unsigned long) (in /usr/lib64/libstdc++.so.6.0.17) 
==23831== by 0x5B96689: std::locale::locale(char const*) (in /usr/lib64/libstdc++.so.6.0.17) 
==23831== by 0x571348E: boost::filesystem3::path::wchar_t_codecvt_facet() (in /usr/lib64/libboost_filesystem.so.1.49.0) 
==23831== by 0x5714A34: boost::filesystem3::path::parent_path() const (in /usr/lib64/libboost_filesystem.so.1.49.0) 
==23831== by 0x405CDF: main (main.cpp:19) 
==23831== 
==23831== 
==23831== HEAP SUMMARY: 
==23831==  in use at exit: 0 bytes in 0 blocks 
==23831== total heap usage: 539 allocs, 539 frees, 35,943 bytes allocated 
==23831== 
==23831== All heap blocks were freed -- no leaks are possible 
==23831== 
==23831== For counts of detected and suppressed errors, rerun with: -v 
==23831== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 2 from 2) 

我沒有看到這個「大小8的無效讀「來自以及代碼如何修復。有人有想法嗎?

我在openSuse 12.2上使用了boost 1.49.0。

編輯

LANG=C與運行Valgrind的測試時不會產生尺寸爲8的無效讀取。這個原因對我來說是未知的。

+0

要知道,有些編譯器和庫優化可能會導致無效的讀取和使用未分配的堆棧空間的臨時空間(可以驗證它不是由一個代碼錯誤引起的後忽略,比如寫靠近堆棧的頂部警告將錯誤的指針傳遞給庫)。 – user2672165

+1

它看起來與語言環境有關。嘗試用'LANG = C'或許去比較 – sehe

+0

你爲什麼認爲它與本地有關? 'LANG = C'確實不會顯示任何無效的讀取。 – Sjoerd222888

回答

-1

正如sehe評論說,這是真正的語言環境有關。在使用valgrind進行測試時,使用LANG=C運行時不會產生大小爲8的無效讀取。

+0

好吧,這使得錯誤更加精確,但它不能解釋它。 – akim

+0

確實。如果您有解釋請分享。 – Sjoerd222888

+0

對不起,我不是說我有答案。只是我也在尋找它。 – akim