Logic

I got bitten by a self-inflicted bug. The original code for embedding C++ objects in Excel using handles assumed functions returning handles occupied their own cell. Handles get created using new so there has to be a corresponding delete. The code for handle would look at the contents of the cell of the calling function and call delete if it was from a prior call. This was not perfect garbage collection, but it ensured at most one call to new for the object was performed. It also saved 1000 lines of code writing an “object manager” that would involve a lookup and a high probability of more bugs. In version 1 the funny looking doubles returned as handles are the C++ pointer. There is no lookup, only a cast.

The problem with version 1 was if a function had an argument that created a handle there was no way to call delete on it. Version 1 had to call back to Excel to get the previous value of a cell. Version 2 detected when this was not available and marked the object for deletion after the outer function returned. That can be expensive on memory, but at least it didn’t leak.

If you want to exempt a pointer from being deleted you can use safe_handle and access the handle using safe_pointer. If you go behind Excel’s back, it is your job to deal with memory. See xll_xml.cpp for examples of how I embedded libxml2 in Excel. Yes, libxml2 is a mess, but it has been battle tested and can handle XML you encounter in the real world.

The dumb thing I did was calling EVAL on a string that evaluated to a handle. The not so clever version 2 code would call delete on that since it looked like an argument to a function. The current solution is Don’t Do That!

Things have come a long way since Euclid and his axioms for geometry. It took 2000 years for people to discover his Fifth Postulate could not be derived from his other axioms by inventing non-Euclidean geometries. It was a bit shocking to realize mathematics does not involve absolute truth. Hilbert showed Euclid missed a few axioms in his theory but had the hope that any mathematical theory could prove any true statement.

Godel blew that out of the water with his Incompleteness Theorem. Any theory that allowed standard arithmetic had a true statement that could not be proved using the theory. Even if you added that statement as an additional axiom there would be other true statements that could not be proved.

Jean-Yves Girard introduced new logical connectives to bring resources under mathematical consideration. It allowed for the notion of a statement being used only once in a proof. We now have Haskell and Rust that can be run on a computer instead of being abstract mathematical constructs.

Maybe if I understood borrow checking better I could solve the EVAL problem.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s