1

我在问题如何将字符串类型路径传递给 boost::filesystem:path 的构造函数中成功编译了程序?但是,输出结果显示的比预期的要多。

Folder status: 0
Bus error: 10

第一行是唯一的预期结果。我不明白为什么第二个来了?

我通过 gdb 获得以下调试信息:

(gdb) run
Starting program: /Users/ZL/Desktop/mtfsg 
Reading symbols for shared libraries ++++............................. done
Folder status: 0

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00007fff5fc005a0
0x00007fff9046aab7 in std::basic_string<char, std::char_traits<char>, 
std::allocator<char>>::~basic_string ()
(gdb) bt
#0  0x00007fff9046aab7 in std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >::~basic_string ()
#1  0x00000001000018dd in main () at m.cpp:12
4

1 回答 1

0

最后,我在我的代码中找到了错误点:

在 OSxFS 类(tfs.h 文件)中,方法“string ShowStatus()”的返回类型“string”不正确。此方法不需要返回值,因为它的功能只是在屏幕上打印目录(文件夹)状态。所以,我用“void”替换了“string”返回类型。

方法“string ShowStatus()”的原始代码

**string** ShowStatus()
    {
      boost::filesystem::file_status folderStatus =
       boost::filesystem::status(mFolderPath);

      try
      {
        cout<<"Folder status: "<<boost::filesystem::is_directory(folderStatus)<<endl;
      }
      catch(boost::filesystem::filesystem_error &e)
      {
        cerr<<"Error captured: "<<e.what()<<endl;
      }
    }

正确的代码:

**void** ShowStatus()
    {
      boost::filesystem::file_status folderStatus = 
       boost::filesystem::status(mFolderPath);

      try
      {
        cout<<"Folder status: "<<boost::filesystem::is_directory(folderStatus)<<endl;
      }
      catch(boost::filesystem::filesystem_error &e)
      {
        cerr<<"Error captured: "<<e.what()<<endl;
      }
    }
于 2012-08-22T08:32:07.567 回答