Someone at Carbon Five posted a nice article about XCode 4 and using external dependencies. For XCode 3, I already know how to use direct dependencies and static libraries. The above article seems pretty well explained, though I have not yet tested it. I will try to give some feedback once I have the time to try it myself.
Author: root42
Direct Dependencies in iOS projects
I found a nice blog entry that explains how to build static libraries and dependent projects with XCode. This is quite nice for iOS development, and relatively elegant. The only thing that bothers me is that the search path for headers has to be augmented manually. I wonder if there is a nice solution…
Controlling MPD with the iPhone
Alignment of double values on ARM architectures
Consider the following code:
class C {
...
uint8_t *data;
size_t size;
...
void writeDouble(double v)
{
...
reinterpret_cast<double*>(this->data)[this->size] = v;
this->size += sizeof(double);
...
}
};
Looks harmless enough, doesn’t it? But here is the problem: It’s not necessarily portable or well behaving code. After debugging for about three hours, I found out something interesting. Namely that on ARM platforms, such as the iPhone, double values on the stack need to be stored at 8 byte aligned memory addresses. This was some existing code I was using, so it took me a while to get to this function and to its problem. Single precision float values have no such restriction, by the way. There are two workarounds in the above case. 1) Write a small for-loop that casts the double value to a uint8_t * and copy the double value byte-wise, or 2) use malloc to copy the value. I did the for-loop, since I thought that maybe the malloc call has too much overhead. I guess you could also cast the double to a uint32_t * pointing to two 32 bit words. Anyway, take care when doing such drastic casts. Some platforms might want alignment for some datatypes!
Valgrind checking of iOS programs
Well, this is awesome news. First, valgrind has been available for OS X for some time now. And second, you can use it to check your iOS programs on the simulator with it.
The idea here is to let your program spawn valgrind itself. Because you cannot tell the simulator to run the program through valgrind. Well, maybe you could build a funky bundle, but I think this works just fine. So here is the code, taken from the above link:
#define VALGRIND "/usr/local/valgrind/bin/valgrind"
int main(int argc, char *argv[]) {
#ifdef VALGRIND_REXEC
/* Using the valgrind build config, rexec ourself
* in valgrind */
if (argc < 2 || (argc >= 2 && strcmp(argv[1], "-valgrind") != 0)) {
execl(VALGRIND, VALGRIND, "--leak-check=full", "--dsymutil=yes", argv[0], "-valgrind",
NULL);
}
#endif
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"PeepsAppDelegate");
[pool release];
return retVal;
}
You will obviously want to define the VALGRIND_REXEC macro, if you need valgrind output. You can also pass different command line arguments to valgrind. E.g. you can switch to different valgrind tools this way, or pipe everything to a log-file.
Update: I finally got around to trying out this method. One problem here is that valgrind will fail to run, since it tries to open /dev/random, which I guess is not allowed for sandboxed applications. But one can fix this by patching and recompiling valgrind, which is not too hard. Especially when using MacPorts. Furthermore, I needed to add –dsymutil=yes to the valgrind options, or else the program would just crash.
Conditionally rewriting mail subjects using exim4
Since MobileMe sadly does not support filtering using RegExps, and I get a lot of heterogenous system mails from our computer systems here, I implemented a workaround. I added some text like [System] to the subject, which can be filtered fine by MobileMe.
All of our machines here send their mail to a central smarthost running exim4. All the system mails are directed to “root” at some of our machines. What I did was first adding a file /etc/exim4/exim.filter containing this:
# Exim filter
if "$h_to:" matches Nroot@.*.your.domain.deN then
headers add "New-Subject: [System] $h_subject:"
headers remove subject
headers add "Subject: $h_new-subject:"
headers remove new-subject
endif
system_filter = /etc/exim4/exim.filter
After that I had to run update-exim4.conf and /etc/init.d/exim4 reload. Then I was all set up.
A very small and simple PyQt OpenGL widget
For all of you who would like to have an OpenGL widget for Python. Here you have it! It’s very basic. The minimal thing is to overload the paintGL() method. Here is a screenshot:
Good for prototyping or writing small test programs.
Python class attributes versus instance attributes
Today I finally found out the difference between class and instance attributes in Python. In C++, this is done by putting the static modifier in front of the declaration. Consider the following code:
#!/usr/bin/env python
class B:
b = 2
class C:
a = B()
def __init__(self):
self.a.b = 1
c = C()
c.a.b = 3
b = C()
print c.a.b, b.a.b
Here, a is a class attribute of class C. That is, there exists only one such attribute for all objects of kind C. So the output of the print statement will be “1 1”. This is the same as a static attribute in C++. But often I want an instance attribute. The correct way to do this would have been:
#!/usr/bin/env python
class B:
b = 2
class C:
def __init__(self):
self.a = B()
self.a.b = 1
c = C()
c.a.b = 3
b = C()
print c.a.b, b.a.b
Now the output is “3 1”, just as expected. I guess this all somehow makes sense in the Python world, but I tripped over this, and worst of all: Sometimes you don’t even notice. If the class attribute is a simple type, like int, the first solution would have worked. However, I have not yet understood why that is the case. One more Python semantic that eluded me so far.
Mobile blog
Finally. My blogs now also support mobile devices with rendering suitable for small screens. All thanks to Blogger, who have introduced a beta version of the mobile templates via their beta program. You can force the template on every blog by appending “?m=1” to the blog URL. On your own blog, you can just enable it in your blog’s mobile settings via draft.blogger.com. Here’s how my blog looks in the mobile version:

Note to self: C/C++/ObjC code completion for Emacs using CLang
I have to try this out some time: There is an Emacs mode for code completion using CLang. It is mentioned in a blog entry by some guy. The nice thing here would be, that it enables ObjC code completion, which is a good thing™. Especially if your are coding for OS X or iOS.

