iPhone development: pointer being freed was not allocated

前端 未结 7 1816
粉色の甜心
粉色の甜心 2020-12-02 12:33

i got this message from the debugger:

Pixture(1257,0xa0610500) malloc: *** error for object 0x21a8000: pointer being freed was not allocated
*** set a breakpo         


        
相关标签:
7条回答
  • 2020-12-02 13:00

    It looks like you have a corrupted memory bug, and it is probably not in this code. Corrupted memory bugs are my second most-fun bugs, partially because they are often non-deterministic, and the symptoms (aka the crashes) usually happen long after the actual bug hit.

    There are two main types of memory bugs:

    1. Allocating more than you free.
    2. Freeing more than you allocate.

    In this case it looks like you're freeing too much, which is the easier case to see (b/c it can crash earlier) but the harder to track down.

    Here is a strategy you can use to help find an extra deallocations:

    • Turn off some of your deallocations.
    • See if the crash still occurs.

    Ideally, you can find one single deallocation command which, when removed, will allow your code to work correctly. It might be useful to try a binary search approach, if that is practical for your codebase. If it's a large codebase, hopefully you're using version control and you can try to focus on the recent diff's.

    Keep in mind that deallocations can be invoked in several different ways here. Most likely, you're calling release and autorelease on objects. It's also possible you might be explicitly calling dealloc (which is usually a mistake, though). And of course you might even be explicitly calling free directly.

    Once you've gotten rid of the extra deallocations, it's a good idea to also check for memory leaks (aka extra allocations). You can do this by using Instruments and other tools. A good place to start is by reading Finding Memory Leaks in the iPhone development guide.

    Added: It's also a good idea to set a pointer to nil right after you've released it and are done using it. This way, if you call [objectPtr release]; later, it won't do anything.

    (PS Btw, my #1 most-fun bug type is memory corruption in mutlithreaded code. I had one of those once in a multi-million line code base.)

    0 讨论(0)
  • 2020-12-02 13:02

    While probably not the cause of your crash, you are leaking memory by not releasing the context2, unmasked, and mask Core Foundation objects using CFRelease(), CFImageRelease(), or the like.

    0 讨论(0)
  • 2020-12-02 13:06

    I had the same issue with the following code.

    -(void)adjustImageToImageView:(UIImage*)img{
    
    float numPixels = 100;
    float radius = 5;
    UIGraphicsBeginImageContext(CGSizeMake(numPixels, numPixels));
    CGContextRef c = UIGraphicsGetCurrentContext();
    
    CGContextBeginPath(c);
    CGContextMoveToPoint  (c, numPixels, numPixels/2);
    CGContextAddArcToPoint(c, numPixels, numPixels, numPixels/2, numPixels,   radius);
    CGContextAddArcToPoint(c, 0,         numPixels, 0,           numPixels/2, radius);
    CGContextAddArcToPoint(c, 0,         0,         numPixels/2, 0,           radius);
    CGContextAddArcToPoint(c, numPixels, 0,         numPixels,   numPixels/2, radius);
    CGContextClosePath(c);
    
    CGContextClip(c);
    
    [img drawAtPoint:CGPointZero];
    UIImage *converted = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.imageView.image = converted;   }
    

    I took the function from the open source Twitterfon app.

    When I was trying to fix the issue, I tried changing the last line to

    self.imageView.image = [converted retain]
    

    And that stopped the error messages in the console. I'm going to check this in Leaks soon to see whats happening.

    0 讨论(0)
  • 2020-12-02 13:11

    I just wanted to confirm, again, that I had was getting:

    pointer being freed was not allocated

    error and it went away if i change my target os to 3.1 as opposed to 3.0

    0 讨论(0)
  • 2020-12-02 13:14

    I've had the same problem, with a lot of the same symptoms:

    • pointer being freed was not allocated error
    • upgraded to Xcode 3.2
    • error happening in code for images

    If I change the build target to 3.1, the errors in the simulator go away. If I run the code on the device, the errors don't appear. Possibly a bug in 3.0

    My advice is to test with 3.1 as your target, and if you want, you can build for 3.0 for release and not worry about the errors as they don't happen on the device.

    0 讨论(0)
  • 2020-12-02 13:15

    It might be just me but you can't do the following ?

    UIImage *whiteMasked = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return whiteMasked;
    

    whiteMasked is allocated on the stack of the function and after it returns the pointer is no longer valid? Or am I missing something? If you use the returned UIImage* it is not guaranteed to still be there. (it would be a hit and miss). Don't you need to alloc a UIImage* and then autorelease it before returning?

    0 讨论(0)
提交回复
热议问题