Using static analyzer for Xojo Plugin development

In Xcode we enjoy some automatic analyzer features to find memory leaks for CoreFoundation and Cocoa object. I used them in the past to find leaks related to various objects in the code for MBS Xojo Plugins. In the last months, I got to use these for our Xojo objects and strings.

I defined my own function annotations to inform the compiler whether the function returns a REALobject or REALstring with +1 reference count or +0 reference count.

To give you an idea how many annotations I needed, here are the numbers:

REALOBJECT_RETURNS_RETAINED 13012
REALOBJECT_RETURNS_NOT_RETAINED 17
REALSTRING_RETURNS_RETAINED 2814
REALSTRING_RETURNS_NOT_RETAINED 7

Once the annotations work, we use analyze command in Xcode to find issues. I setup an example for you:

The functions NewDictionary, REALNewVariantString, REALNewVariantInteger, CString2RB and many others are annotated to return a retained object. Xcode can report that we allocated a dictionary, a string and objects in the method. And since we don’t free them, it can complain.

Adding this took months and on the way, I found a couple of memory leaks. Mostly in edge cases where memory was not released property on case of an exception or with a lot of nested branches.

In future we will enjoy this and I hope it helps us ship less memory leaks.

10 Likes