ofDirectory in Windows crash when listing c:/ (with a solution)

Hi all,
system crash when list root dir in windows
with code below it works, but I have to hack ofFileUtils.cpp (line 1000)

//--- WINDOWS RACK ---------------------- PHDCOSTA
// only add files that doenst crash windows with canRead()
// some protected files in windows throw ACCESS VIOLATION

try {
  ofFile f = ofFile(curPath.toString(), ofFile::Reference);

  // on windows this throw ACCESS VIOLATION for system protected files
  if(f.canRead()) {

    files.push_back(f);

  }
} catch( const std::exception &e) {

  printf(e.what()); printf("\n");
}
//--- WINDOWS RACK ---------------------- PHDCOSTA

// this was the original OF code
//files.push_back(ofFile(curPath.toString(), ofFile::Reference));

hope be usefull for someone
PH

mmh, that’s weird it should only create a reference ofFile not open it for reading. can you post the stacktrace when it crashes?

Hi arturo,

it’s crashing on ofRemove(…) when I’m filtering hidden files or filtering extensions.
at windows root (c:/) there is files like “pagefile.sys” (for example) that crash Poco

if(!showHidden){
	ofRemove(files, hiddenFile);
}

if(!extensions.empty() && !ofContains(extensions, (string)"*")){
	ExtensionComparator extensionFilter;
	extensionFilter.extensions = &extensions;
	ofRemove(files, extensionFilter);
}

you are right, dont need to read the file, if I catch exceptions throwed by ofRemove, it’s work

if(!showHidden){
	try {
		ofRemove(files, hiddenFile);
	} catch( const std::exception &e) {
		printf(e.what()); printf("\n");
	}
}

if(!extensions.empty() && !ofContains(extensions, (string)"*")){
	try {
		ExtensionComparator extensionFilter;
		extensionFilter.extensions = &extensions;
		ofRemove(files, extensionFilter);
	} catch( const std::exception &e) {
		printf(e.what()); printf("\n");
	}
}

here is my vs2008 screen

can you try if changing ofFile::isHidden() with:

//------------------------------------------------------------------------------------------------------------
bool ofFile::isHidden() const {
	try{
		return myFile.isHidden();
	}catch(Poco::Exception & e){
		return false;
	}
}

fixes it?

when I change isHidden() solves only for filtering hidden files
if you need to filter by extensions then crashes in ofFile::isFile or ofFile::isDirectory …

testing as this:

ofDirectory dir;
dir.setShowHidden(false);
dir.allowExt("sys"); dir.allowExt("jpg"); dir.allowExt("jpeg"); dir.allowExt("png");
dir.listDir("c:/");

using “sys” to force the problem (i’m developing some file dialog boxes for my gui)

I think that the problem is inside Poco. It’s always list all items from a path
and when we ask for properties of a specific file, Poco need to read the file in some way
that cause an access violation in windows

i’ve just uploaded some changes to github that should fix this if you want to test it

thanks Arturo, is working fine now
tried with this

//--------------------------------------------------------------
void testApp::draw(){
	float x = 10, y = 10;
	ofSetColor(255,0,0,255);
	for(int i = 0; i < folders.size(); i++) {
		y += 18;
		if(y > ofGetHeight()-20) {
			y = 20; x += 300;
		}
		ofDrawBitmapString("d-" + folders[i], x, y);
	}

	ofSetColor(0,0,255,255);
	for(int i = 0; i < files.size(); i++) {
		y += 18;
		if(y > ofGetHeight()-20) {
			y = 20; x += 300;
		}
		ofDrawBitmapString("f-" + files[i], x, y);
	}
}

//--------------------------------------------------------------
void testApp::keyPressed(int key){

	if(key != 'a' && key != 's') return;

	ofDirectory dir;
	if(key == 'a') {
		dir.setShowHidden(true);
		dir.allowExt("jpg"); dir.allowExt("png"); dir.allowExt("bak"); dir.allowExt("sys");
	} else if(key == 's') {
		dir.setShowHidden(true);
	}

	int nItems = dir.listDir("c:/");

	folders.clear();
	files.clear();

	for(int i = 0; i < nItems; i++) {
		ofFile f = dir[i];
		if(f.isDirectory()) {
			folders.push_back(f.getFileName());
		} else if(f.isFile()) {
			files.push_back(f.getFileName());
		}
	}
}

and here are the result screen