Sourcecode 'n Stuff

Blog
Software

Programming

Grand Theft
Imprint/Contact


Sticky note on top

This information is mostly sprecific to Macs and Xcode, so if you have problems with any other IDE or system this page here will probably be of no help to you, sorry.
2009 06 24

?? in stack trace

If you get a strange stack trace like
#0	0x98981edb in objc_msgSend
#1	0x0471a390 in ??
check for a double release. Try clang (aka "Build and Analyze"), but it doesn't find them all.
2010 05 12

UIViewController's view resizes automatically

If you have an UIViewController and you want to programmatically manage a view that fits between the status bar and the tab bar: forget it. The view will be resized to fullscreen automtically.
"You should not use view controllers to manage views that fill only a part of their window - that is, only part of the area defined by the application content rectangle. If you want to have an interface composed of several smaller views, embed them all in a single root view and manage that view with your view controller."
So in the UIViewController's loadView method just use UIView's init method (calls initWithFrame with a zero rect) and manage the subviews yourself.
2010 01 23

Nerd Alert

Here are some german hex words with leetspeak. Fastfood isn't relly a german word, but fa57f00d is a nice hexword I haven't found elswehere, quite surprisingly.
a177a57e (Alt-Taste = Alt-key)
ba115aa1 (Ballsaal = ballroom)
c00157e5 (coolstes = coolest
d00fd00f (Doofdoof = stupidstupid)
fa110b57 (Fallobst = windfalls)
fa17b007 (Faltboot = folding boat)
fa57f00d (Fastfood = fastfood)
0b575af7 (Obstsaft = fruit juice)
7ab1e77e (Tablette = pill)
7ade1105 (Tadellos = impeccable, flawless)
7ee7a55e (Teetasse = tea cup)
70de5fee (Todesfee = Fairy of Death)
70117011 (Tolltoll = greatgreat)

2010 01 23

Incorrect checksum for freed object

The "error for object oxfa110b57: incorrect checksum for freed object - object was probably modified after being freed." is caused by some memory corruption which happened earlier in the program flow. Turn on GuardMalloc to make the programm crash right away when it *writes* to the invalid memory, not only when it reads it.
2010 01 23

glGenTextures

If glGenTextures always generates zero, make sure you have a valid current NSOpenGLContext.
2010 01 21

Build Settings

If you don't see any build settings which you think you should see, check out the "Base SDK for all Configurations" setting in the general settings section.
Example: When you develop for the iPhone and the base SDK is set to simulator, there will be no "Code Signing Entitlements" subsection under the "Code Signing" section, which can make you pretty mad if you are eager to trot out an Ad Hoc distribution to your willing beata testers and iTunes keeps shouting "Invalid Application" into your face.
2009 06 24

Duplicate symbols

If you keep getting duplicate symbol errors from the linker (i.e. "ld returned 1" or "Developer/usr/bin/g++ failed with exit code 1", check out the "Compile Sources" phase for the current target. Sometimes it contains the same source file twice although it shows up only once in the group tree.
2009 06 23

clang

Use clang!
It just discovered a memory leak which I introduced because I falsely assumed that the Create...() functions in Core Foundation would return some sort of "autoreleased" object like the static "convenience constructors" in Cocoa. This is of course nonsene however because there is no autorelease mechanism in Core Foundation and I just wasn't thinking about it. Bummer!
In contrast to a debugger, clang compiles and analyses the code itself for potential errors, without having to execute it. It the generates a html document that lists the errors and shows you where they are by inserting comments directly into the code displayed in the html document.
In my case there was a comment like
Object allocated on line 27 and stored into 'letter' is no longer 
referenced after this point and has a retain count of +1 (object leaked)
Its so cool it's almost spooky!
If you want to use clang with an iPhone project check out this tutorial. Especially pay attention to select the right base SDK in the general project preferences.
Be aware that clang generates a false leak warning when you use Apple's singleton creation code.
EDIT: For iPhone projects, the execution of the command line tools require you to set the base SDK of the project to some simulator SDK. This will get you into trouble when you want to create an Ad Hoc distribution (see above), so remember to set it back after using clang.
2009 06 21

std::cout umlauts

If you want to read in a text file using std::ifstream and then print the text out to the console using std::cout, you have to save the text as

UTF-8, no BOM

if your text contains any above-ASCII-127 characters like ä, €, ‰, µ or é. Otherwise cout will just print some cryptic number codes.
This is proven to work on a Mac (System 10.5), using BBEdit 9.2 (for saving) and Xcode 3.1.2 (for reading and printing).
2009 06 19

UnLog

Right now I'm on a project where I generate a lot of NSlog() output in the console. Problem is, the NSLog output is prepended by additional (otherwise useful) informations, like
2009-06-11 12:53:23.733 test[77722:10b] count: 465338
If you have a lot of that and want to get rid of it, copy it over to BBEdit (for example) and do a grep search for
2.*\] 
and replace all with nothing. Do not forget the space at the end of the search string.
Caveat: This search pattern will only work until December 31st 2999 inclusively.

2009 06 11

Searching usr

If you want to search the system headers in the usr directory using Spotlight, you first have to execute
mdimport -f /usr
in the Terminal.

2009 06 11

Killing time killers

Apples "develop" magazine was one of the best of it's kind and one of the best articles in there was "Killing time killers" by Bo3b Johnson. Read it, stick to it, it will make your life easier.
BTW, when I started to study computer science I was eager to get to know people who enjoyed programming just as much as I do (I only knew graphics designers because that's what I studied first) and one of the first things I did was to post that article to our mailing list. They reaction when I asked them what they thought about that was, that they stared blankly at me. It was then that I discovered that most of them didn't like programming or had never done so. I was really shocked and I still don't get it. Why would you study computer science when you don't like programming? Its like studying music when you don't like to play an instrument. Bummer!
BTW2, anyone knows what happened to Bo3b Johnson? Is he alive and well?

2009 06 09

Setting UIDeviceOrientation

From the new iPhone SDK for iPhone OS 3.0

UIDevice Class Reference
[...]
orientation:
@property (nonatomic, readonly) UIDeviceOrientation orientation;
Returns the physical orientation of the device. (read-only)

The funny thing is, that they explicitely state (twice), that this property is read only. Since then I keep wondering how one could implement this property as settable. Fiddle with the gravity force field around the iPhone? Or the users brain to make his hands do things against his will?

2009 06 09

SFLog

This just in my mind: Some years ago I wrote an NSLog clone that uses the preprocessors stringifier operator ('#') to get the name and the @encode compiler directive and the typeof operator to determine the type of the variable to be printed. It's called it SFLog, because it was part of a bigger code collection called "Sebi Framework". So you can write:
int x = 1;
float y = 42.31415;
NSDate* date = [NSDate date];
SFLog(x);
SFLog(y);
SFLog(date);
SFLog(sqrt(2));
and it will print
x = 1
y = 42.314152
date = 2009-06-08 19:08:25 +0200
sqrt(2) = 1.41421356Ÿ30951
Remember to set the C language dialect in the build settings to GNU99, for the others do not support the typeof operator. There is quite a bit going on, so if you need to log large amounts of data you might want to use NSLog instead (see next topic).
Check it out.


2009 06 08

NSLog User Script

Typing
NSLog(@"i = %i", i);
is a bit clumsy, because, at least on a german keyboard I have to toggle between the Shift, the Alt key and no modifier key ten times (!), so I wrote me a user script for it. About ten times too. Something always goes badly wrong as soon as I write this script. I save it in the wrong place and forget to back it up then and when the next beta SDK comes out I wipe it. The hard disk crashes. Last time I did it, a few days later some assholes came and stole my computer. It's outrageous! So for the last time now, here it is:
#! /usr/bin/perl -w
my $str = <STDIN>;
print "NSLog(@\"$str = %i\", $str);";
Set the "Input" to "Selection" and the "Output" to "Replace Selection" and if you don't hear from me in three days call the fuzz and tell them, something horrible happened because of the User Script of Doom. I'm prepared.
BTW, you need to correct the format specifier, if you are not printing an int. If someone knows how to insert the correct format specifier, please tell me. I guess it's pretty hard because you would have to parse the source to get the type of the selection.

2009 06 08

Swap

Here is a nice User-Script for Xcode:
#! /bin/sh
echo -n "%%%{PBXSelection}%%%"
pbpaste
pbcopy
echo -n "%%%{PBXSelection}%%%"
It copies the selection to the clipboard and pastes the clipboard into that place instead. It also adjusts the selection to include the new text. I've been wanting this for years and finally, last Saturday with nothing better to do and a bad hangover, I spend some time looking around on the net and found this pbpaste and pbcopy stuff. To make it work you have to set the "Input" to "Selection" and the "Output" to "Replace Selection". I also assigned the key equivalent Cmd-Shift-X to it and now life is sometimes a wee bit easier.
BTW, why isn't Xcode spelled xCode? I always get thet wrong! iCaramba!
(Hmpf! whenever I think if a more or less funny iJoke someone else did it already)

2009 06 08

Yellow breakpoints

Before you start pulling out your hair because your breakpoints turn yellow and are then ignored in Xcode's debugger, turn off "Load symbols lazily" in the debugger preferences.

2009 06 08

getBestDoubleFromString

Once I was a bit annoyed by Photoshop because I tried to enter some number in some field and it kept popping alerts into my face, telling me, that the input wasn't valid. The problem merely was that I use the english version of Photoshop and a german keyboard, so I would enter a comma as a decimal separator but Photoshop wanted a point. That was back in 2003. The funny thing is, now it is the other way round. Photoshop tries to be smart and uses the formattig preferences, and because thes are german Photoshop won't accept the point a decimal separator anymore, only the comma. There are no alerts any more as well, instead the point just vanishes, as soon I hit return. So for six years straight I've been thinking now, "Why the heck can't I use both?". Back then I wrote a little method, that would convert a character sequence into a double, no matter how confused the input is. This should obviously not be used in some critical situations, but works fine most of the time. Maybe the guys at Adobe discover it sometime. Here are some features of the method:
  • if the string is completely empty or contains no valid character or is NULL, 0.0 is returned
  • '.' and ',' are interpreted as decimal seperator
  • if there's more than one decimal seperator only the first one is used
  • invalid characters are just ignored, e.g. 1.2d3 is converted to 1.23
  • if there is more than one minus sign, the first one is used, but only if is is at position 0, of course.
  • exponents are not supported, i.e. 10e3 will not return 1000 but 103
The last feature could be seen as a flaw as well, I know, but the majority of users is not acquainted with scientific exponential number formats, so I think it's OK.
getBestDoubleFromString.h
getBestDoubleFromString.c


2009 06 08

Important Shortcutz

Cmd - Alt - Up
Toggle between source and header in Xcode
Also works when the "Go to Counterpart" button is mistakenly greyed out.
 
Ctrl - /
Select next placeholder in autocompleted code
 
Command - Doubleclick on Symbol
Go to definition
 
Alt - Doubleclick on Symbol
Go to documentation
 
Cmd - Q
Most important at times.
Did you know that your brain continues to work on problems on its own, even while you think about or do something else?
Have you ever experienced to work on a problem for hours without getting it done, then went to bed and had the solution in no time on the next day?
See?
 


2009 06 07

Mac Prog

Other Mac programming tips, tricks and snippets sites:
borkware.com
chimoosoft.com
robnapier.net on xcode build settings and xcconfig files
debugging-tips-for-objective-c
digitalneurosurgeon

2009 06 07