Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

idelovski

macrumors regular
Original poster
Sep 11, 2008
235
0
How are these methods implemented?

Utility methods that return objects send them autorelease message first, but cString does not return an object, it returns a c string. It is just a block of memory. That memory is allocated by the method and yet, the caller (message sender) does not need to release it.

If I wanted to write a similar method, how would I do it?
 

Guiyon

macrumors 6502a
Mar 19, 2008
771
4
Cambridge, MA
How are these methods implemented?

Utility methods that return objects send them autorelease message first, but cString does not return an object, it returns a c string. It is just a block of memory. That memory is allocated by the method and yet, the caller (message sender) does not need to release it.

If I wanted to write a similar method, how would I do it?

IIRC, the cString method is just returning a (const char*) which you should only be using for short read-only work or copying.

The NSString class handles the allocation and freeing of the space internally; once the main NSString object is dealloc'd it also frees the allocated space for the cString so if you didn't copy it and you are still using it you'd get garbage. The same thing applies to the UTF8String method.
 

idelovski

macrumors regular
Original poster
Sep 11, 2008
235
0
The NSString class handles the allocation and freeing of the space internally; once the main NSString object is dealloc'd it also frees the allocated space for the cString ...

Actually, I expected there is some magic that frees that memory before the NSString object gets released.

So, if I was about to implement something similar, a method that returns a block of memory, I'd set some char buffer to NULL in -init method, allocate and fill it in my equivalent of -cString method and free that memory on final release. Hm - no magic this time.

In other words, if I'm dealing with an object that will have a long & healthy life and if I want to give back to the system those internal buffers in my hypothetical class I would need some -freeTemporaryBuffers method.

Thank for the reply.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
How are these methods implemented?

Utility methods that return objects send them autorelease message first, but cString does not return an object, it returns a c string. It is just a block of memory. That memory is allocated by the method and yet, the caller (message sender) does not need to release it.

If I wanted to write a similar method, how would I do it?

These methods are not guaranteed to succeed. If they do succeed, they will usually have to allocate memory and convert from whatever format the NSString used internally; the memory will go away inside [NSString dealloc].


Use malloc/free together with getCString; that would be reliable.
 

idelovski

macrumors regular
Original poster
Sep 11, 2008
235
0
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.