• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Program crashes when executed normally, but runs w/o trouble in the Debugger
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Program crashes when executed normally, but runs w/o trouble in the Debugger


  • Subject: Re: Program crashes when executed normally, but runs w/o trouble in the Debugger
  • From: Ladd Van Tol <email@hidden>
  • Date: Thu, 30 Jun 2005 16:42:10 -0700

Ted,

You'll need to point your "buffer" variable at a char buffer that you control rather than a return from getenv.

    You can do this by mallocing the required space. Example:

        buffer = (char *) malloc(sizeof(char)*1024);
        strcpy(buffer, getenv ("PWD"));

    .. then when you're done with buffer

        free(buffer);

Alternately, you can just declare the buffer on the stack, as you've done with localBuffer

        char buffer[1024];
        strcpy(buffer, getenv ("PWD"));

If you're just doing a one-off project, don't be afraid to make your string buffers much larger than you think you might need. In this case, I'm specifying a 1K buffer.

You may also wish to use the "safe" versions of the C string libraries like strlcat:

x-man-page://strlcat

This lets you specify the maximum length of the buffer, and avoid buffer overflow issues.

The vagaries of C pointers and C string handling can be challenging when you're first learning C, but keep at it -- it will make sense once you've been around it long enough.

Also, many of these tasks can be more easily accomplished in Cocoa, owing to the powerful framework for UI, string handling, and much more. You would have to learn a little bit about Objective-C, but this is quite easy once you have a grasp of the basics of C coding.

- Ladd

On Jun 30, 2005, at 4:01 PM, Lorance, Ted wrote:

I apologize in advance if this is one of those stupid simple things, but I am teaching myself to program in C for the Mac by porting an old curve fitting program I use from time to time. I was defeated in my attempt to use the built-in Menu:Open... function in XCode, so I fell back on using fopen after acquiring the filename from a textbox in the user interface. This worked splendidly all through development (when I was running the program in the Debugger), but neither the "development" or "deployment" builds can do any file access w/o crashing.
The error in both cases occurred while interacting with libSystem.B.dylib, and both instances are Bad Access exceptions (Exception: EXC_BAD_ACCESS (0x0001); Codes: KERN_PROTECTION_FAILURE (0x0002) at 0x00000000). One of the performance tools (which also all crashed, while the Debugger doesn't seem to notice the problem) returned the following message: "/Programming/XCode_Projects/ Grapher/Exponential Fitter.app/Contents/MacOS/Exponential Fitter accessed memory at 0x00000000 illegally. It probably tried to dereference a NULL pointer."


I am sure I am trying to do something stupid, and I could probably figure it out myself if it failed the same way in the Debugger that it does normally.

Any assistance would be appreciated; I have a fair amount of programming experience, but I am a chemist, not a programmer, and I fear it shows.

  Thanks,
  Dr. Ted Lorance


The offending code is as follows (the other file access routine is similar):


OSStatus fgetLaserData (WindowRef window)
{
const ControlID kFileName = { kCommandSignature, kFileNameFieldID };


    ControlRef cr_FileName;
    CFStringRef FileName_text;
    OSStatus err = noErr;
    char * buffer;
    int flag;
    char temp[20];
    char localBuffer[60];
    int i=0;
    FILE * in_file;
    Boolean success;

    buffer = getenv ("PWD");

err = GetControlByID (window, &kFileName, &cr_FileName);
require_noerr_quiet (err, CantGetControl);
err = GetControlData (cr_FileName, 0, kControlEditTextCFStringTag, sizeof(CFStringRef), &FileName_text, NULL);
require_noerr (err, CantGetControlData);
success = CFStringGetCString(FileName_text, localBuffer, 40, kCFStringEncodingMacRoman);
printf("%s\n",localBuffer);


    strcat(buffer,"/\0");
    printf("%s\n",buffer);
    strcat(buffer,localBuffer);
    printf("%s\n",buffer);

    in_file = fopen(buffer,"rt");
    if (in_file==NULL)
    {
        printf("File not found.\n");
        return err;
    }
    else
    {
        printf("File found. All is well. Nothing to see here.\n");
    }

    do {
        flag=0;
        fscanf (in_file, "%s", &temp);
etc.

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40criticalpath.com


This email sent to email@hidden



_______________________________________________ Do not post admin requests to the list. They will be ignored. Xcode-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Program crashes when executed normally, but runs w/o trouble in the Debugger (From: "Lorance, Ted" <email@hidden>)

  • Prev by Date: RE: fopen
  • Next by Date: large memory malloc
  • Previous by thread: Re: Program crashes when executed normally, but runs w/o trouble in the Debugger
  • Next by thread: large memory malloc
  • Index(es):
    • Date
    • Thread