why there is a memory leak in this method?

  • Hi all:

    I have a simple method:

    - (NSString *)hTagString:(NSString *)source{
    NSString *hTag=nil;

    NSRange startRange=[source rangeOfString:@"<h1"
    options:NSCaseInsensitiveSearch];

    if(startRange.length!=0){
    NSRange endRange=[source rangeOfString:@"</h1>"
    options:NSCaseInsensitiveSearch];
    NSRange tmpRange=[source rangeOfString:@">" options:NSCaseInsensitiveSearch
    range:NSMakeRange(startRange.location+startRange.length,endRange.location-startRange.location-startRange.length)];

    int start=tmpRange.location+tmpRange.length;

    int len=endRange.location-tmpRange.location-tmpRange.length;

    NSString *tmpTag=[source substringWithRange:NSMakeRange(start,len)];

    NSAttributedString *test=[[[NSAttributedString alloc]
    initWithHTML:[tmpTag dataUsingEncoding:[tmpTag fastestEncoding]]
    documentAttributes:nil] autorelease];
    hTag=[test string];

    }

    return hTag;

    }

    when i test it with MallocDebug and it displays there is a memory leak
    in [NSAttributedString alloc] initWithHTML, but i have already
    autorealease it, why it still has a leak? should i use NSAutoreleasePool?

    Does anybody can help me, thank you.

    Leo
  • On Fri, 24 Nov 2006 14:21:46 +0800, Leo <amokok...> said:

    > NSAttributedString *test=[[[NSAttributedString alloc]
    > initWithHTML:[tmpTag dataUsingEncoding:[tmpTag fastestEncoding]]
    > documentAttributes:nil] autorelease];
    > hTag=[test string];
    >
    > }
    >
    > return hTag;
    >
    > }
    >
    > when i test it with MallocDebug and it displays there is a memory leak
    > in [NSAttributedString alloc] initWithHTML, but i have already
    > autorealease it, why it still has a leak? should i use NSAutoreleasePool?

    You never know for sure what autorelease will return, so my first piece of
    advice would be to write it like this:

    NSAttributedString *test=
      [[NSAttributedString alloc] initWithHTML:
      [tmpTag dataUsingEncoding:[tmpTag fastestEncoding]]
      documentAttributes:nil];
    [test autorelease];

    Second, are you planning to modify the NSAttributedString's string directly?
    If not - that is, if you just want to see it - consider making a copy. In
    that case you can just release the original attributed string.

    NSAttributedString *test =
      [[NSAttributedString alloc] initWithHTML:
      [tmpTag dataUsingEncoding:[tmpTag fastestEncoding]]
      documentAttributes:nil];
    hTag = [[test string] copy];
    [test release];
    [hTag autorelease];

    See if any of that helps. m.
    --
    matt neuburg, phd = <matt...>, <http://www.tidbits.com/matt/>
    A fool + a tool + an autorelease pool = cool!
    AppleScript: the Definitive Guide - Second Edition!
    <http://www.amazon.com/gp/product/0596102119>
  • On 25/11/06, Matt Neuburg <matt...> wrote:
    > You never know for sure what autorelease will return, so my first piece of
    > advice would be to write it like this:

    The docs state (for -autorelease):
    Return Value
    self.

    -Phil
  • On Nov 23, 2006, at 10:21 PM, Leo wrote:

    > I have a simple method:
    >
    > - (NSString *)hTagString:(NSString *)source{
    > NSString *hTag=nil;

    Your code looks correct. I don't know why it's leaking, but consider
    using just 'release' at the end of the method. You don't always have
    to use autorelease, and it's actually more characters to type.

    Also, since you're doing HTML parsing, make sure you're not
    replicating code that already exists in WebKit. There's some
    fantastic stuff for dealing with web page source. Check out the
    classes declared in WebKit's DOMCore.h and DOMHTML.h. Particularly
    things like DOMHTMLDocument's getElementsByName.

    [ Side note for everyone else: I can't find documentation for the DOM
    classes anywhere -- only the category additions. It seems strange
    that it wouldn't exist. Is it hiding somewhere? Googling for
    "site:developer.apple.com DOMHTMLDocument" returns zero results. ]

        - Scott
  • On 25/11/06 6:29, Scott Stevenson <scott...> wrote:

    > [ Side note for everyone else: I can't find documentation for the DOM
    > classes anywhere -- only the category additions. It seems strange
    > that it wouldn't exist. Is it hiding somewhere? Googling for
    > "site:developer.apple.com DOMHTMLDocument" returns zero results. ]

    All there is is a link to the IDL files at W3C (especially useful when
    you're offline), and the assumption that you can mentally parse them into
    Objective C :-(

    Gah. I end up opening DOM*.h.

    Cheers,

    Chris
  • On Nov 24, 2006, at 10:51 PM, Chris Ridd wrote:

    >> [ Side note for everyone else: I can't find documentation for the DOM
    >> classes anywhere -- only the category additions. It seems strange
    >> that it wouldn't exist. Is it hiding somewhere? Googling for
    >> "site:developer.apple.com DOMHTMLDocument" returns zero results. ]
    >
    > All there is is a link to the IDL files at W3C (especially useful when
    > you're offline), and the assumption that you can mentally parse
    > them into
    > Objective C :-(
    >
    > Gah. I end up opening DOM*.h.

    I filed a bug. Maybe anybody else who would like some class
    references could do the same.

        - Scott
previous month november 2006 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      
Go to today
MindNode
MindNode offered a free license !