File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Strange StackOverflowError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Strange StackOverflowError" Watch "Strange StackOverflowError" New topic

Strange StackOverflowError

Michael Herrmann
Ranch Hand

Joined: Dec 06, 2003
Posts: 60
I'm currently writing a class called FileIterator that implements the Iterator interface and enables to iterate over all files, dirs, files in those dirs etc. in a specific directory. As you can see, you can provide a FileFilter or a FilenameFilter to filter files you don't want to be returned. The strange thing is: if I specify a filter that somehow invokes a method on one of FileFilter or FilenameFilter's accept() args, a StackOverflowError is thrown(see WorkingFileiteratorTest and NotWorkingFileIteratorTest below). What's even more strange is is the fact, that the StackOverflowError is not thrown immediately and, if the number of files in the specified directory is small enough, everything works just fine. Sorry for the long listing, you don't have to read the whole code, the interesting parts lie in WorkingFileiteratorTest and NotWorkingFileIteratorTest. a StackOverflowError after having found some files):

Sorry for this long post and thanks if vou'ye managed to get this far
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

The problem is in this method:

If the filter returns true, then we return "lastFile", and everything's OK. But if the filter returns "false", then this method calls itself recursively. If there are many non-matching files, then you'll get many recursive calls, and eventually you'll run out of stack space and get the exception.

You could rewrite it non-recursively something like this:

[Jess in Action][AskingGoodQuestions]
Michael Herrmann
Ranch Hand

Joined: Dec 06, 2003
Posts: 60
You are my hero
I suspected the problem to be somewhere completely else.
I have to rewrite it nevertheless, because hasNext doesn't work properly. This class was a work in progress and the ability to add Filters was not included in the first running version, so it isn't suited for being able to filter files(I noticed that while searching for the error's cause after having opened this thread). I'll rewrite it and post it in here, maybe someone finds it useful(I definitely think it is).

Thank you very much,
Michael Herrmann
[ September 26, 2004: Message edited by: Michael Herrmann ]
Michael Herrmann
Ranch Hand

Joined: Dec 06, 2003
Posts: 60
Here is the rewritten version that doesn't produce the StackOverflowError mentioned above when traversing my whole HDD(122.000 files, 12.000 directories) or when FileIterator.remove()-ing a directory that contains 23.000 files in 1.600 directories:

I agree. Here's the link:
subject: Strange StackOverflowError
It's not a secret anymore!