• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strange StackOverflowError

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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.

FileIterator.java:


FilenameFilter.java:


NotWorkingFileiteratorTest.java(Throws a StackOverflowError after having found some files):


WorkingFileiteratorTest.java:


Sorry for this long post and thanks if vou'ye managed to get this far
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Michael Herrmann
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic