Method to track down exceptions memory / object problems

Posted on 2006-05-24
Last Modified: 2008-02-07
I have written a DLL that handles printing from our application using Gnostice Tools, creating PDFs.  I create a lot of objects on the fly from tables, from streams, there are datamodules and datasets created from information in a is more than slightly complex.  And it works pretty darn well.  However, when I leave the calling application I get first:
The exception unknown software exception (0x0eedfade) occurred in the application at location )x7c81eb33
and then 21 instances of:
The instruction at 0x01ef32df referenced memory at "0x00000000".  The memory could not be read.
After that everything is fine.  When I am testing this within Delphi calling my DLL from a simpler calling program, I get a wonderful clunk/thunk sound as I exit the program, but no error (I assume the clunk is the indicator that it is having a similar issue).
So, the question is, what is a reasonably efficient way to track down what I am doing wrong.  I assume that I have references to objects that are owned, but no longer exist.  I have tried commenting out code, stepping through the close down process, changing all of my .Free to FreeAnNil.
I'd like to solve this without having to look at and retry every line of code.
I need a method to track this down.
I can send code, but there is a lot of it, and really, I would prefer a method that I will be able to use in the future.
As an added piece of information, I do use interfaces on a number of my objects, but I don't think I am freeing any of those (could be wrong).  The problem is there are only 21 errors and I have hundreds of objects so I'm not thinking that is the problem.  But I'm open to suggestion at this point.
Question by:dugjohnson
    LVL 10

    Accepted Solution

    Hi there,

    I use memproof to find spots where I do things wrong. MemProof gives you the errors and leaks after a run.

    When there is an exception with "referenced memory at 0x00000000" (the latter 21), it is mostly an object variable that is nil and is accessed by a method or member. Since it is always the same instruction (0x01ef32df) it is probably in a loop where the same object is accessed (or freed) multiple times. The loop is not stopped on the first exception, so the method called does handle exceptions with a try/except without reraising the exception.

    The first one has a different address. This is mostly the case when FreeAndNil is not used and you have a dangling pointer. The first exception is probably the cause for the other 21. So you have to look for a method that when it is aborted unexpectedly cause 21 future errors.

    You might look for places where you use with/do inside with/do blocks. Name clashes can make you call the wrong object.

    You can also try and use find error on the first instruction address. This should point you to the place where it goes wrong. (Use the Delphi menu "Search/Find error")

    And a very very clever way of finding the cause for any exception is using madExcept. I think you can try the software for free. It will give you the complete stack trace (any method called) in any thread of the application on the moment the exception occurs.

    One last other options is to alter TObject.Create/TObject.Destroy (or do it at TComponent level) to update a file (inside a critical section) where you update a counter after its class name. Increase when an object is created decrease when one is destroyed. Inspect the file when the exception have occured and open the created file. You will find the classname of objects that are not all destroyted this way. If altering TObject or TComponent is not possible for you can also use this method to instrument any suspect class on its own.

    I hope this get you started.

    Regards Jacco
    LVL 1

    Author Comment

    Everything was very useful.  Gave me what I was looking for.  One minor note, it is (there is an h).  Obviously I found it all.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
    Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now