I’ve been looking into this problem for hours and tried all sorts of things, but everything results in the same – a SIGSEGV (from gdb; according to VC debug it’s a stack overflow…) at the point when a static class method gets called.
The culprit code:
void LEHelper::copySinglePath (std::string from_path, std::string to_path)
{
// first check if this path is a file or folder
if (!folderExists(from_path)) // is a file
{
FILE *fp = fopen(from_path.c_str(), "rb");
FILE *tp = fopen(to_path.c_str(), "wb");
if (fp && tp)
{
// read 1MB chunks from the file and copy to the new destination until finished
LEuchar bytes[1048576];
while (!feof(fp))
{
size_t read_num = fread(bytes, sizeof(LEuchar), 1048576, fp);
fwrite(bytes, sizeof(LEuchar), read_num, tp);
}
}
if (fp)
fclose(fp);
if (tp)
fclose(tp);
}
else // is a folder
{
// make a new directory at the "to" path to copy files into
#if defined(LE_OS_OSX)
mkdir(to_path.c_str(), S_IRWXO | S_IRWXG | S_IRWXU);
#elif defined(LE_OS_WIN)
mkdir(to_path.c_str());
#endif
// need to get all contents and recursively perform this method with correct pathing
LEArray<std::string> contents = getContentsOfDirectoryUsingFilter(from_path, LEH_DIR_BOTH);
std::string *current;
contents.initEnumerator();
while ((current = contents.nextObject()))
{
// first build the current file or folder path (and new path)
std::string current_path = from_path;
std::string new_path = to_path;
if (current->length() > 0)
{
current_path += LE_PATH_SEPARATOR + *current;
new_path += LE_PATH_SEPARATOR + *current;
}
// then copy as necessary --- this is where things go bad ---
copySinglePath(current_path, new_path);
}
}
}
And the call stack is:
#0 00000000 0x0040db8a in _alloca() (??:??)
#1 00403888 LEHelper::copySinglePath(from_path=..., to_path=...)
#2 00403AD1 LEHelper::copySinglePath(from_path=..., to_path=...)
#3 00403C8C LEHelper::copyPath(existing_path=..., new_path=..., ow=true)
#4 00402CD3 main(argc=1, argv=0x992c10)
In the program I’m testing this in it is called once successfully reaching the recursive point with all values correct (I’ve been checking string values with printf), but on calling copySinglePath() within it fails. Since the call stack is so small I find it hard to believe this is actually a stack overflow from too many recursions… but my understanding could be wrong.
In my researching for an answer I read most segmentation faults are caused by pointer issues, so I’ve tried changing the arguments for copySinglePath() to be pointers, allocating the passed in strings on the heap with new (so I have control over allocation, etc.), but that didn’t make any difference.
Also odd is that this exact same code runs perfectly on OSX. Because of this, I thought there may be something somehow wrong with my mingw setup so I’ve reinstalled it with the latest mingw-get and still nothing different. I even had the thought that my Visual Studio install files could somehow be being used (includes that is), so temporarily changed their folder in an attempt to make sure, and it still compiled etc. so that shouldn’t be it…
I’m at a complete loss now as to why this is happening. If anyone has any thoughts on what I might be doing wrong, please…
Thanks to all in advance
You may well need to lower the size of your chunks, or alloc them on the heap. The call stack is very small, but the stack includes local variables. The Windows stack is, by default, only 1MB from memory whereas the Unix stack is 8MB. Allocating a 1MB array off this stack is instant overflow. You need to alter your linker settings to permit a larger stack size.