views:

143

answers:

3

Updates code

- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {
    mytimer3=[NSTimer scheduledTimerWithTimeInterval:.03 target:self selector:@selector(show) userInfo:nil repeats:NO];


        NSLog(@" search is down");  



    //////////// rnd to hold keyboard





     //ovController.view.backgroundColor = [UIColor grayColor];


     self.tableView.scrollEnabled = NO;




     UserText=[self.searchDisplayController.searchBar text];
     myInt= UserText.length;
     //int myInt= [save length];



    // NSString *myStringPrt1 = [[NSString alloc] init];
    // NSString *myStringPrt2 = [[NSString alloc] init];


     if(UserText.length>3)

     {
     //[ovController.view removeFromSuperview];
     //[tableView reloadData];
     url=nil;
    // myStringPrt1=@"http://find.php?keyword=";
    NSString * myStringPrt2=UserText;
    // myStringPrt1=[myStringPrt1 stringByAppendingString:myStringPrt2];
    // myStringPrt1 = [myStringPrt1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    // loadingView.hidden=NO;




         NSString *outputString = [[NSString stringWithString:@"http://find.php?keyword="] stringByAppendingString: UserText];
         outputString = [outputString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


         NSLog(@"My string is  now =  %@", outputString);


     url=   [NSURL URLWithString:outputString];



     NSXMLParser *xmlParser = [[[NSXMLParser alloc] initWithContentsOfURL:url] autorelease];

     //Initialize the delegate.
     XMLParser *parser = [[[XMLParser alloc] initXMLParser]autorelease];

     //Set delegate
     [xmlParser setDelegate:parser];

     //Start parsing the XML file.
     BOOL success = [xmlParser parse];
     if(success)
     {



         [super viewDidLoad];




     [self searchTableView];    
         mytimer4=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(wipe) userInfo:nil repeats:NO];
     }


     }

}

A: 

No depending on your requirement and object allocation, your retain count will definitely increase. But the main thing you need to keep in mind that release all the allocated object at proper place and proper time.

Jim
NSString *myStringPrt1 = [[NSString alloc] init];NSString *myStringPrt2 = [[NSString alloc] init]; //if(UserText.length>3 { //[ovController.view removeFromSuperview]; //[tableView reloadData]; url=nil; myStringPrt1=@"http://wap?keyword="; myStringPrt2=UserText; myStringPrt1=[myStringPrt1 stringByAppendingString:myStringPrt2]; myStringPrt1 = [myStringPrt1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];then after pasring xml when i trace themtheir value is 4NSLog(@"retain cccd %d",[myStringPrt1 retainCount]);/ its 4
ram
when user press search button then i am calling the above lines when should i release myStringPrt1 and myStringPrt2or should i use them without alloc??
ram
one more thing malloc: *** error for object 0x4696730: pointer being freed was not allocated*** set a breakpoint in malloc_error_break to debugWat2Eat(6945,0xa01a7500) malloc: *** error for object 0x4696770: double free###########################################can this leads to memory leakage????
ram
Please, paste this code in your question in a formatted way. This is just unreadable.
Rengers
how to 4mat code?? whats the code ??is that like [code] [/code]???
ram
Just select the code and use the code button in the toolbar.
Rengers
@ram: what's 4mat? /mindlesstrolling
BoltClock
+5  A: 

Don't pay any attention to your retain count. That number will fluctuate for reasons entirely out of your control and trying to determine why the number is what it is will drive you crazy (and not do you any good).

Follow proper Obj-C memory management guidelines, use the leaks detector in Instruments, and run the static analyzer when you build. If you do all that, you'll be fine.

kubi
thanks how to use leak detectetor??? i mean whane you will know your code is efficient???i have seen that graph with some thin pink likes but i dont know what are they plz explain
ram
@ram: kubi even provided a link to the documentation. Do yourself a favour and *read* it.
JeremyP
+5  A: 

Right, you have memory leaks all over the place:

NSString *myStringPrt1 = [[NSString alloc] init];
NSString *myStringPrt2 = [[NSString alloc] init];

url=nil;

myStringPrt1=@"http://wap?keyword="; //MEMORY LEAK, this will assign a new string to myStringPrt1, without releasing the first one
myStringPrt2=UserText; //MEMORY LEAK, this will assign a new string to myStringPrt2, without releasing the first one

myStringPrt1=[myStringPrt1 stringByAppendingString:myStringPrt2]; //MEMORY LEAK, this will assing an autoreleased string the myStringPrt1, without releasing the old one first.
myStringPrt1 = [myStringPrt1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //MEMORY LEAK, this will assing an autoreleased string the myStringPrt1, without releasing the old one first.

I suggest you first read the memory management guidelines as indicated by Kubi. Or, if you are feeling lazy, use this:

NSString *outputString = [[NSString stringWithString:@"http://wap?keyword="] stringByAppendingString: UserText];
outputString = [outputString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Or this:

NSString *outputString = [NSString stringWithFormat:@"http://wap?keyword=%@",UserText];
outputString = [outputString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Rengers
To summarize @Rengers' point, it's almost never the right choice to use manually-retained NSString objects. NSString's convenience constructors, which return autoreleased objects, are almost always simpler to use AND free you from memory concerns.
Dan Ray
Actually, the two leaks are `myStringPrt1=@"http://wap?keyword=";` and `myStringPrt2=UserText;`
tc.
@tc: All of the five assignments after `url=nil` cause leaks.
JeremyP
@Dan Ray: +1 `foo = [[NSString alloc] init]` is almost always wrong since it assigns an empty immutable string to foo the same as `foo = @""`.
JeremyP
@JeremyP: You're right, I forgot to indicate `myStringPrt2=UserText` one. Good rule of thumb on the `[NSString alloc] init]` as well!
Rengers
awsome thanks for all that information i am glad that i am member of this forum ;-) one thing i want to ask 1)if i dont alloc and directly do like this NSString *myStringPrt1 @"http://wap?keyword="; then i dont have release isnt it ..it will be done by autorelase and2) whenever you initalize string in.h then you must have to assing property and then syntezie then only you can use isnt it not like in .h you define string *h; and in .m directly using like this h=@"ddf"; ???
ram
hey i did what you said NSString *outputString = [NSString stringWithFormat:@"http://wap?keyword=%@",UserText];outputString = [outputString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];but still retaincount=4 is that ok??
ram
As Kubi pointed out, you should worry about retain count. If you make sure you balance your retain/release count you should be okay.If you use only those last two lines of code, it should be okay. You could post your 'entire' code in the question if you want to be sure.
Rengers
alright check above code thanks
ram
Looks like there are no big apparent leaks, though there is a lot of unnecessary code in there.
Rengers