Back To Basics of Solid Code

One of my all-time favorite industry books is “Writing Solid Code” by Steve Maguire. (As you may already know.) The principles spelled out in the book are timeless, even if the implementation examples are getting old.

I’ve been using iOS environment variables to implement some automatic tests for memory issues for a while:

  • NSZombiesEnabled
  • MallocStackLogging
  • MallocStackLoggingNoCompact

The first is a big help for getting the system to tell you when you are trying to use pointers to Objective-C objects that have already been freed. The other two help find where in code a pointer was freed. They’ve been such a help that I leave them on full-time.

Yesterday I started using some of the other environment variables available. These can be a big help for finding memory management issues in your C and C++ code (legacy or not.)

These are very good for executing the Solid Coding principle “Fortify Your Sub-Systems”. Or, as I prefer to think of it, “shred your garbage”. Basically, Apple fortified the allocation subsystem for you.

  • MallocScribble – causes system allocation functions to write 0×55 over all freed memory blocks.
  • MallocPreScribble – causes system allocation functions to write 0xAA over all new allocated memory blocks before returning them.

About an hour after turning these on, I had my first hit on a dangling pointer. A pointer that was an instance member of a C++ object was set to 0×55555555 after the object had been freed. Trying to dereference it threw the EXC_BAD_ACCESS exception and dropped me into the debugger. (See my post on how to make execution stop at the beginning of an exception.)

A “random” crash is now not random and found.

XCode 4 – Stopping in the Debugger on NSAssert

I’m not sure if XCode 4 makes this technique unnecessary. Since I originally wrote this, I’ve seen XCode 4.0.2 stop at NSAssert on its own, without me setting this breakpoint. But it may be helpful to someone somehow. So here it is:

Xcode 3: Run Menu -> “Manage Breakpoints”
XCode 4: Go to the breakpoint navigator.

Click the + button at the bottom of the navigator, select “Add Symbolic Breakpoint…” from the contextual menu that appears.

In the “Symbol” box, enter

-[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:].

Then click “Done”. (NOTE: this is the default assertion handler. If you are using a custom assertion handler, you probably need to specify a symbol for its object and method.)


Now execution will break immediately on assertion failure. And very usefully, XCode shows the nearest source code of the stack, not the assembly of the assertion handler.

Tags: ,

XCode 4: Conversion Problem

Did you try to build your project in XCode 4 for the first time and get an error that starts with “There is no SDK with the name or path”?

That happened to me recently.  And the path given in the message had nothing to do with SDK’s.  In fact it was an open-source sub-project I was using.

I Googled for the answer, but mostly just found references to posts on how to fix actual SDK path problems.

It turned out that the problem was how XCode 4 looked at the sub-project.  Somehow, XCode 4 had noticed a missing Base SDK, had a static library as the product, and assumed it was a Mac OS X project.

I closed the project and opened the sub-project directly.  I corrected the project settings back to iOS and it built correctly. Closing and going back to the first project, the build now built correctly (with some warnings that I did not recall from XCode 3.)

 

 

XCode 4 Column-Wise Copy-Paste Changed

One of the strangely useful features of XCode 3 and 4 is the column-wise copy-and-paste.  But a change in XCode 4 takes away column-pasting.

Not seen this before?  Just hold the option-key while you drag a selection in XCode 3 or 4.  The text cursor will be replaced with a “+” and you can select columns of text right out of the middle of a document. (It helps if the text has vertically-oriented columns.)  It made me chuckle the first time I used it to select a column of tabs and paste them into the middle of a bunch of declarations to move them over all at once.

Pasting in a column required adjustment in XCode 3.  It would paste in the same vertical fashion as the copy, so it was easy to write over quite a bit if you weren’t careful with giving yourself space.  But then that made it interesting too.  I copied a column of member variables and pasted them all at the ends of a series of @property directives in one shot.  The same for the series of @synthesize directives.

In XCode 4, while you can copy a column, pasting it is essentially the same as having copied from any other text. Pasting does not happen in columns.

Admittedly, I did convert my columns to regular text pasting with a quick paste and re-copy in an empty file once in a while because that is what was needed.

Still,  I think I’ll miss the ability to paste in a column.

