【问题标题】:Segmentation fault in boost file system pathboost文件系统路径中的分段错误
【发布时间】:2019-05-23 07:37:34
【问题描述】:

我在为一组文件生成哈希的验证类中有一个成员函数。

代码编译正确,但当软件到达散列函数时,它会引发段错误。我一直在寻找年龄,我只是看不到它。这似乎与路径向量有关,也可能与复制功能有关。

Program received signal SIGSEGV, Segmentation fault.
0x000055c84fbbc058 in std::vector<boost::filesystem::path, std::allocator<boost::filesystem::path> >::push_back(boost::filesystem::path const&) ()
(gdb) 
Single stepping until exit from function _ZNSt6vectorIN5boost10filesystem4pathESaIS2_EE9push_backERKS2_,
which has no line number information.

Program terminated with signal SIGSEGV, Segmentation fault.

gdb 的输出

代码如下:

#pragma once
#include <boost/filesystem.hpp>

#include <cryptopp/cryptlib.h>

#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <cryptopp/md5.h>
#include "cryptopp/hex.h"
#include "cryptopp/files.h"

class Verify {
    private:
    typedef std::vector<boost::filesystem::path> vec;
    vec v;
    public:
    bool verify_files();
    bool generate_hash(boost::filesystem::path source);
};

功能实现

bool Verify::generate_hash(boost::filesystem::path source) {
    try {
    if (boost::filesystem::exists(source)) {                                                   //make sure path is valid
      if (boost::filesystem::is_regular_file(source)) {                                        //no regular files
        std::cout << "Please enter base path of directory only." << std::endl;
        return false;
      }
      else if (boost::filesystem::is_directory(source)){                                       //process directory here
        std::cout << "Scanning " << source << std::endl;

        std::copy(boost::filesystem::recursive_directory_iterator(source), boost::filesystem::recursive_directory_iterator(), std::back_inserter(this->v));
        std::sort(v.begin(), v.end());

         for (vec::const_iterator it (v.begin()); it != v.end(); ++it){
           std::string result;
           if (boost::filesystem::is_regular_file(*it)) {
               Weak::MD5 hash;
               FileSource((*it).string().c_str(), true, new HashFilter(hash, new HexEncoder(new StringSink(result), false)));
            }
            std::cout << "Hashing: " << *it << " - " << result << std::endl; 
        }
      } 
    }
    } catch (const boost::filesystem::filesystem_error& e){
        std::cout << e.what() << std::endl;
        return false;
    }
    return true;
}

【问题讨论】:

    标签: c++ boost segmentation-fault


    【解决方案1】:

    你的错误在这一行:

    std::copy(boost::filesystem::recursive_directory_iterator(source), boost::filesystem::recursive_directory_iterator(), std::back_inserter(this->v));
    

    您应该在此处进行手动循环以进一步调查(如果这不能消除问题)

    【讨论】:

    • for(auto& p: boost::filesystem::recursive_directory_iterator(source)){ std::cout v.push_back(p.path());尝试了这个,但仍然在推回功能上出现 seg 错误
    猜你喜欢
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-24
    相关资源
    最近更新 更多