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 (
http://coliru.stacked-crooked.com/a/0bfab95a513ebed8 ):

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

namespace fs = boost::filesystem;

int main()
{
  try
  {
    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:
http://coliru.stacked-crooked.com/a/1b06dca057872762 and with
experimental/filesystem:
http://coliru.stacked-crooked.com/a/7bd5e73f1444619d ).

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:

https://svn.boost.org/trac/boost/ticket/11910

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

Cheers!

M



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net