Professional Web Applications Themes

IsHandleValid - Mac Programming

Am I missing something here? If I throw an arbitrary value at IsHandleValid it crashes the app (OS 10.2.6). What's the point of the function if you can only pass it a valid handle?...

  1. #1

    Default IsHandleValid

    Am I missing something here? If I throw an arbitrary value at
    IsHandleValid it crashes the app (OS 10.2.6). What's the point of the
    function if you can only pass it a valid handle?
    Steve Guest

  2. #2

    Default Re: IsHandleValid

    In article <google.com>,
    com.au (Steve Luckett) wrote:
     

    First, you shouldn't be using that call for anything other than
    debugging purposes. That might be what you're doing already, of course.
    :)

    Second, it's intended for use with handles whose pointers may no longer
    point to valid memory. It isn't intended for use with arbitrary values,
    and it will crash in many of those cases.

    -Eric

    --
    Eric Albert edu
    http://rescomp.stanford.edu/~ejalbert/
    Eric Guest

  3. #3

    Default Re: IsHandleValid

    Thanks. I thought later that may be the case. You are after all
    expected to pass a Handle. However, I haven't found any more
    comprehensive doentation on the function, and I'd still like a way
    to determine if a given value is in fact a Handle (without keeping a
    record of all those I create).
     
    Steve Guest

  4. #4

    Default Re: IsHandleValid

    In article <google.com>, Steve
    Luckett <com.au> wrote:
     
    >
    > Thanks. I thought later that may be the case. You are after all
    > expected to pass a Handle. However, I haven't found any more
    > comprehensive doentation on the function, and I'd still like a way
    > to determine if a given value is in fact a Handle (without keeping a
    > record of all those I create).[/ref]

    Curious statement at the end there...anyways: the general thing with
    handles is to allocate them when you need them, and free them when done.
    If a variable is sometimes a valid handle and sometimes not, I always
    set it to a testable value after deallocating:

    static Handle myHandle = 0L;
    void SomeFunction(void)
    {
    if (myHandle != 0L)
    {
    myHandle = NewHandle(1024);
    // don't forget to test for failure of NewHandle()!!!
    ...

    DisposeHandle(myHandle);
    myHandle = 0L;
    }
    }

    If the handle is coming from some code over which you have no control,
    I guess you could at least sanity-check it: since a handle is a pointer
    to a master pointer, the memory address you get from a single
    dereference should be in an appropriate address range. Likewise, the
    master pointer should be pointing to a reasonable area of memory - not
    into code, for example, if the handle is supposed to be a data
    container. At a minimum,

    if (myHandle != 0L)
    if ((*myHandle) == 0L)
    DebugStr("\pDanger, danger, Will Robinson!!");

    Easier to use some method of keeping track of things from the get-go,
    though.
    David Guest

  5. #5

    Default Re: IsHandleValid

    > If the handle is coming from some code over which you have no control, 

    Yes, this is indeed the case. I'm doing a partial (gasp) Windows API
    emulation to fasttrack a series of platform ports and I have struck
    cases in the alien code where an unitialised handle is passed to me
    for deletion. So I was hoping IsHandleValid would at least indicate it
    was safe to delete. Thanks for your feedback - much appreciated.
    Steve Guest

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