Eclipse : GLib-ERROR - java

I have an JAVA application for the fingerprint scanning and fingerprint matching.For matching the finger prints I am using the libfprint library.I have done all the settings related with the library.
The basic functions like initialization of library is work fine in the application.Now am trying to allocate the memory for the image using the function fpi_img_new() . Here is the syntax for that function
struct fp_img *fpi_img_new(size_t length);
but when I try to allocate some memory by passing the value to this function it shows the following error
GLib-ERROR **: /build/buildd/glib2.0-2.30.0/./glib/gmem.c:170: failed to allocate 3078340616 bytes
/usr/lib/jvm/java-7-openjdk/bin/java: line 31: 3078 Trace/breakpoint trap $jre $args
I am passing the value manually to just check the functionality of this function.But it gets fail.
Could any one please help me in solving the above problem?
Thank you in advance.
EDIT :
Here is my code where i am using the fpi_img_new() function :
API_EXPORTED struct fp_img *fpi_img_new(size_t length)
{
struct fp_img *img = g_malloc(sizeof(*img) + length);
memset(img, 0, sizeof(*img));
fp_dbg("length=%zd", length);
img->length = length;
return img;
}
And i am calling this function by simply passing the integer value like
fpi_img_new(2)

Related

Jpcap crashes when trying to open device

