Boost logo

Boost Users :

Subject: [Boost-users] Filesystem recursive iterator issues
From: mlimber (mlimber_at_[hidden])
Date: 2016-10-24 06:22:40

I have this code, which does a simple recursive directory iteration and
prints each directory entry and then does it again ( ):

#include <boost/filesystem.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>

namespace fs = boost::filesystem;

int main()
    const auto iter = fs::recursive_directory_iterator{ "." };
    for( const auto& entry : boost::make_iterator_range( iter, {} ) )
      std::cout << entry << "\n";

    // Rinse and repeat
    std::cout << "\n";

    for( const auto& entry : boost::make_iterator_range( iter, {} ) ) //
CRASH -- Replace 'iter' with 'fs::recursive_directory_iterator{"."}' and it
will work.
    //for( const auto& entry : boost::make_iterator_range(
fs::recursive_directory_iterator{"."}, {} ) )
      std::cout << entry << "\n";
  catch( const std::exception& e )
    std::cerr << e.what() << "\n";
  catch( ... )
    std::cerr << "Unknown exception.\n";

As you can see at Coliru, it crashes. (It also crashes on Visual Studio
2012 and with Boost 1.55.0 and on Coliru with <experimental/filesystem>
instead of Boost.)

Similarly, using a filesystem iterator with std::distance() fails when the
iterator passed in is used again later (with boost: and with
experimental/filesystem: ).

It seems that something in the state of the *constant* iterator gets
confused or corrupted after it is advanced to the end. I have a workaround
for it commented out in the code.

I found this possibly related issue:

Am I missing something? Should I file a new bug report?



Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at