JNI FindClass java.lang.ClassNotFoundException - java-native-interface

I am currently writing some JNI code to call a class within my JAVA program. I have done this successfully already with static methods within a class.
Currently, when I use:
jclass proc_jclass = env->FindClass("example/io/struct/JavaClass");
It crashes the JVM with:
java.lang.ClassNotFoundException.<init>(Ljava/lang/String;)V+3
I have already tried defining class path locations, to no avail. Any help would be greatly appreciated.
Thanks.

I found the answer myself. The issue was solved by moving the FindClass to directly underneath the JNIEXPORT method. As bmargulies stated it may have been caused by the env pointer being modified. By moving it to the top of the method this was avoided. Looking at the code it still not clear what was causing the problem but ill report back when I find out. In addition top tip use a variable i.e:
env->FindClass(str_class);
str_class was used to define the ClassPath so that you can quickly test what classes are accessible without re-compliing. - I know its obvious, but does save time when debugging.

Related

C++ and LuaJIT, Scoped script environment

I've been using LuaJIT for some times now. The tip of the iceberg was enought for my needs until now, but my recent project require me to dig a little deeper.
My actual knowledge of LuaJIT is making function available from C++ to Lua and from Lua to C++. That include passing parameters, tables and retrieving return values.
This is the model I am used to:
I tried to search around for "scoped environement luajit" and multiple variation of the query, but unfortunately I did not find anything relevant. I might not use the right words?
This is the model I want to achieve :
I want to make a "global script environment" that I will share the C++ functions with then make it available to the "scoped script environments".
//push arguments
luaScopedEnvironment1->call("doSomething");
I just want a starting point, help for the terminology and maybe some pointers to related documentation :)
Thanks you for taking time to read me.
I dont think Lua or LuaJIT supports such a thing but if I'm not mistaken, what you are after is called "sandboxing".
It creates a new environment with which you can strip out or add functionality to. Its handy for removing IO and OS functionality.

repeatedly calling a c++ method from cython suddenly is much slower