I use Jpcap in order to create ARP requests but when calling the method JpcapCaptor.openDevice(interface,snaplen,promisc,to_ms);, I get the following error :
java.lang.NoSuchMethodError: setPacketValue
at jpcap.JpcapCaptor.nativeOpenLive(Native Method)
at jpcap.JpcapCaptor.openDevice(JpcapCaptor.java:61)
The jpcap dll file I'm using was compiled from the source in order to work on Windows 64 bits. Is there anyway I can solve that weird problem ?
After having looked in the jpcap.dll file source code, I see that the following code is used in the openDevice method (the one that crashes) of the JpcapCaptor.java file:
JpcapCaptor jpcap = new JpcapCaptor();
String ret = jpcap.nativeOpenLive(intrface.name, snaplen, (promisc ? 1 : 0), to_ms);
According to the compiler, this is the second line of this piece of code that crashes. So I looked in the nativeOpenLive method which comes from the file JpcapCaptor.c which starts with:
JNIEXPORT jstring JNICALL
Java_jpcap_JpcapCaptor_nativeOpenLive(JNIEnv *env,jobject obj,jstring device,jint snaplen,jint promisc,jint to_ms){
char *dev;
jint id;
set_Java_env(env);
However, in this last function (set_Java_env), I found the call to the setPacketValue method in the following form: setPacketValueMID=(*env)->GetMethodID(env,Packet,"setPacketValue","(JJII)V");
Having only very weak bases in C, I would like to know the meaning of these different methods and, if possible, where the error comes from.
The source is available at the following address: https://github.com/jovigb/jpcap-x64/blob/master/src

Need help in understanding Android JNI logcat related to IRT

Using Android JNI , I have created file and writing data with file descriptor.
example :-
dest = memalign( BLKSIZE, sz);
if (dest == NULL) {
LOGE("Unable to allocate memory");
return -1;
}
memcpy(dest, bufferIn, sz);
int rc = write(fd, dest, sz);
LOGI("write %d bytes.", rc);
free(dest);
if(rc == -1) {
LOGI("Error in writing : %d %s\n", errno, strerror(errno));
}
Write is successful and I can see data in file. But for each write I am getting log in adb logcat.
01-06 12:46:36.831 16292-16806/com.test.example I/io: write 1024 bytes.
01-06 12:46:36.831 16292-16806/com.test.example W/art: Attempt to remove
local handle scope entry from IRT, ignoring.
what this log mean:- I unable to understand, or what this log mean.
/W/art: Attempt to remove local handle scope entry from IRT, ignoring/
Is it something related to app need to handle.
After browsing android source code which is at
http://androidxref.com/5.1.1_r6/more/art/runtime/indirect_reference_table.cc?q=Attempt+to+remove+local+handle+scope+entry+from+IRT
I was trying delete local reference
(*env)->DeleteLocalRef(env, jbIn);
we don't need to delete local references yourself, once the JNI function returns to Java the references will get GC.
Local references will be discarded by JNI on return from a native method, but this process has nothing to do with Java garbage collection. Usually, we don't need to worry about local references.

How to call String[][] in R using rjava

I am preparing an R wrapper for a java code that I didn't write myself (and in fact I don't know java). I am trying to use rJava for the first time and I am struggling to get the .jcall right.
Here is an extract of the java code for which I write a wrapper:
public class Model4R{
[...cut...]
public String[][] runModel(String dir, String initFileName, String[] variableNames, int numSims) throws Exception {
[...cut...]
dir and initFileName are character strings for the directory and file name with initial conditions, variable names is a list of character strings that I would write like this in R: c("var1", "var2", "var3", ...) and can be of length from one to five. Finally, numSim is an integer.
Here is my tentative R code for a wrapper function:
runmodel <- function(dir, inFile, varNames, numSim){
hjw <- .jnew("Model4R")
out <- .jcall(hjw, "[[Ljava/lang/String", "runModel", as.character(dir), as.character(inFile), as.vector(varNames), as.integer(numSim))
return(out)
}
The error in R is:
Error in .jcall(hjw, "[[Ljava/lang/String", "runModel", as.character(dir),
: method runModel with signature (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;II)[[Ljava/lang/String not found
I suspect that the JNI type isn't correct for String[][]. Anyhow, any help that could direct me towards a solution would be welcome!
You're missing a semicolon at the end of the JNI for String[][] - it should be "[[Ljava/lang/String;". Also, I think you need to call .jarray instead of as.vector on varNames. The R error is telling you that rJava thinks the class of the third argument is Ljava/lang/String; instead of [Ljava/lang/String;.

Java Strings via SWIG in Android NDK have strange characters in place of null bytes

I am using SWIG to generate an interface to some C code which I have compiled as an Android NDK library. My C code NDK library uses a structure, MY_STRUCT that contains char* elements that pass data in both the in and out directions and the SWIG generated JNI works fine as far as that goes, ie I can read the strings in the C code, set data and read the result in the Java code as required.
However I have a problem in that if I pass in a java String that contains null bytes the nulls are replaced by "\300\200"
Eg, if I create a string in java as follows :
MY_STRUCT myStruct = new MY_STRUCT();
byte[] myBytes = new byte[21];
String myString = new String(myBytes);
myStruct.setName(myString);
then myStruct has it's name field set to 21 null bytes as required and this is visible in the java debugger, but the string passed across to my NDK library code as seen in the NDK C debugger is as follows :
"\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200\300\200"
My SWIG .i file has the following relevant portion :
%include various.i
%include cpointer.i
%pointer_functions(int, intp);
%include ../../../mydir/myheader.h
myheader.h has the following relevant portion :
typedef struct
{
...
char* name;
...
} *P_MY_STRUCT, MY_STRUCT;
The C code debugs fine and I can read and write all the strings pointed to by the name etc elements of MY_STRUCT, the only issue is the transformation of null elements of the strings input to the SWIG generated JNI code into the strange "\300\200" elements in the C code in the NDK library.
EDIT:
Considering an alternative : I have several functions that take byte arrays instead of strings for C-function char* arguments and this is achieved in the myModule.i file as follows :
bool myFunc(P_MY_STRUCT, char* BYTE, int32_t);
Is there any way in SWIG of achieving the equivalent of the BYTE functionality for structure members ? I tried using the same BYTE trick in myModule.i as follows but it didn't work :
typedef struct
{
...
char* BYTE;
...
} *P_MY_STRUCT, MY_STRUCT;
Thanks again,
This is known crazy behavior by Java and described in the answers to this question:
What does it mean to say "Java Modified UTF-8 Encoding"?

How to fix JNI crash on env->NewObject?

I'm trying to use Clang via JNI (Clang C-API).
One moment after few iteractions it just stop creating new objects and crashes:
map method
0 args:
create Method instance 0x7fa26ba23c90 0x7fa26ba2a0c0 libclang: crash
detected during indexing source file: { 'source_filename' :
'/Users/asmirnov/Documents/dev/src/clang_jni/mac/test/TestFile.h'
'command_line_args' : ['-c', '-x', 'c++'], 'unsaved_files' : [],
'options' : 0, }
The code is pretty straight-forward:
mapMethod(JNIEnv *env, const CXIdxDeclInfo *info) {
debug("map method");
int numArgs = clang_Cursor_getNumArguments(info->cursor);
debug(" %i args:", numArgs);
debug("create Method instance %p %p", MethodClass, MethodConstructor);
jobject result = env->NewObject(MethodClass, MethodConstructor);
debug("create Method params instance");
Method class and constructor is found and registered as globals correctly (seems to be) and it works few iterations:
// method
MethodClass = (jclass)env->NewGlobalRef(env->FindClass("name/antonsmirnov/clang/dto/index/Method"));
debug(MethodClass != NULL ? "found MethodClass" : "not found MethodClass");
MethodConstructor = env->GetMethodID(MethodClass, "<init>", "()V");
debug(MethodConstructor != NULL ? "found MethodConstructor" : "not found MethodConstructor");
I've read some "jni tips and tricks" articles and tried to env->DeleteLocalRef and make local references count too big just to try, but no result:
// magic
jint ensureResult = env->EnsureLocalCapacity(1024);
debug("ensure result %i", ensureResult);
jint pushResult = env->PushLocalFrame(1024);
debug("push result %i", pushResult);
Clang is hijacking exception so i can't see the real reason.
The problem happens after few iterations as i said so it seems to be some limit exceeded problem or smth.
What is wrong?
UPDATE: I've done some research and i found that if i delete some local vars before then i can get one iteration more and one object instance more. So it makes me feel that it's using 16 local vars indeed and ignore my EnsureLocalCapacity invocation. Where should it be done?
Fixed using EnsureLocalCapacity in JNI_OnLoad() (did not work in each native method call).
objects created via NewObject, FindClass,needed to be freed via DeleteLocalRef(), since number of local variables are limited in jni. or you can use EnsureLocalCapacity in JNI_OnLoad().

Categories

Resources