我正在使用Sphinx来解析语法文件。
我有一个加载所有语法文件的函数。在程序开始时以及每次用户请求reload
时调用此函数。最初调用它有效。当对此函数进行后续调用时,会出现StackOverflowError
。导致错误的函数中的特定行是gram.listRuleNames()
行,特别是RuleGrammar listRuleNames()
的方法。
有没有人知道为什么会这样?
功能:
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
}
错误:
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)
经过一些调试后,我注意到recognizer.deleteRuleGrammar(rg)
没有删除旧的语法。
我已将其缩小到行loadJSGF()
,当调用两次时会导致StackOverflowError
。我认为它与识别器有关,而不是删除旧的语法。