tags:

views:

104

answers:

2

Hi everyone,

I understand Objective-C memory management but I'm using Core Graphics functionality such as CGRect, CGPoint, CGImageRef etc.. which is written in plain C.

My question is how do i manage this memory or is it already handled for me? According to the Apple documentation if an Apple Objective-C function doesn't have copy, new or create in it the returned object is managed for you using autorealease. Is this true for the Core Graphics stuff also? (Well i guess it wont be using autorealease but maybe something similar?)

Thanks for taking the time to read this.

+4  A: 

Nothing in CoreGraphics or CoreFoundation is autoreleased. In general, you own a CF/CG object if the method you got it from had copy or create in its name. However, with CF/CG it is really important to read all the documentation for the method you are calling. The documents will always give you any memory management considerations.

Also, you must read the documents for all the data types that you are not familiar with. For example, CGRect is a simple structure, so it is almost always within the automatic scope (i.e., allocated on the stack for you). Unless you create and allocate memory for a pointer to a CGRect (which is not a common case), there is no memory management involved.

If you feel unsure, I recommend you read all the documentation for each function you call and each data type you use with any of the CF C API.

Jason Coco
Thanks a lot for a really helpful reply. I never even considered that CGRect or similar might be allocated on the stack. I better read that documentation.
toc777
A: 

CGRect, CGPoint, etc. are just plain structs, so you can just pass them around by value... you don't need to allocate them on the heap. However, if you do allocate them on the heap, then you will need to use malloc/free to manage them yourself. Most CoreGraphics functions that return these objects simply return the object (e.g. CGRect) and not a pointer to them (e.g. CGRect*), so you don't need to worry about it.

Michael Aaron Safyan
Hi Michael, thanks also for your helpful reply.
toc777