I often wrap c++ classes using cython. All the calculations are done in c++ and cython is only used to pass variables to the constructor and get the results from c++.
For a recent project, I am having the following problem: The code (which initializes a class and then calls a method) always runs fine initially, but after calling the same method repeatedly (I can't reproduce when exactly), it suddenly runs a factor of at least 1000 slower than normal when calling the same method.
My question is: What could cause such a seemingly random behavior and how would you go about debugging it?
I know this is impossible to solve without seeing the code, but it's large and I don't know where the problematic behavior comes from. I'm just asking for hints and strategies of how to solve it.
Things I've tried:
Checked the c++ code for leaks.
tried different compiler directives (#cython: wraparound=False, boundscheck=False, ...)
A hint could be that if I run the python code without the --pylab option of ipython it complains about a symbol not being found, but that's the only problem I have been able to identify.
It was an uninitialized member that occasionally caused a problem, not a memory leak. Thanks to those who tried to help!
The issue with ipython remains, but I posted that in another question.

__do_global_ctors segfault somewhere in project, cant locate it

I migrated a project from Qt4 to Qt5, and it compiles and everything but the application crashes before it even reaches the main function. I know there is a null value that fucks up something at some point, maybe a file that cant find or something, but there are so many .cpps and .h and libraries that its pretty hard to locate the source of the error plus I cant set any breakpoints. I have a lot of debugging data so maybe any of you can guide me in the right direction. I dont know what I would be doing without stack overflow honestly, so thankyou in advance.
When debugging I get different crashes:
The stack in each case shows different crashes, but all of them have something in common, which is this __do_global_ctors thingy, I have researched and apparently it has to do with constructors, but I have no idea what I should be looking for.
if I missed any info please do ask. I hope someone can enlighten me, I am so so close to get this working.
The __do_global_ctors() is called before your main(), as the framework needs to instantiate all of the global objects that main() might use.
This method will call the constructors for all static objects, and their component objects. I.e. all static constructors.
From the look of the stack trace, it appears that the segfault occurs during the construction of a QGlobalStatic<QMutex, [incomlpete types - see trace for details]> object, which makes sense. This is being constructed by qRegisterResourceData as part of qInitResources_mimetypes.
Try placing a breakpoint in this function in qrc_mimetypes.cpp (if you have the source) and see where that gets you. Or look at the Qt documentation for mimetypes initialisation and make sure you've specified your application's resources correctly.
I managed to solve the issue by thoughtfully re-compiling all the libraries to Qt5 and making sure all the cpps that the program refered were Qt5 too. Also double-checked the linkings. I thought I had done it but apparently I missed one library.
Mind that some libraries need to be migrated and there are others that you can download and compile directly with Qt5. If you are having this same problem make sure that there are no Qt5 versions of that library before migrating them yourself.

port only a single method of class to C?

I have a class with few methods and I'm happy with the performance of all of them except one method. We want to port that to C++. However we don't want to spend too much time porting the whole class to C++, just that single method. Is this possible? How should I do it? Should it be in a blank class? Not in a class?
What I want is to try to use the C version and if failed (other OS, missing pyd), load the Python version.
Thank you.
Depending on the complexity of your code, you could look into using Weave, which is part of SciPy. It allows you to embed C/C++ code in your python module. There's a tutorial here.
Another option you could look at is Boost::Python, which may be a bit more complex to use.

windows application crashing with pure virtual function call

I have a Windows application, it crashes every now and then and not reroducibly.
When it does, I get pure virtual function called. I have set it up to create a Windows dump using ADplus and even when it crashes, there is never a dump.
I am pretty sure this is build error, I am building using VC2008 SP1 and this is the release build.
Any insights on this? How can I debug this, it is a release build with .pdb file and map file.
Thanks
Reza
As was pointed out already, it is very unlikely that this is a build error. I had a quite complex project with a similar problem and was able to track it down by using _set_purecall_handler and providing my own handler. This way I was able to break into the debugger when it happened and see the call stack. Obviously an alternative here is to create the minidump when it happens. Remember that you need to prepare everything for the minidump before the program encounters an exception.
However, there is also a good chance this could be caused by a heap corruption. In such a case I would expect a variety of symptoms, though. You describe this specific symptom, so it's likely that your code is indeed at fault.
The project I mentioned above was a legacy project that modeled something similar to COM and there were indeed places where the compiler could not have possibly found all occasions of pure virtual functions for which no implementation existed in derived classes.
First of all, make sure that you are not calling pure virtual function. If this is not the case, try to start your program under WinDbg.
I am pretty sure this is build error
Very, very unlikely. It's much more likely that this is a bug in your code.
You might be slicing an object somewhere. If you have your app set up to generate a dumpfile on unhanded exceptions and you are still dying with no dump file, then its very possible the bug is in the exception handler.
You need to get a dump file. That should be your first priority.
If your issue is not constant most probably there are some problems with out of index writes which could corrupt memory
If you've tried a full rebuild, then it seems more likely it's a coding issue.
Are you calling any (pure) virtual functions in any constructors or destructors?
More likely is that you're calling a pure virtual function on a deleted object, and the vtable has been moved to point to the parent object. In this case valgrind (free, linux) or Purify ($$$, Windows) will really be able to help you out. VS may have a memory checker with it too.
I once had a similar problem with C++. I had a call to a virtual function from a constructor somewhere, so the object was not constructed yet when the call happened, explaining the "pure" virtual function called. It might also be a problem of slicing if you have bad casts on objects instead of pointer to objects. Use a debugger and step by step/backtrace to find the source so we can help you better.
I have the same error. I reviewed code many times and never found virtual functions usage in constructors or destructors.
The problem was in build system. I had old static library version on my computer where some function wasn't pure virtual, and new one when it became (I added one more abstraction layer). Because EXE file was created with old static library, but new headers, this error appeared.
So, make sure that your libraries and include files versions are consistent if nothing else helps.