开发者

Class members of a native object accessed using JNI change value unexpectedly

开发者 https://www.devze.com 2023-01-30 06:59 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消