Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8512061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:10:46+00:00 2026-06-11T04:10:46+00:00

I’m facing a weird problem. I’m developing an iOS command line barcode scanner utility

  • 0

I’m facing a weird problem. I’m developing an iOS command line barcode scanner utility using libzbar (yes, this is for jailbroken devices). All goes fine except when I’m trying to use the CGImageCreateWithPNGDataProvider() or CGImageCreateWithJPEGDataProvider() methods to obtain a CGImageRef from a file – because these two functions throw a segfault on my 5.1.1 iPad. The problem is not in my custom class, ZBarScanner, because if I use an UIImage to obtain the image data, using something like

UIImage *uiImage = [UIImage imageWithContentsOfFile:fname];
CGImageRef image = uiImage.CGImage;

then it works fine and prints the data stored in the barcode. Also, the PNG and JPEG images are well-formatted – I can view them using a file browser on the device itself and I tried several other images as well. I even tried to omit all the CFRelease() function calls and release messages in order to avoid having dangling pointers. Here’s my code:

#define LOG() NSLog(@"Reached line %d", __LINE__)

int main(int argc, char **argv)
{
    if (argc != 2)
            return 1;

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    LOG(); // line 21

    NSString *fname = [[NSString stringWithUTF8String:argv[1]] retain]; // added an extra retain just in case

    LOG(); // line 25

    CFDataRef data = (CFDataRef)[NSData dataWithContentsOfFile:fname];
    CGDataProviderRef dProv = CGDataProviderCreateWithCFData(data);
    // I also tried using
    // dProv = CGDataProviderCreateWithFilename(argv[1]);
    // that made no difference either. The data and data provider are
    // valid, but the CGImage constructors always segfault.
    if (dProv == NULL) {
        fprintf(stderr, "Invalid CGDataProvider\n");
        abort();
    }

    LOG(); // line 34

    CGImageRef image = NULL;

    if ([[fname pathExtension] isEqualToString:@"png"]) {
        LOG(); // line 39
        NSLog(@"Function pointer: %p", CGImageCreateWithPNGDataProvider);
        image = CGImageCreateWithPNGDataProvider(dProv, NULL, false, kCGRenderingIntentDefault); // This function segfaults, or...
        LOG();
    } else if ([[fname pathExtension] isEqualToString:@"jpg"]
        || [[fname pathExtension] isEqualToString:@"jpeg"]) {
        LOG();
        image = CGImageCreateWithJPEGDataProvider(dProv, NULL, true, kCGRenderingIntentDefault); // ... or this one.
        LOG();
    } else {
        fprintf(stderr, "File '%s' is neither a PNG nor a JPEG file!\n", argv[1]);
        LOG();
        abort();
    }

    LOG();
    // CFRelease(dProv);
    LOG();
    ZBarScanner *scanner = [ZBarScanner zbarScannerWithCGImage:image];
    // CFRelease(image);
    LOG();
    NSArray *arr = [scanner scan];
    NSLog(@"The result of the scanning is:\n%@", arr);
    LOG();
    [pool drain];

    return 0;
}

If I run it in the debugger (GDB and NSLog clutter removed for clarity):

gdb ./scanner
(gdb) run ./barcode1.png
Reached line 21
Reached line 25
Reached line 34
Reached line 39
Function pointer: 0x37c5b535

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x00000000
0x00000000 in ?? ()
(gdb) backtrace
#0 0x00000000 in ?? ()
(gdb)

So even the backtrace doesn’t show anything obviously wrong/helpful… It seems though that something is NULL somewhere. I even suspected that due to my toolchain being an unofficial 4.0-based build, these functions might not be available in iOS 5.1.1, so the build succeeds as the CGImageCreateWith[...]DataProvider symbols are inside the development sysroot but not among iOS’ actual dynamic libraries, but if this was the case, the function pointer I NSLogged out would be NULL, right? However, neither of the NS and CG objects nor the functions seem to be NULL – the only NULL I pass to the CGImage constructors is a decodeArray parameter, but it’s explicitly mentioned in Apple’s documentation that it can be NULL… (Update: I tried passing a valid non-NULL array to find out if the documentation is wrong, but I still got the same error).

Could you please give me any pointers (pun intended) about this crash? What am I missing here? All tutorials and references I have found so far suggest using CGDataProvider and CGImage just like this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:10:47+00:00Added an answer on June 11, 2026 at 4:10 am

    when using a filename, the sample code from the apple documentation uses a combination of what you had mentioned trying in your comments, plus the value kCGRenderingIntentPerceptual rather than the default:

        CGDataProviderRef pngDP = CGDataProviderCreateWithFilename([filePath fileSystemRepresentation]);
        if (pngDP) {
            CGImageRef img = CGImageCreateWithPNGDataProvider(pngDP, NULL, true, kCGRenderingIntentPerceptual); // true for interpolate, false for not-interpolate
    

    doing this should keep you from having to keep the data itself in your program, and may prevent the segfault you’re seeing.

    (at the very least, perhaps get and try the sample code for CoreTextPageViewer found in the official iOS documentation, build that project, and try to figure out how what you’re doing differs.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.