ios - unsigned char allocation comes nils in offset value objective c -


I'm getting pixel color values ​​with touch points. I've been doing this successfully but sometimes app error (EXC_BAD_ACCESS (CODE) = 1, address = 0x41f6864) The memory allocation problem is the source code for your reference.

  - (UIColor *) getPixelColorAtLocation: (CGPoint) point {UIColor * color = nil ; @try {{CGImageRef inImage = drawImage.image.CGImage; // Create the screen bitmap context close to draw the image format ARGB is 4 bytes for each pixel: low, Red, green, blue CGContextRef cgctx = [self-createARGBBitmapContextFromImage: inImage]; if (cgctx == zero) {zero return; / * error * /} size_t w = CGImageGetWidth (inImage); size_t H = CGImageGetHeight (inImage); CGRAct Rect = {{0,0}, {w, h}}; // Attract the image in the bitmap context. Once we draw, then in the memory allocated for reference for the presentation / then the // will be the raw image Data in the specified color space. CGContextDrawImage (cgctx, rect, inImage); // Now we can get an indicator in image data related to bitmap // context. Unsigned four * data = {0}; Data = (unsigned char *) calloc (CGImageGetHeight (inImage) * CGImageGetWidth (inImage), CGBitmapContextGetHeight (cgctx) * CGBitmapContextGetWidth (cgctx)); Data = CGBTMAPCTextetataTata (CGTX); If (data! = Null) {// Offset X, underlines the pixels in data from Y, the data is 4/4 for 4 bytes of 4 pixels, W is the width of one line of data. Int offset = 4 * ((W * round (point.i)) + round (point.x)); // NSLog (@ "% s111111", data); Int alpha = data [offset]; /////// EXC_BAD_ACCESS (code = 1, address = 0x41f6864) int red = data [offset + 1]; Int green = data [offset + 2]; Int blue = data [offset + 3]; // NSLog (@ "Offset:% i Color: RGB one% i% i% i% i", offset, red, green, blue, alpha); Color = [UIColor colorWithRed: (red / 255.0F) green: (green / 255.0f) blue: (blue / 255.0f) alpha: (alpha / 255.0f)]; } // When finished, issue the reference / / CGImageRelease (* data); CGContextRelease (cgctx); // free image data storage for reference (data) {free (data); }} @chich (NSE Exception * Exception) {} Return color; }   

Memory management in your code appears to be incorrect:

Announce data and assign it without any value:

  Unsigned four * data = {0};   

Allocate the memory block and store it in the data - without any initial initial overwriting:

  data = (Unsigned char *) calloc (CGImageGetHeight (inImage) * CGImageGetWidth (inImage), CGBitmapContextGetHeight (cgctx) * CGBitmapContextGetWidth (cgctx));   

Now get a reference of the different memory block and store it in data , calloc ' Ad block:

  data = CGITMAPCentextet (CGTX);   

Do some other stuff and then clear the block which you have not done calloc :

  free (data);   

If you are allocating your own memory buffer then you should send it to CGBitmapContextCreate provided you are using iOS 4+, your There is no need to allocate its own buffer.

As a memory access error, you are not doing any check on the value of point and your calculation offset which is incorrect point Add checks to values ​​of and offset and take appropriate action if they are out of range (you have to decide what this should be).

hh

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -