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:
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?
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;
}
So, to summarize:
1)Has this code any memory leaks?
2)Is there any other way to do what I want?