Tuesday, April 11, 2017

Super Awesome Debugging Technique with Comments

Here is a trick I learned many many years ago and it remains one of the best tools in my debugging toolbox for all programming languages. This example just happens to be in C++.


#include <iostream>


int main(int argc, const char * argv[]) {
    std::cout << "Hello\n";
    
//*
    std::cout << "Hello, World!\n";
    
/*/
    std::cout << "A different Hello!\n";
//*/
    return 0;

}

now remove the first slash in the front of the first comment and you get:


#include <iostream>


int main(int argc, const char * argv[]) {
    std::cout << "Hello\n";
    
/*
    std::cout << "Hello, World!\n";
    
/*/
    std::cout << "A different Hello!\n";
//*/
    return 0;

}


This is such a handy debugging technique.

Thursday, April 6, 2017

Programmatically Creating a Window on macOS

Programmatically creating windows without using a NIB file can be tricky on the Mac. Especially if you're developing in C or C++ instead of Objective-C. Here's one of the issue most common that may come up.

Assuming you got the window creation code right, you may be thinking everything is going to work, but you get this error:

Oct 30 17:34:39  myapp[48617] : kCGErrorInvalidConnection: CGSGetCurrentCursorLocation: Invalid connection
Oct 30 17:34:39 myapp[48617] : kCGErrorFailure: Set a breakpoint @ CGErrorBreakpoint() to catch errors as they are logged.



This means your NSApplication is not being initialized. In other words, the Objective-C side of your system needs to be properly initialized. Adding the following line may help:

[NSApplication sharedApplication];

Most likely you aren't trying to do this so it is never an issue, but for those few souls out there this might just save you some serious time.