Objective C++ class question
- Get link
- X
- Other Apps
i've been messing objc c++ quite time now, , building framework unify 2 languages.
have c++ class contains objc class this:
and have simple main():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; } };
the code compiles ok, on runtime, error:code:int main (int argc, const char * argv[]) { sfmutablestring str = @"hello world!"; nslog(str._str); return 0; }
i tried using autorelease pool, errors double deallocation... ideas on why happens , can do?code:[session started @ 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 no pool in place - 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 status 0.
edit: tried using autorelease pool, had remove [release str] command deallocation method of class. somehow, don't feel way supposed done. mean, if implement class (without release command on deallocation, leaving job done autorelease pool) on cocoa program, won't memory leaks?
1. sfmutablestring(const char* astring) constructor asking trouble. think crash.
2. assignment operators return object *this. highly unusual thing do, because means copy constructor called every assignment. return reference object, not object itself.
Forums Macs Mac Programming
- iPhone
- Mac OS & System Software
- iPad
- Apple Watch
- Notebooks
- iTunes
- Apple ID
- iCloud
- Desktop Computers
- Apple Music
- Professional Applications
- iPod
- iWork
- Apple TV
- iLife
- Wireless
- Get link
- X
- Other Apps
Comments
Post a Comment