Consulting

Results 1 to 4 of 4

Thread: Referencing Classes ByRef vs ByVal

  1. #1
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,709
    Location

    Referencing Classes ByRef vs ByVal

    Saved From: http://dailydoseofexcel.com/?s=Termi...endent+Classes

    Rob Bruce December 30, 2007 at 11:55 am

    Your Parent properties are causing circular references. These don’t get cleared until the original process is reset. In effect, that’s a memory leak.
    The VB workaround is an absolutely beautiful hack first demonstrated, I believe, by Bruce McKinney. First, change your internal variable for the parent object to a long. This is going to point to the object rather than refer to it. Then use the undocumented objptr and the hack objfromptr functions to convert the object to and from its pointer. This way you’re never storing an object reference, so the circular reference in not created.

    In your child class
     Option Explicit
    'Code edited by SamT: http://www.vbaexpress.com
    '12/03/2017 to remove non-VBA Characters and add """
    
    Private m_lngParentPtr As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
    (dest As Any, Source As Any, ByVal bytes As Long)
    The Parent property
    Public Property Get Parent() As Class1
        Set Parent = ObjFromPtr(m_lngParentPtr) 'Returns an object given its pointer.
    End Property
     
    Public Property Set Parent(obj As Class1)
    m_lngParentPtr = ObjPtr(obj) 'Returns the Pointer of a given Object
    End Property
    This function reverses the effect of the ObjPtr function.
    Private Function ObjFromPtr(ByVal pObj As Long) As Object
        Dim obj As Object
    
        'force the value of the pointer into the temporary object variable
        CopyMemory obj, pObj, 4
    
        'assign to the result (this increments the ref counter)
        Set ObjFromPtr = obj
    
        'manually destroy the temporary object variable
        '(if you omit this step you’ll get a GPF!)
        CopyMemory obj, 0&, 4
    End Function
    HTH
    Rob
    Last edited by SamT; 12-03-2017 at 06:35 PM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,785
    Location
    Interesting

    I liked the link also

    Now .... can you make it work in a 64 bit Office environment?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,709
    Location
    I can't, I don't have a 64bit environment, but you should just have to change
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _ 
    (dest As Any, Source As Any, ByVal bytes As Long)
    That might be as simple as using :
    PtrRtlMoveMemory
    or
    PtrMoveMemory

    I only posted it here so I could save this as a web page.
    Last edited by SamT; 12-05-2017 at 09:55 AM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,232
    Location
    This one slipped through the cracks....
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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