1

I am using Boost 1.62 to map a file into memory with boost/iostreams/device/mapped_file.hpp

It works for most of the files, but certain files will lead to a program crash. I am on a Windows 10 x64 system, files like NTUSER.DAT.LOG, although I can open them with an editor and view the content.

Here is the code I use to open the file:

uint64_t read_directory(char* in_path) {
    struct _stat64 fileinfo;
    const char* filename;
    int result;

    int counter = 0;

    boost::iostreams::mapped_file_source file;

    for (auto it : recursive_directory_range(in_path)) {

        string filename_s = it.path().string();
        filename = filename_s.c_str();
        result = _stat64(filename, &fileinfo);
        // cout << result << endl;

        if (boost::filesystem::is_directory(it) || fileinfo.st_size == 0) {
            continue;
        }
        if (boost::filesystem::is_regular_file(it)) {

            file.open(filename, numberOfBytes);
        }

    if (file.is_open()) {

        int* data = (int*)file.data();
        file.close();
    }

    counter++;
    if (counter % 1000 == 0) {
        cout << counter << endl;
    }
}

The recursive walk is taken from here: How do you iterate through every file/directory recursively in standard C++?

After trying to access this particular file the program crashed with the following message in the debugger:

boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::ios_base::failure> > at memory location 0x0000004529D2F118.

The program runs as an user with Administrator permissions, but that should not be the problem, as opening in Notepad++ reveals the file contents. What am I doing wrong?

Edit: I think it might have something to do when the file is in use by another process. I tried it with gpodder, while it's open, I can't map the file. Once I close the program I can do it again. Is there a way to check this beforehand?

fsp
  • 515
  • 6
  • 21
  • Not sure if it relates to your issue, but when you call ````file.close()```` you are freeing the mapping that your ````data```` ptr is now pointing to. I assume you are reading the data somewhere in that block? – mascoj Nov 27 '17 at 17:59
  • tried it without doing the `int* data = (int*)file.data();` line; still same error – fsp Nov 27 '17 at 18:01
  • have you tried catching the error and showing it – sehe Nov 27 '17 at 19:23
  • it's in the post above. – fsp Nov 27 '17 at 20:07
  • added some info about file lock, maybe this will help – fsp Nov 28 '17 at 12:27

0 Answers0