Question

I'm using boost/filesystem for iterating directory and add them to Zip file on MacOSX + XCode3.

My original logic looks like this

path targetDir( "Volumes/data/some_directory" );
directory_iterator it( targetDir ), eod;

std::string filename;
std::string strPath;

// I tried both of two types of making loop
for( ; it != eod ; ++it )
{
    path const& p = *it;
    filename = p.filename();
    if( is_directory(p) )
    {
        strPath = strDirectory + filename + string("/");

        // Initially I wanted this logic to be recursive(these code block is a part of PackDirectory)
        PackDirectory( archive, strPath, lpszPackFile );
    }
    else if( is_regular_file(p) )
    {
        strPath = strDirectory + filename;
        // add this file to specified Zip file here
    }

}

then function returns here.

The problem arises after returning this function, specifically when destructor of directory_iterator is called, I guess. It seems it deletes invalid pointer, and receiving SIGABRT. Program sometimes crashes like below, and sometimes it freezes when I hit step over, XCode says "stepping over" but nothing proceeds with call stack disappearing. AND the point is, problem remains even if I don't do anything inside the loop, which means when variables are simply created and function returned.

for more information, call stack looks like this when program crashes.

#0  0x93f86c5a in __kill
#1  0x93f86c4c in kill$UNIX2003
#2  0x940195a5 in raise
#3  0x9402f6e4 in abort
#4  0x93f2c575 in free
#5  0x00134aea in dir_itr_close [inlined] at v2_operations.cpp:1300
#6  0x00134aea in ~dir_itr_imp [inlined] at operations.hpp:877
#7  0x00134aea in     checked_delete<boost::filesystem2::detail::dir_itr_imp<boost::filesystem2::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem2::path_traits> > > [inlined] at checked_delete.hpp:34
#8  0x00134aea in boost::detail::sp_counted_impl_p<boost::filesystem2::detail::dir_itr_imp<boost::filesystem2::basic_path<std::string, boost::filesystem2::path_traits> > >::dispose at v2_operations.cpp:78
#9  0x00136583 in boost::detail::sp_counted_base::weak_release at sp_counted_base_pt.hpp:97

It goes into ~dir_itr_imp, so it seems it reaches correct destructor after passing type checking.

Did I do something wrong with directory_iterator? If someone went through this problem, please let me know.

Was it helpful?

Solution

A signal that is raised in free does not necessarily point to a defect in the calling functions. An earlier and broken call to free could have corrupted malloc's free memory list, and thereby caused the error.

Try running your program in valgrind, which is more robust and will die immediately when corrupt memory allocation/deallocation is used. If valgrind is not an option, you can also try to test your code in a unit test independently of the rest of the program.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top