Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 12, 2006, 04:23 AM   #1
Soulstorm
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
Obj C inputs

I am learning more as I go, but I seem to have lost the ball a little in memory management techniques. I want to as you something about this code:

Code:
#include <iostream>
#include <string>
#import <Foundation/Foundation.h>

using std::cin;
using std::string;

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	char p[] = "hello message 2";
	string s;
	getline(cin, s);
	NSMutableString *str1 = [[NSMutableString alloc] initWithCString: s.c_str()];

	NSLog(@"%s and the retain value is %i", [str1 cString], [str1 retainCount]);
	
	[str1 setString: [[NSString alloc] initWithCString: p]];
	
	NSLog(@"%s and the retain value is %i", [str1 cString], [str1 retainCount]);
	
	[pool release];
    return 0;
}
Is this code valid in Obj -C++ or I am creating a memory leak here? The point is that I want to create a program that at some part will constantly access an NSMutableString and change its contents to a given string by the user. Although I know I could create an object and initialize it with an InitWithCString every time I call that function and then release it, I am looking for a better way to do it.

So, to summarize:
1)Has this code any memory leaks?
2)Is there any other way to do what I want?
Soulstorm is offline   0 Reply With Quote
Old Jun 12, 2006, 04:48 AM   #2
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
Yes, I believe you have a memory leak there, and a zombie. Any time you alloc-init an object, you have to send it an explicit release message when you're done with it.

[str1 release];

Then, when you assign the second string you perform an alloc-init without ever assigning it to anything. In this case, you aren't assigning it to the str1 variable, you are passing allocated but unassigned memory to it. It's got a retain count of 1 and it will never be released (the retain count will never reach 0 because there is no reference pointing to it).

In these cases, you could use convenience constructors, which are autoreleased, such as:

NSMutableString *str1 = [NSMutableString stringWithCString: s.c_str() encoding:NSASCIIStringEncoding];

(By the way, NSMutableString's initWithCString: method is deprecated, you should not be using it, use the one that specifies an encoding instead.)

You don't have to explicitly release autoreleased objects, they will be sent a release message when the autorelease pool is released.
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Fix the audio input jack? davidlv MacBook Pro 5 Nov 25, 2011 04:24 AM
Using Mic Input With iOS Simulator Wellington2k iPhone/iPad Programming 0 Oct 16, 2011 01:12 PM
Cocoa - Obj C and Java? jeremy.king Mac Programming 3 Sep 26, 2004 09:32 PM
XCode / Obj C - Websites / tutorials SilentPanda General Mac Discussion 3 Feb 18, 2004 01:18 PM
Keynote Objects 1.0 package includes 15 high-quality 3D obje... MacBytes New Mac Application Announcements 0 Aug 22, 2003 08:33 AM


All times are GMT -5. The time now is 02:33 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC