Retain or copy NSEvents?

  • I have some classic drag and drop code, where I save the mouse down
    event, and then later provide this event to dragImage when the drag
    and drop begins.

    My original code simply retained the mouseDown event. A month or so
    ago I was reading some Apple document that said the system sometimes
    "reuses" an event, and thus the proper way to retain an event is to
    make a copy of it. I modified my code and made a simple note that
    Apple advised doing this. [I also recall going to the online NSEvent
    document and making a comment that this fact should be in the NSEvent
    documentation, and sited the reference.]

    Of course, now that I was asked for a reference on this, I cannot for
    the life of me find it. I tried all the Event and NSEvent
    documentation, NSRunLoop, and Drag and Drop.

    Does anyone out there recall where this is documented? If so please
    speak up! Thanks,

    David
  • On Dec 15, 2007, at 7:50 AM, David Hoerl wrote:

    > I have some classic drag and drop code, where I save the mouse down
    > event, and then later provide this event to dragImage when the drag
    > and drop begins.
    >
    > My original code simply retained the mouseDown event. A month or so
    > ago I was reading some Apple document that said the system sometimes
    > "reuses" an event, and thus the proper way to retain an event is to
    > make a copy of it. I modified my code and made a simple note that
    > Apple advised doing this. [I also recall going to the online NSEvent
    > document and making a comment that this fact should be in the
    > NSEvent documentation, and sited the reference.]
    >
    > Of course, now that I was asked for a reference on this, I cannot
    > for the life of me find it. I tried all the Event and NSEvent
    > documentation, NSRunLoop, and Drag and Drop.
    >
    > Does anyone out there recall where this is documented? If so please
    > speak up! Thanks,

    In the book "Cocoa Programming" (Anguish, Buck, Yacktman) Chapter 8,
    in the "What is a Responder?" section (page 192):

    "The NSEvent passed to each event-processing method is only valid
    within that method's implementation. The Cocoa frameworks reserve the
    right to reuse existingNSEvent instances or otherwise tamper with
    their contents. To preserve the information in an NSEvent instance,
    copy it or store the information in a separate data structure. Simply
    retaining the NSEvent instance for later use is not sufficient."

    As to what Apple document (if any) that this was derived from, I do
    not know.

    ___________________________________________________________
    Ricky A. Sharp        mailto:<rsharp...>
    Instant Interactive(tm)  http://www.instantinteractive.com
previous month december 2007 next month
MTWTFSS
          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            
Go to today