Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios: how to solve this memory leak warning

I got the following code line:

mainLayer.shadowColor = CGColorCreate( CGColorSpaceCreateDeviceRGB(), components );

When I run Product->Analyse in xcode it gives me the warning:

Potential leak of an object allocated on line 176

So that means that I do not free my CGColor. Therefore I thought a good solution would be the following:

CGColorRef shadowColor = CGColorCreate( CGColorSpaceCreateDeviceRGB(), components ); 
mainLayer.shadowColor = shadowColor;
CGColorRelease( shadowColor );

But I still get the same leak warning. How do I repair the problem?

like image 755
toom Avatar asked Aug 10 '11 08:08

toom


People also ask

What causes memory leaks in iOS?

A memory leak occurs when allocated memory becomes unreachable and the app can't deallocate it. Allowing an allocated-memory pointer to go out of scope without freeing the memory can cause a memory leak. A retain cycle in your app's object graph can also cause a memory leak.

Where is memory leak in iOS app?

Using the Xcode Memory Graph tool To be thorough when detecting memory leaks, you should run the app and navigate through all possible flows by opening the same view controllers several times and entering the memory graph debugger to look at the memory heap.


2 Answers

You need also to release colorspace:

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGColorRef shadowColor = CGColorCreate( colorspace, components ); 
mainLayer.shadowColor = shadowColor;
CGColorRelease( shadowColor );
CGColorSpaceRelease(colorspace);
like image 188
user478681 Avatar answered Oct 23 '22 21:10

user478681


Is this:

CGColorSpaceCreateDeviceRGB()

by any change returning an object you're responsible for deallocating? I thought I remembered there being a function like CGColorSpaceRelease().

like image 28
John Heaton Avatar answered Oct 23 '22 20:10

John Heaton