I am using Sphinx to parse grammar files.
I have a function that loads all the grammar files. This function is called at the start of the program and every time the user requests a reload
. When called initially it works. The StackOverflowError
arises when subsequent calls are made to this function. The particular line in the function that causes the error is the gram.listRuleNames()
line, specifically the method of RuleGrammar listRuleNames()
.
Does anyone have an idea as to why this is occurring?
The function:
BaseRecognizer recognizer;
public static void main(String[] args) {
recognizer = new BaseRecognizer();
recognizer.allocate();
LoadAllGrammars();
Thread.sleep(1000);
LoadAllGrammars();
recognizer.deallocate();
}
private static void LoadAllGrammars() throws IOException {
RuleGrammar[] gramss = recognizer.listRuleGrammars();
for (RuleGrammar rg : gramss) {
recognizer.deleteRuleGrammar(rg);
}
recognizer.commitChanges();
RuleGrammar gram = null;
URL dir = new File("grams").toURI().toURL();
gram = recognizer.loadJSGF(dir, "main", true, true, null);
String[] names = gram.listRuleNames();
//the rest of the code in the function is irrelevant to the question
}
The error:
Exception in thread "Thread-23" java.lang.StackOverflowError
at java.util.HashMap$KeyIterator.<init>(Unknown Source)
at java.util.HashMap.newKeyIterator(Unknown Source)
at java.util.HashMap$KeySet.iterator(Unknown Source)
at java.util.AbstractCollection.toArray(Unknown Source)
at com.sun.speech.engine.recognition.BaseRuleGrammar.listRuleNames(BaseRuleGrammar.java:266)
at com.sun.speech.engine.recognition.BaseRecognizer.loadFullQualifiedRules(BaseRecognizer.java:958)
at com.sun.speech.engine.recognition.BaseRecognizer.loadImports(BaseRecognizer.java:938)
at com.sun.speech.engine.recognition.BaseRecognizer.loadFullQualifiedRules(BaseRecognizer.java:980)
After some debugging I have noticed that recognizer.deleteRuleGrammar(rg)
isn't deleting the old grammars.
I have narrowed it down to the line loadJSGF()
which when called twice results in the StackOverflowError
. I would think it has something to do with the recognizer not deleting the old grammars.