I've been messing with ObjC with C++ for quite some time now, and I am building a framework to unify those two languages.
I have a C++ class that contains an ObjC class like this:
And I have this simple main():
The code compiles OK, but on runtime, I get this error:
I tried using an Autorelease pool, but then I get errors about double deallocation... Any ideas on why this happens and what can I do?
EDIT: I tried using an autorelease pool, but I had to remove the [release str] command from the deallocation method of the class. But somehow, I don't feel that this is the way it is supposed to be done. I mean, if I implement this class as it is (without the release command on deallocation, leaving the job to be done by an autorelease pool) on a Cocoa program, won't I get memory leaks?
I have a C++ class that contains an ObjC class like this:
Code:
class SFMutableString{
public:
NSMutableString *_str;
SFMutableString(){_str = [NSMutableString new];}
~SFMutableString(){
[_str release];
NSLog(@"str deallocating....");
}
SFMutableString(const char* aString){
[_str setString:[NSMutableString stringWithCString:aString encoding:NSUTF8StringEncoding]];
}
SFMutableString(const SFMutableString& other){
_str = [NSMutableString stringWithString:other._str];
}
SFMutableString(NSString* aString){
_str = [NSMutableString stringWithString:aString];
}
void getString(const char* aStr){
[_str setString:[NSMutableString stringWithCString:aStr encoding:NSUTF8StringEncoding]];
}
SFMutableString operator=(SFMutableString& other){
[_str setString:other._str];
return *this;
}
SFMutableString operator=(const NSString *otherString){
[_str setString:[NSMutableString stringWithString:otherString]];
return *this;
}
};
And I have this simple main():
Code:
int main (int argc, const char * argv[]) {
SFMutableString str = @"hello world!";
NSLog(str._str);
return 0;
}
The code compiles OK, but on runtime, I get this error:
Code:
[Session started at 2007-08-27 10:19:08 +0300.]
2007-08-27 10:19:08.245 foundation tool 1[1100] *** _NSAutoreleaseNoPool(): Object 0x605050 of class NSCFString autoreleased with no pool in place - just leaking
2007-08-27 10:19:08.245 foundation tool 1[1100] hello world!
2007-08-27 10:19:08.245 foundation tool 1[1100] str deallocating....
foundation tool 1 has exited with status 0.
I tried using an Autorelease pool, but then I get errors about double deallocation... Any ideas on why this happens and what can I do?
EDIT: I tried using an autorelease pool, but I had to remove the [release str] command from the deallocation method of the class. But somehow, I don't feel that this is the way it is supposed to be done. I mean, if I implement this class as it is (without the release command on deallocation, leaving the job to be done by an autorelease pool) on a Cocoa program, won't I get memory leaks?