Consulting

Results 1 to 3 of 3

Thread: Referencing Classes ByRef vs ByVal

  1. #1
    Moderator VBAX Wizard SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    6,988
    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 always 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 Wizard
    Joined
    Apr 2007
    Posts
    6,770
    Location
    Interesting

    I liked the link also

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

    ------------------------------------------------------------------------------------------------------------------------
    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)
    (multiple files can be selected while holding Ctrl key) / 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
    Moderator VBAX Wizard SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    6,988
    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 always expect the student to do their homework and find all the errrors I leeve in.

    Please take the time to read the Forum FAQ

Posting Permissions

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