JNA Invalid memory access while writing to stdout

I am writing a jna wrapper for the c library compiled using gcc under cygwin. Depending on how I run the jna shell, the java application either just freezes (if it runs as a unit test in eclipse) or terminates with an Invalid memory exception. The problem seems to occur only in the c library, which writes something to either stdout or stderr. Here is my minimal (un) working example:

add.c

#include <stdio.h>

int add (int x, int y)
{
    fprintf(stdout, "hello world\n" );
    return x + y;
}

jna wrapper

public interface Add extends Library
{
    Add INSTANCE = (Add) Native.loadLibrary("add", Add.class);
    int add(int x, int y);
}

Compiling a c file under cygwin as follows:

gcc -g -Wall -c add.c
gcc -shared -o add.dll add.o

If I delete the fprintf line, everything works fine. Both add.dll and cygwin1.dll files are in the target java folder.

+1
source share
1 answer

fprintf.

int fprintf(FILE *restrict stream, const char *restrict format, ...);

(%s).

0

All Articles