GDI Memory Management

Team-Fly    

 
Application Development Using Visual Basic and .NET
By Robert J. Oberg, Peter Thorsteinson, Dana L. Wyatt
Table of Contents
Chapter 11.  Introduction to GDI+


GDI+ Memory Management

An important benefit in programming with the .NET Framework is memory management facilities provided by the Common Language Runtime, including automatic garbage collection. Normally, you do not have to be concerned with explicitly freeing memory, like you do in some languages, such as C++. There is an important exception to this simplification, however. Whenever you create an object that allocates unmanaged memory, there is the potential for running out of resources that will not be taken care of by the system garbage collector.

Objects in GDI+ provide an example of resources with unmanaged memory. Device contexts, as encapsulated by Graphics objects, pens, brushes, and fonts, all encapsulate GDI resources, which are not managed by .NET. Thus, even if there is plenty of free memory in the system and the garbage collector is not invoked, it is still possible for a program to fail because the system has run out of some other kind of resource. This issue is quite real for GDI resources on Windows 9x systems. It may also become an issue on new systems targeted towards mobile devices with a relatively small amount of memory.

.NET provides a design pattern for dealing with resource deallocation in such situations, as we discussed in Chapter 10. A class that wraps an unmanaged resource should implement the IDisposable interface, and a client of that class should call the Dispose method when it is finished using an object instance of that class.

graphics/codeexample.gif

Version 2 of the RectangleDemo program illustrates use of Dispose . In the Version 1 program we keep on creating Graphics objects in the mouse event handler. Eventually, the garbage collector would deallocate these objects, and in turn the underlying device context would be freed. However, if other memory in the system is not low, the garbage collector may not be called, and the system might run out of device context resources. To avoid this possible problem, the program should call Dispose before exiting the mouse handler.

 Public Class Form1    Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " ...    Private Function MakeRectangle(ByVal p As Point, _     ByVal q As Point) As Rectangle       Dim top, left, bottom, right As Integer       top = IIf(p.Y < q.Y, p.Y, q.Y)       left = IIf(p.X < q.X, p.X, q.X)       bottom = IIf(p.Y > q.Y, p.Y, q.Y)       right = IIf(p.X > q.X, p.X, q.X)       Return New Rectangle(left, top, right - left, _          bottom - top)    End Function    Private Sub Form1_MouseDown(ByVal sender As Object, _     ByVal e As MouseEventArgs) Handles MyBase.MouseDown       Dim g As Graphics = CreateGraphics()       Dim p1 As New Point(20, 10)       Dim p2 As New Point(e.X, e.Y)       Dim rect As Rectangle = MakeRectangle(p1, p2)       g.DrawRectangle(Pens.Black, rect)  g.Dispose()  End Sub End Class 

Code Examples Are Simplified

It is important to remember that code examples in this book are simplified to focus on the point at hand. Thus, the main body of GDI+ example code illustrates the various classes and programming techniques used with GDI+. We have usually not added the code to handle resource deallocation issues. This simplification is similar to our policy regarding exception handling. We try to keep our example programs simple. But in production code it is important for you to worry about resource deallocation, exception handling, and similar issues.


Team-Fly    
Top
 


Application Development Using Visual BasicR and .NET
Application Development Using Visual BasicR and .NET
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 190

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net