One way to solve this problem is to use NSMutableData, so you can replace
unsigned char *rawData = malloc(height * width * 4);
from
myData = [[NSMutableData alloc] initWithCapacity:height * width * 4]; unsigned char *rawData = myData.mutableBytes;
you can free myData in your expander.
alternatively you can do
myData = [NSMutableData dataWithCapacity:height * width * 4];
This means that your myData is stored around the duration of the event loop, you can even change the type of the returned getRawData method to return NSMUtableData or NSData, and thus it can be saved by other parts of your code, the only time I return unprocessed bytes to my code, if I know that it will be available for the life of the object that returns it, so if I need to hold the data, I can save the owner class.
Apple often uses
myData = [[NSMutableData alloc] initWithCapacity:height * width * 4]; unsigned char *rawData = myData.mutableBytes;
and then document that if you need bytes that go beyond the current autorun cycle, you will have to copy it.
source share