Class members of a native object accessed using JNI change value unexpectedly
I have a Java project in which I use some C++ code using JNI. I encountered a weird problem.
I have a class th开发者_开发技巧at looks more or less like so:
class MyClass
{
private:
MyType* _p;
public:
MyClass();
virtual ~MyClass();
void myFunc();
};
And:
MyClass::MyClass() : _p(NULL) {
// _p's value here is indeed NULL (0)
}
MyClass::~MyClass() {
}
void MyClass::myFunc() {
if (_p != NULL) {
delete _p;
}
_p = new MyType();
}
No other function but myFunc
touches _p
, and for some reason, even after initializing it to NULL
, when calling myFunc
for the first time, _p
has some garbage value in it and the function attempts to delete it.
The ctor of MyClass
is called using JNI, and myFunc
is too called using JNI, on a separate occasion.
Any help would be greatly appreciated.
精彩评论