Hi everybody, I'm trying to get something to work properly for about 2 days but it's beyond me. So my problem is this:
I have a function that has to update a pointer with the content of a CGImageRef which is constantly changing (or almost constantly). The problem is that when I update this pointer I get more and more memory allocated and whatever I tried is not freeing up the memory, I will list the two methods that cause this huge object allocation and than try to explain my problem a little more. Below you have the method that updates my pointer with the new BitmapData, the struct that represents a pixel and the method that creates a BitmapDataContext:
As you can see I tried to deallocate the pointer and release the context every time I update but It seems I do something wrong because memory gets allocated more and more until my app crashes. I hope someone around this forum cand point me in a right direction because I'm out of clues.
Thanks in advance for your time and your help.
P.S.: I apologize for my rusty english.
I have a function that has to update a pointer with the content of a CGImageRef which is constantly changing (or almost constantly). The problem is that when I update this pointer I get more and more memory allocated and whatever I tried is not freeing up the memory, I will list the two methods that cause this huge object allocation and than try to explain my problem a little more. Below you have the method that updates my pointer with the new BitmapData, the struct that represents a pixel and the method that creates a BitmapDataContext:
Code:
//THIS IS THE STRUCT USED FOR A PIXEL REPRESENTATION
typedef struct {
Byte alpha;
Byte red;
Byte green;
Byte blue;
} Pixel;
//THIS IS THE POINTER I USE TO MANIPULATE IMAGE PIXELS AND I THINK THIS LOADS THE MEMORY
Pixel *mapData;
- (void) updateMapImage:(CGImageRef)mapImage{
if(mapData){
free(mapData); //HERE I'M TRYING TO RELEASE THE OLD POINTER DATA
mapData = NULL;
}
//I CREATE THEN A NEW BITMAPCONTEXT WITH THE NEW IMAGE
CGContextRef cgctx = [self CreateARGBBitmapContext:mapImage];
if(cgctx == NULL) {
NSLog(@"Error creating context");
}
mapWidth = CGImageGetWidth(mapImage);
mapHeight = CGImageGetHeight(mapImage);
CGRect rect = CGRectMake(0,0,mapWidth,mapHeight);
CGContextDrawImage(cgctx, rect, mapImage);
//GET THE NEW DATA OUT FROM THE BITMAP CONTEXT
Byte *mapDataBytes =(Byte *)CGBitmapContextGetData(cgctx);
if (mapDataBytes != NULL){
//REPOPULATE MY DATA POINTER
mapData = malloc(mapWidth * mapHeight * sizeof(Pixel));
for(int y = 0; y < mapHeight; y++){
for(int x = 0; x < mapWidth; x++){
mapData[y * mapWidth + x].alpha = (Byte)mapDataBytes[0];
mapData[y * mapWidth + x].red = (Byte)mapDataBytes[1];
mapData[y * mapWidth + x].green = (Byte)mapDataBytes[2];
mapData[y * mapWidth + x].blue = (Byte)mapDataBytes[3];
mapDataBytes += 4;
}
}
}
free(mapDataBytes);
mapDataBytes = NULL;
CGContextRelease(cgctx);
}
//THIS IS THE METHOD THAT CREATES MY BITMAPCONTEXT FROM INPUT IMAGE
- (CGContextRef) CreateARGBBitmapContext:(CGImageRef) inImage{
CGContextRef context = NULL;
CGColorSpaceRef colorSpace;
void * bitmapData;
int bitmapByteCount;
int bitmapBytesPerRow;
size_t pixelsWide = CGImageGetWidth(inImage);
size_t pixelsHigh = CGImageGetHeight(inImage);
bitmapBytesPerRow = (pixelsWide * 4);
bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
colorSpace = CGColorSpaceCreateDeviceRGB();
if (colorSpace == NULL){
NSLog(@"Error allocating color space");
return NULL;
}
bitmapData = malloc( bitmapByteCount );
if (bitmapData == NULL){
NSLog(@"Memory not allocated");
CGColorSpaceRelease(colorSpace);
return NULL;
}
context = CGBitmapContextCreate ( bitmapData,
pixelsWide,
pixelsHigh,
8,
bitmapBytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedFirst );
if (context == NULL){
free(bitmapData);
NSLog(@"Context not created!");
}
CGColorSpaceRelease( colorSpace );
return context;
}
As you can see I tried to deallocate the pointer and release the context every time I update but It seems I do something wrong because memory gets allocated more and more until my app crashes. I hope someone around this forum cand point me in a right direction because I'm out of clues.
Thanks in advance for your time and your help.
P.S.: I apologize for my rusty english.