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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-19-2010, 07:49 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 327
thomashw is on a distinguished road
Default Probably simple problem

Hey guys, new to Objective-C. I'm pretty confident what I'm not doing right is easy and I just don't understand things 100%. If anyone could help me out that'd be great.

Does anyone see anything wrong with the following method? The first time through it works, the second time through it always goes into the if( content == nil ) code, and the third time it will crash.

If I remove all the release methods it works perfectly and doesn't go into the if( content == nil ) code.

Code:
@interface IPAddress : NSObject {
	
	NSMutableString *stringURL;
	NSMutableString *content;
	NSURL *URL;
	NSStringEncoding *enc;
	
}

@property (nonatomic, retain) NSMutableString *stringURL;
@property (nonatomic, retain) NSMutableString *content;
@property (copy) NSURL* URL;

- (NSMutableString *) getIP: (NSString *) fromURL;

@end
Code:
- (NSMutableString *) getIP: (NSString *) fromURL {
	
	[stringURL release];
	stringURL = [[NSMutableString alloc] initWithString: fromURL];
	
	[URL release];
	URL = [[NSURL alloc] initWithString: stringURL];

	[content release];
	content = [[NSMutableString alloc] initWithContentsOfURL: URL 
												  usedEncoding: enc 
														 error: NULL];
	if( content == nil )
		content = [[NSMutableString alloc] initWithString: @"0.0.0.0"];
	
	return content;
}

Last edited by thomashw; 09-19-2010 at 07:52 PM.
thomashw is offline   Reply With Quote
Old 09-19-2010, 07:59 PM   #2 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 327
thomashw is on a distinguished road
Default

Okay, it works if I comment [stringURL release] and leave the other release methods in there. Can anyone explain what I'm doing wrong with releasing stringURL?
thomashw is offline   Reply With Quote
Old 09-19-2010, 08:13 PM   #3 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

I don't see the error, assuming that no one else is changing the pointer stringURL. I might just be missing it though.

You're definitely working harder than you need to, though - you could use those properties you set up instead of doing the retain/release yourself:

Code:
- (NSMutableString *) getIP: (NSString *) fromURL {
	
	self.stringURL = [NSMutableString stringWithString: fromURL];
	
	self.URL = [NSURL URLWithString: stringURL];

	self.content = [NSMutableString stringWithContentsOfURL: URL 
												  usedEncoding: enc 
														 error: NULL];
	if( content == nil )
		self.content = [NSMutableString stringWithString: @"0.0.0.0"];
	
	return content;
}
__________________

Free Games!
smasher is offline   Reply With Quote
Old 09-19-2010, 08:20 PM   #4 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 327
thomashw is on a distinguished road
Default

Ahh, cool. Okay.

Can you explain how changing the pointer stringURL would cause an error? I don't understand why it would because I'm allocating/initializing it at the beginning of the method.
thomashw is offline   Reply With Quote
Old 09-19-2010, 08:36 PM   #5 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

I assume that other parts of the app are calling [stringURL appendFormat:] or [stringURL appendString:] ; that's fine. You're just calling some methods on the existing object.

However if you're doing something like stringURL = [NSMutableString string] elsewhere in your app, then your stringURL is now pointing at a new string that gets autoreleased and will probably cause a crash if you release it again.

If the crash is really caused by [stringURL release] then you should examine every line that says stringURL = blah.

EDIT: Oh, or if someone else is asking for someVar = thisObject.stringURL and keeping the value around and not retaining it. For example, this would cause a crash with your original code:
Code:
[thisObject getIP: someURL];

NSString *someVar = thisObject.stringURL;
[thisObject getIP: someURL];

//anything I do with someVar now will cause a crash; you released the string and I didn't retain it.
NSlog(@"%@", someVar);
__________________

Free Games!

Last edited by smasher; 09-19-2010 at 08:51 PM.
smasher is offline   Reply With Quote
Old 09-19-2010, 10:21 PM   #6 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 327
thomashw is on a distinguished road
Default

Hmm, it's a small program and nothing is done with stringURL besides that method.

content is used elsewhere in the program. Could that effect stringURL?
thomashw is offline   Reply With Quote
Old 09-19-2010, 11:24 PM   #7 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Ah! It's the encoding. You're expected to pass the address of an encoding variable, and the call fills in that variable with the encoding that was used. It looks like you're creating a pointer and passing the value of that pointer - which is probably nil.

Try this instead:

Code:
NSStringEncoding enc; // no *; this is not a pointer

// pass in &enc; the address of your enc variable
content = [[NSMutableString alloc] initWithContentsOfURL: URL 
     usedEncoding: &enc 
     error: NULL];
The difference is that this code creates *space* for an encoding, and passes the address of that space for the method to write into. You code creates a *pointer* to an encoding, and passes in the value of that pointer (nil by default).

This is an example of pass-by-reference, a technique to return multiple values from a C function (or an objc method).
__________________

Free Games!
smasher is offline   Reply With Quote
Old 09-20-2010, 11:46 AM   #8 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 327
thomashw is on a distinguished road
Default

Thanks! I'll give that a go.

Looks like it worked. Thanks!

Last edited by thomashw; 09-20-2010 at 01:49 PM.
thomashw is offline   Reply With Quote
Old 09-20-2010, 10:52 PM   #9 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by thomashw View Post
Thanks! I'll give that a go.

Looks like it worked. Thanks!
No, thank you! That was a puzzler and had me going for a while.
__________________

Free Games!
smasher is offline   Reply With Quote
Reply

Bookmarks

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: 476
15 members and 461 guests
7twenty7, AlanFloyd, AppsBlogger, David-T, iAppDeveloper, imac74, Jaxen66, lovoyl, Music Man, mutantskin, Sami Gh, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

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