Prevent memory from being changed while it's in use

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    Prevent memory from being changed while it's in use

    I have a problem and I don't know why I'm having such a hard time solving it.

    The problem comes from a VB6 COM object that raises an event periodically. The call back method for the event (the method that handles the event) has a strange signature. It looks like this:

    onTheEvent(ByRe f x As AStructureType, ByRef y As Boolean)

    I need to retrieve the information stored in "x" but sometimes the next event is raised before I can retrieve this information. Since it's being passed in ByRef the underlying memory for the structure is changed and ...well...thing s get really messed up.

    How do I prevent the memory from being changed by the COM object (lock it) while I am using that memory?

    (PS...if you haven't already gathered, it's a Structure, not an Object that I'm working with)

    Thanks,

    -Frinny
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I've been trying to find information on how to copy the structure so that I'm not always pointing to the same memory location....... I am not having any luck with this though.

    The data is changed if I try a member-by-member copy in my .NET code....which means the copy will not work.

    *Frustrated!*

    -Frinny

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Yeah generally I would say the first thing you should do is copy the struct data to a local variable.
      However you go about doing that. (Is it as simple as declaring a new var and using the = operator?)

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        It would be as simple as declaring a new assignment operator except that I don't have access to the code where it should be implemented (the VB6 code).

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Ok so the event and its handler are all raised INSIDE the COM object then?

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            The event is raised by the COM object.

            My code handles the event that is raised but it is passed a variable ByRef. This means that the next time the event happens the memory is changed even though I'm in the midst of using it.

            I tried using reflection to copy the Structure but I found it was too slow, the next event was raised before it could complete (when you first start receiving events a Whole bunch of them are sent to you extremely quickly).

            I changed my code so that it didn't use reflection and things have been working without incident so far (on my development machine)...not sure how it will fair on a faster, better machine.

            -Frinny

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Right, thats what i thought the handler is in your managed code.
              So couldn't you copy the value in the handler?
              Just like when working with threads, make a local copy imediately?

              Comment

              Working...