Glib memory allocation error

本秂侑毒 提交于 2019-12-11 04:34:46

问题


I am using a library libfprint on ubuntu nd I am trying to call a function through my java code.

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;
}

I am passing integer value 5 from my java code to this function. When I try to execute above function I got following errors:

GLib-ERROR **: /build/buildd/glib2.0-2.30.0/./glib/gmem.c:170: failed to allocate 3077591024 bytes

I have tried same code on 2 different ubuntu machine but the error remains the same. I dont know why it is trying to allocate so many bytes for 24+5 length.

Could anyone suggest me any solution?


回答1:


The source code clearly states:

/* structs that applications are not allowed to peek into */
(...)
struct fp_img;

So, I'm not sure what you did in order to even compile something that needs the size of struct fp_img: you're not supposed to be able to do that, since the structure declaration is opaque.




回答2:


It look like you get a pointer instead of a size_t. Try to change your definition to:

API_EXPORTED struct fp_img *fpi_img_new(size_t * length);

You then need to derefenrece it:

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;
}

Note: It seems that 3077591024 is on the stack (0x125807FE) it is highly platform dependent so don't quote me on that.




回答3:


What happens if you change

sizeof(*img)

with

sizeof(struct fp_img)

? I'm thinking that this could be your problem because *img is not initialised to anything at the time you are calling g_malloc().




回答4:


ERROR: struct fp_img *img = g_malloc(sizeof(*img) + length);

*img is new created, so you can not use it like "sizeof(*img)", you could write like this: "sizeof(struct fp_img)"



来源:https://stackoverflow.com/questions/18187529/glib-memory-allocation-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!