简体   繁体   English

iphone ansi c:未分配指针

[英]iphone ansi c : pointer being freed was not allocated

I am using ac lib in my iPhone application. 我在我的iPhone应用程序中使用ac lib。 The c lib is written by someone I do not have access to. C库是由我无权访问的人编写的。

However I am getting the error that object 0xa6000d is being freed but not allocated. 但是我得到的错误是对象0xa6000d被释放但未分配。 The debug variable screenshot is here. 调试变量屏幕截图在这里。

在此处输入图片说明

struct GtsMsg is defined as, struct GtsMsg定义为

struct msg {
    int offset;
    int dataLength;
    u8* data;
    cBool expandable;
    cBool owned;
    u8* mid2key;
};

#define GtsMsg struct msg


void freeMessageData(GtsMsg* msg) {
    if (msg == NULL) return;
    if (msg->owned) {
        if (msg->data != NULL) {
            free(msg->data);
        }
    }
    free(msg->mid2key);
    memset(msg, 0, sizeof(GtsMsg));
}

The breakpoint for malloc_error_break is the line free(msg->data). malloc_error_break的断点是free(msg-> data)行。

I have added the checking if (msg->data != NULL), but it did not work. 我已经添加了检查(msg-> data!= NULL),但是没有用。 What are the ways to check if the memory for msg->data or for msg is allocated or not? 检查msg-> data或msg的内存是否已分配的方法有哪些?

Do you think there is something fishy, at *data = (u8)'\\0'? 您是否认为* data =(u8)'\\ 0'有点问题?

thanks in advance! 提前致谢!

Thanks to all to your suggestions. 感谢所有您的建议。 Thanks hmjd for your tips. 感谢hmjd的提示。

I found the struct is used in a cpp class which destructor calls the freeMessageData. 我发现该结构在析构函数调用freeMessageData的cpp类中使用。 But I could not find that the struct is initialized. 但是我找不到该结构已初始化。 So, if I initialize the struct to NULL in the declaration, this avoid the free memory problem and do not crash. 因此,如果我在声明中将结构初始化为NULL,则可以避免可用内存问题,并且不会崩溃。

Its something suggested by simonc. 它由simonc建议。 If he writes that as an answer not, comment, I could accept that. 如果他没有这样回答,请发表评论,我可以接受。

From comments of hmjd , hmjd评论中,

The check for NULL is useless as free() will be a no-op if the pointer passed to it is NULL. 对NULL的检查是无用的,因为如果传递给它的指针为NULL,则free()将是空操作。 Just because a pointer is not NULL does not mean it points a valid memory location (the memory may have already been freed or the pointer was never initialized to NULL). 仅仅因为指针不是NULL并不意味着它指向有效的内存位置(内存可能已经被释放,或者指针从未初始化为NULL)。 It is the responsibility of the programmer to ensure that the pointer is valid before attempting to free it. 程序员有责任在尝试释放指针之前确保指针有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM