# Problem using graphviz with JNI

**URL:** <https://forum.graphviz.org/t/problem-using-graphviz-with-jni/1953>\
**Category:** Dev\
**Created:** [December 28, 2023, 2:24am UTC](https://forum.graphviz.org/t/problem-using-graphviz-with-jni/1953 "2023-12-28T02:24:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![PavelTurk](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@PavelTurk](https://forum.graphviz.org/u/PavelTurk)\
**Post date:** [December 28, 2023, 2:24am UTC](https://forum.graphviz.org/t/problem-using-graphviz-with-jni/1953/1 "2023-12-28T02:24:57Z")

</div>

Hello all,

I am a Java developer and I want to use graphviz native libraries with Java. For this I use JNI. This is my C code for JNI method:

```auto
  (JNIEnv *env, jobject object, jobjectArray arguments) {
    int argc = env->GetArrayLength(arguments);
    char *argv[argc];
    jstring strings[argc];

    for (int i = 0; i < argc; i++) {
        jstring strArg = (jstring) env->GetObjectArrayElement(arguments, i);
        const char *charArg = env->GetStringUTFChars(strArg, 0);
        strings[i] = strArg;
        argv[i] = (char*) charArg;
    }

    GVC_t *gvc = gvContext();
    gvParseArgs(gvc, argc, argv);

    graph_t *g, *prev = NULL;
    while ((g = gvNextInputGraph(gvc))) {
        if (prev) {
            gvFreeLayout(gvc, prev);
            agclose(prev);
        }
        gvLayoutJobs(gvc, g);
        gvRenderJobs(gvc, g);
        prev = g;
    }
    gvFinalize(gvc);
    int result = gvFreeContext(gvc);

    for (int i = 0; i < argc; i++) {
        env->ReleaseStringUTFChars(strings[i], argv[i]);
    }
    return result;
}

```

And I have three tests for this method. I can use these test in any order but the following statement is always true - image is generated for first test, image is NOT generated for the second test and JVM crashes for the third test with the following stack:

```auto
C [libc.so.6+0x18b6a7]
C [libc.so.6+0x7d022] (vfprintf-internal.c:2377)
C [libcgraph.so.6.0.0+0x68ed] agerr_va+0xe6
C [libcgraph.so.6.0.0+0x6a15] agerr+0xb9
C [graphvizj+0x5f767] gvNextInputGraph+0xbb

```

From the fact that test order can be different but result is always the same I can conclude that state is somewhere saved between tests.

Could anyone say what I am doing wrong and how to fix it?

---

<div class="post-metadata">

**Author:** ![PavelTurk](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@PavelTurk](https://forum.graphviz.org/u/PavelTurk)\
**Post date:** [December 28, 2023, 6:51pm UTC](https://forum.graphviz.org/t/problem-using-graphviz-with-jni/1953/2 "2023-12-28T18:51:38Z")

</div>

After some tests I created this [issue](https://gitlab.com/graphviz/graphviz/-/issues/2484) .

---

<div class="post-metadata">

**Author:** ![schmoo2k](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/schmoo2k/32/29_2.png) [@schmoo2k](https://forum.graphviz.org/u/schmoo2k)\
**Post date:** [December 29, 2023, 11:59am UTC](https://forum.graphviz.org/t/problem-using-graphviz-with-jni/1953/3 "2023-12-29T11:59:18Z")

</div>

FWIW this the code I use when linking natively (not for JNI, but for WebAssembly):

Note: `src` is the DOT string and only contains a single graph.

```c
    GVC_t *gvc = gvContextPlugins(lt_preloaded_symbols, true);

    agseterr(AGERR);
    agseterrf(vizErrorf);
    agreadline(1);

    Agraph_t *graph = agmemread(src);
    if (graph)
    {
        char *data = NULL;
        unsigned int length;

        gvLayout(gvc, graph, engine);
        gvRenderData(gvc, graph, format, &data, &length);
        m_result = data;
        gvFreeRenderData(data);
        gvFreeLayout(gvc, graph);
        agclose(graph);
    }

    gvFinalize(gvc);
    gvFreeContext(gvc);

```

---

<div class="post-metadata">

**Author:** ![scnorth](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/scnorth/32/89_2.png) [@scnorth](https://forum.graphviz.org/u/scnorth)\
**Post date:** [December 29, 2023, 1:16pm UTC](https://forum.graphviz.org/t/problem-using-graphviz-with-jni/1953/4 "2023-12-29T13:16:56Z")

</div>

Over in Graphviz issues, it’s pointed out that apparently `gvParseArgs()` has a bug and isn’t reentrant (if people still use that word?)

---

<div class="post-metadata">

**Author:** ![PavelTurk](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@PavelTurk](https://forum.graphviz.org/u/PavelTurk)\
**Post date:** [December 29, 2023, 11:46pm UTC](https://forum.graphviz.org/t/problem-using-graphviz-with-jni/1953/5 "2023-12-29T23:46:14Z")

</div>

@schmoo2k Could you show full chain from your non C code to Graphviz? I mean you code → … → … → Graphviz wasm.

---

<div class="post-metadata">

**Author:** ![schmoo2k](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/schmoo2k/32/29_2.png) [@schmoo2k](https://forum.graphviz.org/u/schmoo2k)\
**Post date:** [December 30, 2023, 9:26pm UTC](https://forum.graphviz.org/t/problem-using-graphviz-with-jni/1953/6 "2023-12-30T21:26:34Z")

</div>

The code is here: [GitHub - GordonSmith/hpcc-js-wasm: HPCC-Systems Web-Assembly (JavaScript)](https://github.com/GordonSmith/hpcc-js-wasm) but its somewhat complicated (and somewhat outdated). Currently it goes like this:

1. You write an IDL file: [hpcc-js-wasm/src-cpp/graphviz/main.idl at WASI\_WIT · GordonSmith/hpcc-js-wasm · GitHub](https://github.com/GordonSmith/hpcc-js-wasm/blob/WASI_WIT/src-cpp/graphviz/main.idl)
2. The IDL file generates additional c++ wrappers and corresponding JavaScript loaders
3. The C++ and generated wrappers get compiled to wasm
4. The generated JavaScript code loads the wasm and wires up all the wasm methods so they can be called from JavaScript.

Note: The above has been obsoleted by WASI and WIT Components - which does a similar thing, but in a more “standard” way and isn’t limited to C++ “Guests” and JS “Hosts”

---

<div class="post-metadata">

**Author:** ![scnorth](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/scnorth/32/89_2.png) [@scnorth](https://forum.graphviz.org/u/scnorth)\
**Post date:** [December 31, 2023, 1:22am UTC](https://forum.graphviz.org/t/problem-using-graphviz-with-jni/1953/7 "2023-12-31T01:22:36Z")

</div>

I’m not exactly sure what we are trying to figure out now. Over in gitlab, there’s a [merge request](https://gitlab.com/graphviz/graphviz/-/merge_requests/3502) to fix the bug that started this. I believe @PavelTurk then asked if Graphviz was threadsafe, and we said Definitely Not. @PavelTurk said Then what about WASM Graphviz. The example is a single shot, anyway, maybe can’t answer that question.
