Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8786051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:30:57+00:00 2026-06-13T21:30:57+00:00

I’m getting malloc: * error for object 0xbfffe160: pointer being freed was not allocated

  • 0

I’m getting “malloc: * error for object 0xbfffe160: pointer being freed was not allocated” when trying to free memory (in objective-c code) of an object that was allocated inside c function. This C function creates and returns binary data packet that is used as NSData later. Here’s my obj-c code part where I’m creating struct variable and passing it by reference to C function:

MyPacket packetRef;
allocAuthentificationCodePacket(&packetRef);

NSData *data = [NSData dataWithBytes:packetRef.bytes length:packetRef.packet->packetSize];
free(&packetRef); // getting error

Everything work fine, except I’m trying to release the memory because the data should be retained by NSData variable. The C functions performs calloc inside itself, so I should somehow to release that memory:

packetRef->bytes = calloc(1, sizeof(*packetRef));

Here’s are my structs for storing binary data:

typedef struct {

  uint8_t packetType;
  uint16_t packetBody;

} MyStruct;

and another struct:

typedef union {

  const uint8_t *bytes; 
  MyStruct *packet;

} MyPacket;

How should I free the memory? The error I’m getting is not crash, it just a message in debug console when running unit tests.

UPDATE. Tried to release “bytes” struct member but getting the same error message:

free(&packetRef.bytes);

UPDATE2. Thanks, the suggested way did worked and malloc error message disappeared from console:

free(packetRef.bytes);

However, getting a warning in Xcode “Passing ‘const uint8_t *’ (aka ‘const unsigned char *’) to parameter of type ‘void *’ discards qualifiers”. I’m using Apple LLVM 4.1 compiler. C function resides in separate file and only a header is included because Android guys will have to reuse it.

UPDATE3. Thanks to @simonc and @nos who have pointed out the struct member “bytes” has const. The warning has disappeared after removing const. The initial idea of using const was to protect “bytes” from modification.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:31:00+00:00Added an answer on June 13, 2026 at 9:31 pm

    This is always wrong. (Hint: It’s almost always wrong to put & inside of free().)

    MyPacket packetRef;
    ...
    free(&packetRef); // getting error
    

    It doesn’t matter what MyPacket is — it has automatic storage duration, i.e., the compiler automatically allocates storage and frees it when the function exits.

    Do not free() something unless it came from malloc() (or calloc(), etc.)

    Since packetRef.bytes was allocated with calloc(), you can free() that instead.

    MyPacket packetRef;
    allocAuthentificationCodePacket(&packetRef);
    ...
    free(packetRef.bytes);
    

    Update

    If the function that you call, allocAuthentificationCodePacket, contains the code:

    packetRef->bytes = calloc(1, sizeof(*packetRef));
    

    And if the bytes field has type const uint8_t *, then something is wrong.

    1. Perhaps your code is wrong, and you are supposed to call some function to free the packet rather than freeing it yourself.

    2. Perhaps the type of the bytes field is wrong, and should be uint8_t * instead of const uint8_t *.

    3. Perhaps allocAuthentificationCodePacket is wrong.

    Who knows? It’s not wrong enough to crash, but it is a problem.

    Footnote

    There are no references in C. &x is “address of x”, not “reference to x”.

    Let’s consider the following code:

    char *x = malloc(10);
    free(x);
    

    When people talk about this code, they will say something like “x is allocated on the heap”, but that’s not technically correct, x is allocated on the stack and contains the address of 10 bytes on the heap. Likewise, the line free(x) does not actually free x, it frees the memory which x points to.

    So when someone tells you, “don’t forget to free x“, you know they actually mean “don’t forget to free the memory which the value contained in x points to”. People are sloppy with terminology but computers aren’t.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.