Browsed by
Category: Mac Development

Memory bugs in Cocoa

Memory bugs in Cocoa

At work, our software runs on Windows and Mac OS.  I’m trying to reuse as much code as possible; not through some fancy cross-platform tools but by simply writing in portable C or C++ code and by using class wrappers that can each be implemented in the native platform.  One thing I’ve been pretty pleased with XCode is it’s support of mixing Objective-C and C++ code fairly seamlessly.

Recently, in trying to port some C++ Windows code over I started having some memory crashes.  Unfortunately the XCode debugger was of very little help with the stack trace not including any calls from my code.  This was an excellent thread on some techniques to troubleshoot EXC_BAD_ACCESS problems.  Fortunately, I didn’t have to try all the various techniques listed.  After reading the entire thread I decided to pursue the notion that I might be over-releasing an object (though why should this be a problem?  A warning perhaps but seems once a reference counter is down to 0 calling release again should be harmless).  This DID lead me to the offending code so I was able to fix my problem without wasting too much time.  Once again, the developer community saved me hours if not days of debugging.  How did we get along as programmers before the Web?

Debugging AutoRelease problems

Linking up Help in Cocoa

Linking up Help in Cocoa

I haven’t had to "hook-up" help in a Cocoa appplication yet since the application I inherited at work already had it working.  But recently, when the application invoked help I’ve been getting the message:

"Help isn’t available for <application name>"

I found this article to help me understand how help is hooked up in Cocoa.

I checked the required metatag and the info.plist values and they seemed in order.  What worked for me is to remove the Help Book Folder from the resources folder and re-insert it.  Be sure to select the radio button titled "Create Folder References for any added folders"

The one thing that is different in my Xcode 2.4.1 version project from the article was that the Resources folder exists inside a Frameworks folder.

objective-c error message: invalid conversion from ‘objc_object*’

objective-c error message: invalid conversion from ‘objc_object*’

This error message had me stumped for a while:

invalid conversion from ‘objc_object*’ to ‘int’

the line in question was something like this:

int iResult = [MyUtils utilsMemberFunc:param1,param2];

it doesn’t matter what the "to" type is, what is important is that you recognize that this message, in this context, is reporting that the utilsMemberFunc declaration was not found and due to objective-c’s dynamic binding it is assuming it returns an objc_object* rather than the type that utilsMemberFunc was declared to return.  So why isn’t it finding the declaration?  Because I used ‘,’ rather than ‘:’ to separate the parameters.  Besides the fact my brain still thinks in C++, I use commas to separate parameters in a variable list alot ( [NSString stringWithFormat:@"%d,%d", num1, num2]).

Through trial and error and comparing similar code that works it finally dawned on me that my syntax was incorrect.

Problem with breakpoints in Xcode

Problem with breakpoints in Xcode

I'm running Xcode 2.4.1

I have an odd breakpoint problem that I have not seen written about. I can set breakpoints no problem, my program will break correctly. My problem is when I run my program again, though the breakpoints appear marked correctly in the UI and in the breakpoint window, none of the breakpoints break.  Going to the command-line GDB (GNU debugger) via <Option><Apple> (must be in the debugger window) I type in "break info" and it reports no breakpoints!

Don't know what happened in my environment for this to start occuring but it is very inconvenient to re-enter all my breakpoints on each invocation.  I hope I find the problem soon.

Update: I found the "Stop on Debugger()/DebugStr()" command in the Debug window.  This will save me, for now, allowing me to break inside some initialization code which runs before I get a chance to set breakpoints.  I just insert a Debugger(); call in the code where I want to "break".

Update:  Today,  Aug 26, 2008, "xcode breakpoint" is appearing at #1 in Google.  In case you aren't reading the comments to this post,  Aaron's suggestion:

"In xcode go to Preferences, select the page for Debugging then make sure "Load symbols lazily" is NOT selected"

seems to fix many of the breakpoint issues for most people.  It didn't address my particular issue that I described in the post (I think my project has gotten corrupted or something), but it did help me in other instances.

Working with Pascal Strings in Cocoa

Working with Pascal Strings in Cocoa

In trying to access information from a quicktime file I came across my first pascal string (pstring).  Trying to copy this string using strncpy or NSString:stringWithCString did not work so I looked at the memory and saw a number as the first byte.  I recognized this as a pascal string so I searched on conversion from pstring to NSString and found this:

The documentation for CFString states: "in a method where you see an NSString * parameter, you can pass in a CFStringRef" but the compiler complained so as in the example code linked above, I resorted to using the NSString:stringWithFormat. My first thought was to simply use NSString:stringWithString.

Here is the wrapper function I came up with:

-(NSString*)NSStringFromPString:(unsigned char*) pstring
    CFStringRef strRef = CFStringCreateWithPascalString (NULL, pstring, kCFStringEncodingMacRoman);
    return [NSString stringWithFormat:@"%@",strRef];

Update 8/24/07: It seems the error when using stringWithString has something to do with what source is being compiled.  If it is in an .mm file (enables C++ compatibility?) I get the error:

error: cannot convert ‘const __CFString*’ to ‘NSString*’ in argument passing

but if compiled in an .m file it works fine.  It appears that in the former case, the compiler is being stricter, though a forced cast will eliminate the error message.

Mac Documentation, not so good

Mac Documentation, not so good

I’ve given the Development Documentation for XCode a few weeks, but yesterday, after spending half a day searching how to retrieve the Codec information from a quicktime file, I finally concluded that the best source of documentation is source code on the web.  With the lack of examples the documentation is only of minimal help when you are new and trying to discover HOW to do things, not just reference information.

Two sites helpful in searching code are Krugle and Koder.  Krugle has a nicer UI. I haven’t used Koder much.

XCode Project Window Layouts

XCode Project Window Layouts

It took me a while to find this so I’m posting it so I don’t forget.  Before I got my MacBook I was using a Mac Pro workstation with XCode already installed.  It had a control in the toolbar titled "Page" with three options: Project, Build, and Debug.

When I installed XCode on my MacBook I lost the ability to switch between layouts.  One of the things I don’t like working with XCode is how so many windows end up being displayed and the difficulty in finding and switching between them.  The problem is exacerbated by the fact the MIB Editor is a separate program which is not integrated with the XCode environment.  It’s too bad this All-In-One layout isn’t used by default, I may not have had such a bad first impression of the XCode UI.

I finally found how to restore my preferred 3-layout mode.  Simply select All-In-One Layout in the General tab of the XCode Preferences.  BTW, I searched using Google for "Customizing page control in XCode" to find this Apple documentation.  I had found in the View menu "Show Page Control" which gave me a clue but the default page control only gives you Project & Morph.  I though it odd that a control that has it’s own menu item, and that can contain different items, couldn’t be customized on it’s own.