2 messages in net.java.dev.jna.usersRe: [jna-users] Pointer to double arr...
FromSent OnAttachments
Louis HemonSep 28, 2007 12:29 pm.cpp, .h, .java, 1 more
Timothy WallSep 28, 2007 1:21 pm 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] Pointer to double array in callback function (crash JVM)Actions...
From:Timothy Wall (twal@dev.java.net)
Date:Sep 28, 2007 1:21:00 pm
List:net.java.dev.jna.users

Most crashes are due to a write or a read outside of valid memory.

Why you're writing doubles into an array of 'int', I don't know.

Are you certain you're only writing within the space of the allocated array of 'int'?

You're writing N doubles of size X into a space allocated for N ints of size Y. What are X and Y?

On Sep 28, 2007, at 3:29 PM, Louis Hemon wrote:

Hello,

I have a problem with this code (attachement).

C callback : typedef void (*MYFUNCTION)(int n, int *x);

in java : public interface MYFUNCTION extends Callback { void callback(int n, Pointer x); }

This function is implemented as follow in other class :

class Myfunction implements Testlib.MYFUNCTION { public Myfunction(Testlib testlib) { this.testlib = testlib; }

@Override public void callback(int n, Pointer x) { System.out.println("Myfunction"); int doublesize = testlib.getDoubleSize(); System.out.println("doublesize : " + doublesize);

for (int i = 0; i < n; i++) x.setDouble(doublesize * i, i);

System.out.println("End Myfunction"); }

Testlib testlib; }

When I execute this code the JVM crash. I don t understand why?

Thanks,

Louis Hemon

#include "Test.h" #include <iostream>

int getDoubleSize() { return sizeof(double); }

void test(MYFUNCTION f) { std::cout << "begin test"<< std::endl;

int n = 8; int* iRow = new int[n];

(*f)(n,iRow);

std::cout << "fin test"<< std::endl; delete [] iRow; std::cout << "fin test"<< std::endl; }

#ifndef __lib_H__ #define __lib_H__

#ifdef __cplusplus extern "C" { #endif

typedef void (*MYFUNCTION)(int n, int *iRow);

int getDoubleSize();

void test(MYFUNCTION f);

#ifdef __cplusplus } /* extern "C" { */ #endif

#endif import com.sun.jna.Pointer;

class Myfunction implements Testlib.MYFUNCTION { public Myfunction(Testlib testlib) { this.testlib = testlib; }

@Override public void callback(int n, Pointer x) { System.out.println("Myfunction"); int doublesize = testlib.getDoubleSize(); System.out.println("doublesize : " + doublesize);

for (int i = 0; i < n; i++) x.setDouble(doublesize * i, i);

System.out.println("End Myfunction"); }

Testlib testlib; }

public class Test {

public Test() { }

public static void main(String[] args) throws Exception { Testlib testlib = Testlib.INSTANCE;

Myfunction g = new Myfunction(testlib); testlib.test(g); testlib.test(g); testlib.test(g); testlib.test(g); testlib.test(g); } } import com.sun.jna.Callback; import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.Pointer;

public interface Testlib extends Library { public static Testlib INSTANCE = (Testlib) Native.loadLibrary ("Test", Testlib.class);

public interface MYFUNCTION extends Callback { void callback(int n, Pointer x); }

int getDoubleSize();

void test(MYFUNCTION f); }