NSString componentsSeparatedByString returned memory

  • Hi - I'm drilling down on memory leaks in my code and I'm finding some
    strange memory usage that I don't quite understand. I'm calling
    'componentsSeparatedByString' on an NSString and I seem to be finding
    a memory leak with the NSArray that is returned.

    If I release the NSAutoreleasePool after the call the memory for the
    NSArray is not free'd up. If I manually release the NSArray I get a
    double free error later on in the code when the NSAutoreleasePool is
    released.

    This doesn't make sense - you either release objects yourself or you
    let the NSAutoreleasePool do it right ? With this case I seem to
    either get a leak or an error - neither method seems to properly clean
    the memory up.

    Am I missing something subtle here ? Thanks.
  • Don't release the NSArray yourself.

    How are you determining that the NSArray object is being leaked? It's
    more likely that it's retained by another source that you don't know
    about—and you don't need to know about. Just trust the Cocoa memory
    management rules to do their job.

    Martin Linklater wrote:
    > Hi - I'm drilling down on memory leaks in my code and I'm finding some
    > strange memory usage that I don't quite understand. I'm calling
    > 'componentsSeparatedByString' on an NSString and I seem to be finding
    > a memory leak with the NSArray that is returned.
    >
    > If I release the NSAutoreleasePool after the call the memory for the
    > NSArray is not free'd up. If I manually release the NSArray I get a
    > double free error later on in the code when the NSAutoreleasePool is
    > released.
    >
    > This doesn't make sense - you either release objects yourself or you
    > let the NSAutoreleasePool do it right ? With this case I seem to
    > either get a leak or an error - neither method seems to properly clean
    > the memory up.
    >
    > Am I missing something subtle here ? Thanks.
  • Apologies - I seem to have found my problem.

    On 14 Dec 2007, at 21:29, Martin Linklater wrote:

    > Hi - I'm drilling down on memory leaks in my code and I'm finding
    > some strange memory usage that I don't quite understand. I'm calling
    > 'componentsSeparatedByString' on an NSString and I seem to be
    > finding a memory leak with the NSArray that is returned.
    >
    > If I release the NSAutoreleasePool after the call the memory for the
    > NSArray is not free'd up. If I manually release the NSArray I get a
    > double free error later on in the code when the NSAutoreleasePool is
    > released.
    >
    > This doesn't make sense - you either release objects yourself or you
    > let the NSAutoreleasePool do it right ? With this case I seem to
    > either get a leak or an error - neither method seems to properly
    > clean the memory up.
    >
    > Am I missing something subtle here ? Thanks.
  • On Dec 14, 2007 4:29 PM, Martin Linklater <mslinklater...> wrote:

    > Hi - I'm drilling down on memory leaks in my code and I'm finding some
    > strange memory usage that I don't quite understand. I'm calling
    > 'componentsSeparatedByString' on an NSString and I seem to be finding
    > a memory leak with the NSArray that is returned.
    >
    > If I release the NSAutoreleasePool after the call the memory for the
    > NSArray is not free'd up. If I manually release the NSArray I get a
    > double free error later on in the code when the NSAutoreleasePool is
    > released.
    >
    > This doesn't make sense - you either release objects yourself or you
    > let the NSAutoreleasePool do it right ? With this case I seem to
    > either get a leak or an error - neither method seems to properly clean
    > the memory up.
    >
    > Am I missing something subtle here ? Thanks.

    Sort of. The -componentsSeparatedString: method is one for which you are not
    responsible for releasing the return value - you obviously understand that
    part. However, that's where the contract ends; whether it's autoreleased, or
    released later by some other mechanism, is an implementation detail that you
    shouldn't worry about. The only thing you can depend on for certain is that
    you don't need to release the returned array yourself.

    If releasing the array yourself results in a double free error, that
    indicates that *something* is eventually releasing it, just as it should.
    That means there's no leak; if there were - i.e. if it weren't being
    released properly - you wouldn't get that error.

    sherm--
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today