我是C ++的新手,为对象分配值时出现以下错误:
JNI DETECTED ERROR IN APPLICATION: non-zero capacity for nullptr pointer: 8968320
这是我要为其分配值的类:
class DirtyRegion
{
public:
DirtyRegion():dirtyRects(0), numRects(0), maxRects(0) {}
~DirtyRegion() {}
public:
ARect *dirtyRects; // Array of Rects
int numRects; // Number of Dirty Rects in the Array
int maxRects; // Size of Array
};
我认为构造函数中的第一行正在初始化对象,但我不确定。如您所见,它具有类型为“ ARect”的变量,这与android.graphics.Rect的NDK等效:
typedef struct ARect {
#ifdef __cplusplus
typedef int32_t value_type;
#endif
/// Minimum X coordinate of the rectangle.
int32_t left;
/// Minimum Y coordinate of the rectangle.
int32_t top;
/// Maximum X coordinate of the rectangle.
int32_t right;
/// Maximum Y coordinate of the rectangle.
int32_t bottom;
} ARect;
在main方法中,我使用此行创建一个实例:
android::DirtyRegion dirtyRegion;
这很好,但是,如果我将值赋给对象变量,则会出错。例如:
dirtyRegion.maxRects = 0;
我在这里缺少基本的东西吗?
谢谢!
答案 0 :(得分:1)
dirtyRects被创建为空指针,因此您需要在访问元素之前分配内存。相反,请考虑使用std :: vector。
#include <vector>
struct ARect {
#ifdef __cplusplus
typedef int32_t value_type;
#endif
ARect(int _l=0, int _t=0, int _r=0, int _b=0):
left(_l), top(_t), right(_r), bottom(_b) {}
/// Minimum X coordinate of the rectangle.
int32_t left;
/// Minimum Y coordinate of the rectangle.
int32_t top;
/// Maximum X coordinate of the rectangle.
int32_t right;
/// Maximum Y coordinate of the rectangle.
int32_t bottom;
};
class DirtyRegion
{
public:
DirtyRegion():dirtyRects(0), numRects(0), maxRects(0) {}
~DirtyRegion() {}
public:
std::vector<ARect> dirtyRects; // Array of Rects
int numRects; // Number of Dirty Rects in the Array
int maxRects; // Size of Array
};
int main()
{
DirtyRegion dirtyRegion;
dirtyRegion.dirtyRects.push_back(ARect(0,0,0,0));
return 0;
}