Goldenseal Pro Progress: Memory (July 21)

As you run Goldenseal accounting software, the app creates all sorts of things that sit around in RAM. Some last a long time, and some are just temporary.

If we don’t delete temporary items when they are done, the app will gradually consume more and more RAM. It’s called a memory leak. Eventually the computer will get low on memory, and slow down. It may run out entirely, and crash.

If we delete too soon, the app may try to use something that doesn’t exist. That causes an instant crash.

Our code sends thousands of little things flying around on your CPU every second. Keeping them from leaking or crashing is one of the biggest challenges for programmers.

The C++ language does not manage memory automatically. Fortunately, there are ways to do it without much fuss. Thanks to careful programming, the current Goldenseal rarely crashes. It does leak 40 bytes of memory whenever you open a new window, but it will take months of constant use for that to become a problem. That’s an old PowerPlant bug that we’ll lose in Goldenseal Pro.

Qt manages memory by giving a parent to everything but top-level windows. When you quit or close the window, the parent deletes all its children. Those delete their children, and so on down the chain. The system works well. If we forget to give something a parent, it floats all by itself on the screen. Hard to miss.

Newer languages like Java, Python, Swift and Objective-C manage memory automatically. The problem is, there is no guaranteed way to do that unbreakably.

Most languages use some form of reference counting. When something else links to an object, it adds 1 to the count. When they are finished, the count decreases by 1. When there are zero references, it’s time to delete the object. Most languages wait for a while then delete everything at once: it’s called garbage collection. When an app suddenly stalls out for a second or two, it’s probably emptying its trash.

The biggest problem with reference counting is that it can’t handle circles. If two objects reference each other, deleting one of them will leave a dangling reference in the other. It won’t die when it should, which causes a leak.

In Python, you are just supposed to never make circular references. That’s not easy, because often it helps to have a two-way link. We use them all the time.

Cocoa has strong and weak references to handle the problem. We found it to be hard to set up, and even harder to debug. We spent several months tracking down weird memory problems when we used Cocoa, 2016 to 2019. Sometimes ‘easy’ really isn’t any easier.

Qt is completely C++. It’s kind of like going from an automatic transmission back to manual. No need to guess what the gearbox will decide to do.

Meanwhile, our staff took a break from breakdown tables this week, and switched over to Custom Layouts for a while. It’s a nice change of pace. There are plenty of tasks left to do, and no reason to finish them in any specific order.

Dennis Kolva
Programming Director
TurtleSoft.com

Author: Dennis Kolva

Programming Director for Turtle Creek Software. Design & planning of accounting and estimating software.

Leave a Reply