Modal View appearing the wrong location?

Is calling presentModalViewController: while a keyboard is visible making your view appear in the wrong place on-screen?

Try sending the message to a different part of the view-controller hierarchy.

I had this problem recently and was getting ready to start hunting down the current first-responder when I lucked into a quicker answer:  change the receiver from the current view controller to the navigation controller. The navigation controller handles getting objects to resign first-repsonder status without any more code from me.

Getting the view dismissed when done is easy too.  Just send the dismiss message to the modal view’s navigationController.

Online Stores and Capped ISP’s; Conflict Looming?

One thing struck me when Apple announced that Mac OS X 10.7 (Lion) would be download-only:  its size.

When they said how many Gigabytes it was, it sounded like a huge chunk of, or more than, many ISP’s bandwidth caps.

And when you add automatic online backups, and uploads and downloads of synced files, I have to wonder how many people will begin getting upset about going over their caps without realizing it.

Update 6-19-11: David Pogue conjectures about the same thing:  http://nyti.ms/mHz4oW

Something about this struck me since I first wrote: many cable providers are now offering access to shows over the internet.  How are they now going to keep their own customers from breaking bandwidth caps with their own services?

If a cap-battle does loom, there is one thing that may be spotlighted they don’t want:  how do they even measure bandwidth in the first place?  Are re-transmitted packets counted twice?  How do customers even know how much they use? And for what?    If a virus starts eating up bandwidth will customers complain that they didn’t know and should not be charged?  What about bandwidth used by programs without customer knowledge?

If you have to know how much you use,  you are going to have to learn a lot about how the internet works.   How many of an ISP’s customers want to do that?

 

XCode 4 and Little Snitch and GitHub

Today was my first try using XCode 4 with GitHub.  When I tried to clone a repository,  Little Snitch dutifully reported it and asked me to approve the connection.  I gave it “forever” permission for GitHub and the git port number.

Just after I clicked it,  I saw that XCode was reporting that permission was denied.

Just pressing “Try Again” got it going, but I noticed that it had created the folder for the repository.  When I clicked the same outer folder, it asked about replacing it and I chose “Replace”.

After it spun for a while, it gave an error about a missing folder for the checksum.  When I looked, the entire repository folder did not exist.  So I tried yet again, choosing the same outer folder.

Finally it completed and my work can go forward.

XCode 4.0.2,  Build 4A2002a

Unsung benefit of App Store: Apple’s Lawyers

I’ve been telling people for a while that one of the unsung benefits of the iTunes App Store is having Apple’s lawyers on your side when a patent troll shows up. (Even the most staunch Microsoft-only shop seems to respect Apple’s lawyers.)

I just pointing out my own supposition based on things like Apple getting the one-click patent rights with Amazon and the endless articles out there that detail the patent troll strategy of getting lots of small companies to settle before taking on bigger companies.

Now, this ZDNet article talks about Apple stepping up to protect its developers from just such patent suits:
http://news.cnet.com/8301-27076_3-20065399-248.html?part=rss&subj=news&tag=2547-1_3-0-20

Of course,  Apple (and any other app store) may have to work harder to get those kinds of broad licenses in the future.

Maybe one day,  Apple, Microsoft, Google and the rest of us will ban together to just buy out patent trolls in bulk and put the patents into a non-profit that charges a reasonable yearly fee to cover the costs of the buyouts and licenses.   (The publicly traded trolls sometimes have a lower market cap than a single lawsuit.)  Somehow that seems more likely than our government doing something about it.

 

iOS Developers of Orlando Meetup

Meet fellow iOS developers.  April 26th, 2011 at 7:00 pm.

See the Meetup site for details. http://www.meetup.com/Orlando-iOS-Developer-Group/events/17295761/

Pro iPhone Developer Meetup in Orlando

March 8, 2011

7:00 pm

Panera’s by Lake Eola in Downtown Orlando

Details on nscodernightorlando.org

This one is for professional and/or advanced iOS developers rather than for beginning developers, hobbyists, users or enthusiasts.  I look forward to deep discussions of XCode, Interface Builder, iPad-2, and the Apple App Store.

So if you are such a developer and in central Florida that night, drop by.