1

我正在使用 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. 我认为这与识别器不删除旧语法有关。

4

0 回答 0