Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-19-2009, 09:20 AM   #1 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: Munich, Germany
Posts: 108
Unhappy Strange Malloc Error

Hi,

im recieving a data through a steam which looks something like this:

Quote:
nameStartBLABLABLABLABLAnameEnd
or

Quote:
startTextBLABLABLABLABLAendText
When the data arrives I want to first check if it says "startName" or "startText" in the beginning and then use the data in between accordingly. The first string recieved is always the one with "startName". The app does recognize this and used the string correctly. But wehn the second string arrives I always get an error. Here is my code and the error:

Quote:
if (stream == _inStream) {

NSInteger bytesRead;
uint8_t buffer[32768];
bytesRead = [_inStream read:buffer maxLength:sizeof(buffer)];


NSString *incoming = [[NSString alloc] initWithBytes:buffer length:bytesRead encoding:NSUTF8StringEncoding];
NSString *outputText = [[NSString alloc] init];

if(!bytesRead) {
if ([stream streamStatus] != NSStreamStatusAtEnd)
[self _showAlert:@"Failed reading data from peer."];

}

if (bytesRead > 0 ) {

if ([[incoming substringWithRange:NSMakeRange(0,9)] isEqualToString: @"nameStart"]) {

guestName = [incoming substringWithRange:NSMakeRange(9,([incoming length] -16))];

} else {

if ([[incoming substringWithRange:NSMakeRange(0,9)] isEqualToString: @"startText"]) {

outputText = [[NSString alloc] initWithString: [incoming substringWithRange:NSMakeRange(9,([incoming length] -16))]];

}
}
}
}break;
Quote:
Program received signal: “EXC_BAD_ACCESS”.
App(2953,0x39d802b8) malloc: *** error for object 0x164084: incorrect checksum for freed object - object was probably modified after being freed.
Where is the problem. I cant see where I am freeing twice!
freshking is offline   Reply With Quote
Old 09-19-2009, 10:34 AM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

Code:
if (stream == _inStream) {

   NSInteger bytesRead;
   uint8_t buffer[32768];
   bytesRead = [_inStream read:buffer maxLength:sizeof(buffer)];

   NSString *incoming = [[NSString alloc] initWithBytes:buffer length:bytesRead encoding:NSUTF8StringEncoding];
   NSString *outputText = [[NSString alloc] init];
   // This string is leaked later.  Just do this:
   NSString *outputText = nil;

   if(!bytesRead) {
      if ([stream streamStatus] != NSStreamStatusAtEnd)
         [self _showAlert:@"Failed reading data from peer."];
   } 

   // Do yourself a favor...
   NSString *prefix = [incoming substringWithRange:NSMakeRange(0,9)];
   
   if (bytesRead > 0 ) {

      if ([prefix isEqualToString: @"nameStart"]) {

         guestName = [incoming substringWithRange:NSMakeRange(9,([incoming length] -16))];
         // Is guestName an instance variable?  Use [set setGuestName:...] instead.

      } else {

         if ([prefix isEqualToString: @"startText"]) {

            outputText = [[NSString alloc] initWithString: [incoming substringWithRange:NSMakeRange(9,([incoming length] -16))]];
            // You've just leaked the string created previously.  And, since you
            // don't seem to be doing anything with it past this point, you're
            // leaking the new one, too.

         }
      } 
   }
   // incoming is leaked here since you haven't released it.
}
break;
I'm not seeing an over-release problem, but you are leaking.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 09-19-2009, 11:04 AM   #3 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: Munich, Germany
Posts: 108
Default

Quote:
@interface AppController : NSObject <..>
{
NSString *guestName;
}
@property (nonatomic, retain) NSString *guestName;
This is how I tried doing it but im still getting the same error:

Quote:
NSString *outputText = nil;
Quote:
NSLog(@"Start 1");

