Detect Unused Objects

Improper use of objects and object references can cause your application’s objects to stay resident in memory. Such “memory leaks” often causes system performance degradation and prevents your application from scaling well over time. Instead of using complex system utilities and third-party tools to detect such “bad code” in your application, you can put VB5/VB6’s event technology to work for you.

Create an ActiveX DLL-such as ListObjects.dll-with a single class: ResidentObjs.cls. Add a method to raise an event. You may call this subroutine DetectAllObjects. Declare an event in the Declarations section of the class:

 Option ExplicitPublic Event ObjectNotification()Public Sub DetectAllObjects()	DoEvents	' let the system do its job.	RaiseEvent ObjectNotification()End sub

In your application, add a reference to ListObjects.dll to your project. Then declare a global variable in a BAS module as a ResidentObjs type, and instantiate this object variable at Application startup:

 Sub Main()	Set oListObject = New ListObject.ResidentObjsEnd Sub

In each class of your project, you must declare a ResidentObjs variable using WithEvents. Set this object variable to the global variable upon class initialize or some other class function that is called each time you create an object of that class. In the event function, you can add any code that warns you about the current object instance. For example, you can add Debug.Print, or some code that writes the class name to a file.

At any suitable point in your project code, you can call DetectAllObjects(). The number of times the event function gets called is the number of objects resident in your memory at that time. To zero in on only some of the main classes, you can limit the DLL object declaration to only those classes.

How does this work? Events are a form of anonymous broadcast messages. They are sent to every object instance in your application. Hence, this simple yet powerful mechanism can detect invalid objects. In fact, you can step into the code to actually see the event function being called several times.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

Top 5 B2B SaaS Marketing Agencies for 2023

In recent years, the software-as-a-service (SaaS) sector has experienced exponential growth as more and more companies choose cloud-based solutions. Any SaaS company hoping to stay ahead of the curve in this quickly changing industry needs to invest in effective marketing. So selecting the best marketing agency can mean the difference

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.