0

My app crashes (occasionally - Mostly after 8 to 10 runs) - when I call CFDataGetBytePtr. Basically I want to copy pixel bytes from image and store in a buffer when I can manipulate further. Is it because I am not releasing m_PixelBuf? Or is it the wrong way to copy data?

+ (int**) UIImage2GrayInt:(UIImage*)inImage
{
CGImageRef cgImage = inImage.CGImage;
CFDataRef m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(cgImage));  
UInt8 *m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);  
int m_iH = CGImageGetHeight(cgImage);
int m_iW = CGImageGetWidth(cgImage);
//short* m_inDataGrey = malloc(sizeof(short)*(m_iW*m_iH));

int**piData = [self initVal:m_iH:m_iW];
//Convert GreyScale and copy data
int iStep,jStep;


for (int i = 0; i < m_iH; i++) 
{
    iStep = i*m_iW*4;
    //iStepGrey = i*m_iW;
    for (int j = 0; j < m_iW; j++) 
    {
        jStep = j*4;
        piData[i][j] =   ((double)m_PixelBuf[iStep + jStep] + (double)m_PixelBuf[iStep + jStep +1] + (double)m_PixelBuf[iStep + jStep +2])/3.0;
    }
}

m_PixelBuf = NULL;
CFRelease(m_DataRef);
return piData;
}

Here are the crash logs

1.  0 MyApp 0x000606ae testflight_backtrace + 238
    2.  1 MyApp 0x00061398 TFSignalHandler + 264
    3.  2 libsystem_c.dylib 0x328b87ec _sigtramp + 48
    4.  3 MyApp 0x00046fa4 +[clsGlobalHelper UIImage2GrayInt:] (clsGlobalHelper.m:276)
    5.  4 MyApp 0x00046fa4 +[clsGlobalHelper UIImage2GrayInt:] (clsGlobalHelper.m:276)
    6.  5 MyApp 0x00046d40 +[clsGlobalHelper resizeImageAvg:::] (clsGlobalHelper.m:212)
    7.  6 MyApp 0x00036fd2 -[clsNewPhoto ProcessSelectedFace] (clsFirstViewController.m:618)
    8.  7 MyApp 0x000354ae -[clsNewPhoto btnSelectGender:] (clsFirstViewController.m:309)
    9.  8 CoreFoundation 0x354063fc -[NSObject performSelector:withObject:withObject:] + 52

Here is the code of initval

+(int**) initVal:(int) Nr:( int) Nc 
{
  int ** p;
  p = calloc(Nr,sizeof(int*));

  for(int i = 0; i < Nr; i++)
  {
    p[i] = calloc(Nc,sizeof(int));

  }
  return p;
}
ThomasW
  • 16,981
  • 4
  • 79
  • 106
TorukMakto
  • 2,066
  • 2
  • 24
  • 38
  • Show code of `initVal::` – Nekto Oct 24 '12 at 04:02
  • Edited now.. added the code of initval – TorukMakto Oct 24 '12 at 04:08
  • Have you tried running the memory analyzer? Under Xcode use `Product -> Analyze`. The results might point out any memory leaks. – ThomasW Oct 24 '12 at 04:14
  • Yes - Analyze does not show any memory leaks in this function. Thats why I never doubted it.. – TorukMakto Oct 24 '12 at 04:19
  • Yes, you will need to release the memory allocated by `m_DataRef`. You will also need to properly release the memory allocated by `initVal::`. Can you show us how you do that? – ThomasW Oct 24 '12 at 04:24
  • Hi Thomas -I just updated the code - CGImageRef cgImage = inImage.CGImage; CGDataProviderRef imageDataProvider = CGImageGetDataProvider(cgImage); CFDataRef m_DataRef = CGDataProviderCopyData(imageDataProvider); CFRelease(imageDataProvider); - And then did analyze - It shows me warning that - "Incorrect decrement of reference count of a object that is not owned at this point by caller" – TorukMakto Oct 24 '12 at 04:32
  • It also crashes in simulator. And now I recall why I had removed that CFRelease statement a month back when I initially tested it... Meanwhile let me check how I am releasing the initval allocated memory.. – TorukMakto Oct 24 '12 at 04:38
  • yup - I was not releasing the memory allocated by initVal:: - just used a free statement now.. I am planning to test it using instruments-profile, what options should I use? (using it first time) – TorukMakto Oct 24 '12 at 04:48
  • Can you show the `dealloc` code? – ThomasW Oct 24 '12 at 06:19

1 Answers1

0

Answer from this question helped me solve the issue. Still don't know what was the problem with CFDataGetBytePtr but atleast I have the solution to move forward.

Community
  • 1
  • 1
TorukMakto
  • 2,066
  • 2
  • 24
  • 38