Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB5,VB6
Expertise: Intermediate
Jul 15, 2000

Beware of hidden circular references between a form and an object

Many VB developers use a class module to augment the capabilities of a control on a form. For example, consider this simple example, that formats a currency value in a TextBox control when the focus leaves the control:

' ------ the FORM1 module
Dim Formatter As New CFormatter

Private Sub Form_Load()
    ' bind the object to a textbox
    Set Formatter.TextBox = Text1
End Sub

' -------- the CFORMATTER class module
Public WithEvents TextBox As TextBox

Private Sub TextBox_LostFocus()
    ' format the value when the focus leaves the control
    TextBox.Text = Format(TextBox.Text, "###,##0.00")
End Sub

The above code contains an hidden circular reference between the form and the instance of the CFormatter class. The circular reference isn't apparent at first, because the CFormatter object references the Text1 control, not the form itself. However, because the Text1 control - or any other control for that matter - can exist without its parent form, this creates the circular reference.

This hidden circular reference can cause a memory leak in your application. In fact, when the VB attempts to unloads

Francesco Balena
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date