Welcome to my Programming Blog...

I am currently working on a couple of projects: An original game called "Implosion" which I am porting from Flash to the iPad, and a remake of Q*bert in Python (pygame), as part of the class I am teaching. Please feel free to use the "Labels" (at right) to follow a specific project or theme. If you are one of my Python students, I recommend that you check out the Python thread.

Saturday, June 19, 2010

Memories...

A bit of a delay since my last post - I've been tilting windmills. (Actually, I've been hunting memory leaks, which are less satisfying.)

But first, I've been trying to make some fans (besides you, dear reader). The fans in question are 8 bladed, fan shaped objects that start off in front of the purple rings, so they are entirely visible. Then, as time goes by, they move backward, so that they gradually sink into the rings and out of sight. I'm not entirely happy with the look, but the idea of a secondary object that will partially obscure the main shape is one I'm pretty settled on. When the timer expires, both the ring and the fan should disappear, and they should be replaced by one of the original shapes.

The issue has been that although I could make the ring go away, and the new shape would appear, the fan didn't disappear. The code to make it do so was placed in the "deallocation" method, and it never was being called. And it wasn't being called because I was having an imbalance in my memory retain/release calls.

Memory management is one of the more annoying parts of the older, C - based language, of which Objective C is one. In Objective C, every time you create an object, add it to a list or tell another object about it, it keeps a count of how many objects know about it (a "retain count".) Whenever you remove an object from a list or another object stops keeping track of it, the retain count should decrease (it should be "released." ) If the retain count ever gets to zero, the computer knows it can safely stop keeping track of it. In my case, it never quite got to zero. And so I spent several evenings trying to hunt down what I had done wrong.

As it turns out, it wasn't the lists I was adding the rings to, nor was it the way I created the rings in the first place. Even the objects I was telling about the rings (the next and previous chips in the sequence) were handling the retain counts properly - I just wasn't ever telling them to let go. I made the new chip follow and lead the old one (which I didn't mean to do), but the preceding and following chips still held the same connections. Once fixed, the chips and rings started working tonight.

I'm still not happy with the fans, though. Because they are angled, the light hits some vanes better than others, so only half of them appear. And because they are almost as thick as the ring itself, there is a definite difference in apparent size of the fans between the leading edges and the rear edges - they show up around the outside and inside of the ring, when they should be exactly the same size - all because of perspective. In short, it looks ugly and distracting.

So, I am going to try tilting the fans to less than 45° to see whether that helps, and if not, we will go for a shrinking disk in the middle, a lá the timer.

No comments:

Post a Comment