if ([prefix isEqualToString: @"startText"]) {

NSLog(@"Start 2");

outputText = [[NSString alloc] initWithString: [incoming substringWithRange:NSMakeRange(9,([incoming length] -16))]];

NSLog(@"Start 3");

if (sounds) {
AudioServicesCreateSystemSoundID (recieveFileURLRef, &recieveFileObject);
AudioServicesPlaySystemSound (self.recieveFileObject);
}

if (vibration) {
AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
}
//when you recieve something over wifi
MyObject* obj = [[MyObject alloc] initWithString: outputText andType: Received ];
[services addObject: obj];
[obj release];

[tableView reloadData];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[tableView numberOfRowsInSection:0] - 1 inSection:0];
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
NSLog(@"Start 4");
Quote:
2009-09-19 17:00:15.139 Texter[3096:207] Start 1
2009-09-19 17:00:15.147 Texter[3096:207] Start 2
Program received signal: “EXC_BAD_ACCESS”.
App(3096,0x39d802b8) malloc: *** error for object 0x15fab4: incorrect checksum for freed object - object was probably modified after being freed.
freshking is offline   Reply With Quote
Old 09-19-2009, 11:15 AM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

Ok, this is going to be tough to do in pieces. Post the entire method using the CODE tags. And will this method be run more than once?
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 09-19-2009, 11:35 AM   #5 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: Munich, Germany
Posts: 108
Default

Yes, it will be run everytime the app recieves data.

I postet the code here. I hope this gives you a better overview!



Quote:
2009-09-19 17:00:15.139 Texter[3096:207] Start 1
2009-09-19 17:00:15.147 Texter[3096:207] Start 2
Program received signal: “EXC_BAD_ACCESS”.
App(3096,0x39d802b8) malloc: *** error for object 0x15fab4: incorrect checksum for freed object - object was probably modified after being freed.
freshking is offline   Reply With Quote
Old 09-19-2009, 01:43 PM   #6 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

Ok, random observations:

1. Stop using instance variables directly. See the properties link in my signature for more information. Briefly, everything that you have that is _something should be self.something instead. I'm not kidding. Rewrite everything. Do it now. This is important regardless, but is doubly so in code that gets run a lot.

2. This:
Code:
outputText = [[NSString alloc] initWithString: [incoming substringWithRange:NSMakeRange(9,([incoming length] -16))]];
...doesn't need to be an alloc/init. It can be this:
Code:
outputText = [incoming substringWithRange:NSMakeRange(9,([incoming length] -16))];
3. Add another log:
Code:
NSLog(@"Start 2");
NSLog(@"incoming is: %@", incoming);
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 09-19-2009, 03:35 PM   #7 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: Munich, Germany
Posts: 108
Default

I will start rewriting it tomorrow.

I added the log and this is what it says:

Quote:
2009-09-19 21:33:10.454 Texter[3240:207] Start 1
2009-09-19 21:33:10.462 Texter[3240:207] Start 2
2009-09-19 21:33:10.478 Texter[3240:207] incoming is: startText
Program received signal: “EXC_BAD_ACCESS”.
Texter(3240,0x39d802b8) malloc: *** error for object 0x13b0f4: incorrect checksum for freed object - object was probably modified after being freed.
freshking is offline   Reply With Quote
Old 09-19-2009, 03:39 PM   #8 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

Shouldn't incoming have a lot more text than that? Wouldn't the range's length be negative if that's all there is?
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 09-19-2009, 03:46 PM   #9 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: Munich, Germany
Posts: 108
Default

Thats is exactly another thing which is driving me crazy!
freshking is offline   Reply With Quote
Old 09-19-2009, 03:49 PM   #10 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

Well, fix that part first. If you aren't getting the correct data in the first place, the other issue doesn't matter so much.

I'm not familiar with what you are doing, but maybe NSStreamEventEndEncountered would be more appropriate?
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 09-20-2009, 05:52 AM   #11 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: Munich, Germany
Posts: 108
Default

No because this stream is kept aktive for the whole session. Once a user quits the app then NSStreamEventEndEncountered is called.
freshking is offline   Reply With Quote
Reply

Bookmarks

Tags
nsstring, stream, string, substringwithrange

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 250
18 members and 232 guests
ADY, Alsahir, beleg_1998, Dani77, e2applets, iph_s, JamesCahall, JasonR, mer10, Monstertaco, prchn4christ, Promo Dispenser, Robiwan, Rudy, smithdale87, timle8n1, Touchmint
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,756
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:20 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0