Professional Web Applications Themes

Can anybody see/tell me what's wrong with this code? - Mac Programming

Style doesn't count... :) I've been chasing this one for the last two days, and still can't figure out what the trouble is. Aside from the problem noted in the comment about 3/4 way down the code (Right after the FSRead() call), the program this routine belongs in works just fine. Note that it *IS NOT* going to MacsBug due to an intentional DebugStr() - This is an all-out crash that happens every time I hit the FSRead() call, whether single-stepping in the debugger, or an all out "run it". Recovery (via "ES", "EA", etc MacsBug commands) is a no-go ...

  1. #1

    Default Can anybody see/tell me what's wrong with this code?


    Style doesn't count... :)

    I've been chasing this one for the last two days, and still can't figure
    out what the trouble is. Aside from the problem noted in the comment
    about 3/4 way down the code (Right after the FSRead() call), the program
    this routine belongs in works just fine. Note that it *IS NOT* going to
    MacsBug due to an intentional DebugStr() - This is an all-out crash that
    happens every time I hit the FSRead() call, whether single-stepping in
    the debugger, or an all out "run it". Recovery (via "ES", "EA", etc
    MacsBug commands) is a no-go - Whatever this problem is, it's a "restart
    the computer" crash to MacsBug.

    As can be seen, error-handling is rudimentary, but should be sufficient
    for the development stage (somebody flog me if I turn this thing loose
    as-is... I'll deserve it!) - I'll worry about making it "nicer" once I
    figure out why basic functionality is nonexistent...

    Here's the routine:
    (My apologies for the way it decides to wrap... It's much "prettier"
    when seen "in person")

    -_-_-_-Approx 100 lines of code follow -_-_-_-
    #include "ReadHostCacheInfo.h" // Just our prototype. (Also pulls
    <Carbon.h> into the build)
    #include "Globals.h" // A buttload of typedefs and enums,
    including the one for GlobalsStruct
    #include "Pbuffer.h" // Pr routine's prototype (Still just
    a stub)

    extern GlobalsStruct Globals;

    void ReadHostCacheInfo(void)
    {
    OSStatus Result;
    short TheVRefNum;
    long TheDirID; // Old (IM-era) docs/Universal Headers say this
    should be a short.
    // New headers and carbon docs say it's supposeed to
    be a long.
    // Compiler pukes on FindFolder(), claiming "can't
    convert short * to long *"
    // if it's a short.
    // Guess That means it's really a long...
    FSSpec TheFSSpec;
    short RefNum;
    long FileLength;
    char *TheBuffer;

    Result = FindFolder(kOnSystemDisk, 'pref', false, &TheVRefNum,
    &TheDirID);
    if (Result != noErr)
    {
    DebugStr("\pCould not find the prefs folder in
    ReadHostCache().");
    }
    else
    {
    // Use the VRefNum and DirID to construct an FSSpec for our
    HostCacheInfo file
    Result = FSMakeFSSpec(TheVRefNum, TheDirID,
    "\pStoredHostCaches.txt", &TheFSSpec);
    if (Result != noErr)
    {
    DebugStr("\pFSMakeFSSpec() call failed in
    ReadHostCacheInfo().");
    }
    else
    {
    // TheFSSpec now holds a live FSSpec to our HostCache file.
    Result = FSpOpenDF(&TheFSSpec, fsCurPerm, &RefNum);
    if (Result != noErr)
    {
    DebugStr("\pFSpOpenDF() failed in ReadHostCacheInfo().");
    }
    else
    {
    // RefNum is a reference number for an open file. How
    big is the file?
    Result = GetEOF(RefNum, &FileLength);
    if (Result != noErr)
    {
    DebugStr("\pUnable to get HostCache file's EOF.");
    }
    else
    {
    // We've now got an open file, and know how much
    space it will take to load the whole thing.
    // Allocate a buffer sufficient to hold it all.
    Include some "headroom".
    TheBuffer = NewPtrClear(FileLength+1024L);
    if ((TheBuffer == 0) || (MemError() != noErr))
    {
    DebugStr("\pUnable to allocate a buffer to hold
    HostCache file's contents.");
    }
    else
    {
    // We've got a place to put the data. Set the
    file's pointer to first byte of file.
    // (Might be overkill, but "Lets be sure"...)
    Result = SetFPos(RefNum, fsFromStart, 0L);
    if (Result != noErr)
    {
    DebugStr("\pSetFPos() failed in
    ReadHostCacheInfo().");
    }
    else
    {
    // File is positioned. Read it in.
    Result = FSRead(RefNum, &FileLength,
    &TheBuffer);
    // Program barfs into MacsBug with either a
    PowerPC Access Exception
    // or Bus Error when the FSRead() call is
    executed.
    // Why???? As far as I can tell, I'm doing
    everything right...


    // Code from here on never gets executed due
    to the crash.
    if (Result != noErr)
    {
    DebugStr("\pFSRead failed in
    ReadHostCacheInfo().");
    }
    else
    {
    // File has been read, and its contents
    are in *TheBuffer. Time to close the file.
    Result = FSClose(RefNum);
    if (Result != noErr)
    {
    DebugStr("\p Attempt to FSClose()
    HostCacheInfo.txt failed.");
    }
    else
    {
    // File is now closed. Time to p
    the data held in *TheBuffer.
    PBuffer(TheBuffer);

    }
    }
    }
    }
    }
    }
    }
    }
    }
    -_-_-_-end code-_-_-_-

    --
    Don Bruder - net - New Email policy in effect as of Feb. 21, 2004.
    I respond to Email as quick as humanly possible. If you Email me and get no
    response, see <http://www.sonic.net/~dakidd/main/contact.html> Short
    form: I'm trashing EVERYTHING that doesn't contain a password in the subject.
    Don Guest

  2. #2

    Default Re: Can anybody see/tell me what's wrong with this code?

    TheBuffer = NewPtrClear(FileLength+1024L);
    Result = FSRead(RefNum, &FileLength,&TheBuffer);

    Well since TheBuffer is a pointer, trying to read at the address of the
    buffer rather than in the buffer itself is pretyy certain to corrupt a lot
    of things.

    You should write:
    Result = FSRead(RefNum, &FileLength,TheBuffer);

    IMHO

    Eric

    Eric Guest

  3. #3

    Default Re: Can anybody see/tell me what's wrong with this code?

    In article <wRTkc.9192$sonic.net>,
    Don Bruder <net> wrote:
     

    Try

    Result = FSRead( RefNum, &FileLength, TheBuffer);

    instead, and, if you don't immediately realize your mistake, read up at
    C pointer basics (if 'FileLength' is not zero, your version of the call
    overwrites your stack)

    Reinder
    Reinder Guest

  4. #4

    Default Re: Can anybody see/tell me what's wrong with this code?

    In article <wxs.nl>,
    Reinder Verlinde <invalid> wrote:
     
    >
    > Try
    >
    > Result = FSRead( RefNum, &FileLength, TheBuffer);
    >
    > instead, and, if you don't immediately realize your mistake, read up at
    > C pointer basics (if 'FileLength' is not zero, your version of the call
    > overwrites your stack)
    >
    > Reinder[/ref]

    ARG!!!!! Darn trees kept getting in the way of the forest!

    Just what I needed - Somebody else to look at code that I've gotten
    bleary-eyed staring at. Fresh eyes spotted what I've been overlooking
    for two days in just a few minutes.

    Thanks, guys! Works exactly as expected, now that the extraneous "&" is
    gone.

    Now, if you'll excuse me, I'm going to step into the next room and beat
    myself about the head and shoulders with a large blunt implement for
    such a newbie-level mistake...

    --
    Don Bruder - net - New Email policy in effect as of Feb. 21, 2004.
    I respond to Email as quick as humanly possible. If you Email me and get no
    response, see <http://www.sonic.net/~dakidd/main/contact.html> Short
    form: I'm trashing EVERYTHING that doesn't contain a password in the subject.
    Don Guest

  5. #5

    Default Re: Can anybody see/tell me what's wrong with this code?

    In article <wRTkc.9192$sonic.net>,
    Don Bruder <net> wrote:
     

    I know your bug's been found, but what's up with 1024? This line looks to me
    like you were making random changes in hope your bug goes away.

    meeroh

    --
    If this message helped you, consider buying an item
    from my wish list: <http://web.meeroh.org/wishlist>

    Miro Guest

  6. #6

    Default Re: Can anybody see/tell me what's wrong with this code?

    In article <mit.edu>,
    Miro Jurisic <org> wrote:
     
    >
    > I know your bug's been found, but what's up with 1024? This line looks to me
    > like you were making random changes in hope your bug goes away.[/ref]

    That's actually "original code", whacked into place before ever
    encountering the trouble I was having. It'll almost certainly (unless I
    just plain forget to do it) get "tuned away" before this thing gets much
    further along, leaving the NewPtrClear() call allocating exactly the
    file's length worth of storage.

    Basically, it's nothing but overkill on my part. At least in theory, I
    already know exactly how much space is needed, and therefore, should be
    able to get away with allocating exactly that amount in the
    NewPtrClear() call. In a fit of "over-cautious", I was adding an extra K
    to the allocation to be certain I wasn't going to overflow it somehow.
    The "somehow" being related to the fact that I was originally thinking
    that I might end up parsing the file "in place", and since it's intended
    to be a network app, and data may be added to the file by network
    operations, was trying to leave room for possible expansion caused by
    any end-of-line translation that might be needed. Now that I've gotten
    further along, I've decided I'm not going to "p in place" after all,
    and realized that, if anything, I'm going to *SHRINK* the data by
    translating line-ends, it's entirely wasted space that just hasn't "gone
    away" yet.

    --
    Don Bruder - net - New Email policy in effect as of Feb. 21, 2004.
    I respond to Email as quick as humanly possible. If you Email me and get no
    response, see <http://www.sonic.net/~dakidd/main/contact.html> Short
    form: I'm trashing EVERYTHING that doesn't contain a password in the subject.
    Don Guest

  7. #7

    Default Re: Can anybody see/tell me what's wrong with this code?

    In article <mBUkc.9201$sonic.net>,
    Don Bruder <net> wrote:
     

    I'd suggest holding off as it'll only reduce the cognitive and
    visual process further.

    Perhaps sleep first...
    Not Guest

  8. #8

    Default Re: Can anybody see/tell me what's wrong with this code?

    In article <CBVkc.9211$sonic.net>,
    Don Bruder <net> wrote:
     

    Yeah, if you are not converting to DOS newlines, you can always perform newline
    translation in place without expanding the buffer :-)

    meeroh

    --
    If this message helped you, consider buying an item
    from my wish list: <http://web.meeroh.org/wishlist>

    Miro Guest

Similar Threads

  1. what's wrong with this code?
    By Chris in forum ASP.NET Data Grid Control
    Replies: 4
    Last Post: February 11th, 02:27 PM
  2. what's wrong with my code ?
    By pronto in forum ASP.NET Web Services
    Replies: 1
    Last Post: September 23rd, 10:42 AM
  3. could someone tell me what they see wrong with this code.
    By w-news in forum Macromedia Flash
    Replies: 0
    Last Post: June 8th, 06:04 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139