简体   繁体   中英

memory problem with array releasing in objective-c

i am getting problem with this can any one help me here is my code. This code work 1st click but when click 2 time it get error

malloc: error for object 0x4e226a4: incorrect checksum for freed object - object was probably modified after being freed. * * set a breakpoint in malloc_error_break to de**bug

- (void)updateTextViewContents {
    content = [[NSMutableString alloc] init];   
    for (int i = 0; i <[ _scoresArray count]; i++)
    {
    NSMutableString *data = [_scoresArray objectAtIndex:i];
        [content appendString:data];
        if([content isEqualToString:UserText.text]&&[content isEqualToString:PassText.text])
        {
        UIAlertView *alt = [[UIAlertView alloc] initWithTitle:nil message:@"Valid User" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alt show];
            [alt release];
            [content release];

        }
        else    
        {
        UIAlertView *alt1 = [[UIAlertView alloc] initWithTitle:nil message:@"NOT A Valid User" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alt1 show];
        [alt1 release];
        }
    }

when i release over here it work but when i click 2nd time by entering right user name and password both alter view display at time .I think it will happen because of array get add at every cilck that's why both alter view dispaly at time how i sovle this . //[content release];

}

Can't you replace

NSMutableString *data = [_scoresArray objectAtIndex:i];
        [content appendString:data];

with this

    [content appendString:[_scoresArray objectAtIndex:i]];

您的两个警报都显示为有效用户,因为您的循环放置break ,因为您在有效用户之后一定不能通过循环,希望这会带来好运

You have release content in the if condition. For the next iteration you are again trying to append data to content, which is already released if the condition was true in previous iteration. And thus you are getting that error object was probably modified after being freed .

Here generate memory problem because of,

you release content NSMutableString in if condition and then append it again.

So, Don't release in to for loop, release it out of for loop.

- (void)updateTextViewContents {

content = [[NSMutableString alloc] init];

for (int i = 0; i <[ _scoresArray count]; i++) 
{

    NSMutableString *data = [_scoresArray objectAtIndex:i];
    [content appendString:data];
    if([content isEqualToString:UserText.text]&&[content isEqualToString:PassText.text])
    {
        UIAlertView *alt = [[UIAlertView alloc] initWithTitle:nil message:@"Valid User" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alt show];
        [alt release];
        //[content release]; - Not release here.

    }
    else    
    {
        UIAlertView *alt1 = [[UIAlertView alloc] initWithTitle:nil message:@"NOT A Valid User" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alt1 show];
        [alt1 release];
    }
}

[content release];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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