Code Repo    |     RSS
MD's Technical Sharing



Saturday, August 20, 2011

Xcode 4 bug: Crash when using Core Data Model from Xcode 3

I recently upgraded my Xcode from version 3 to 4.0.2 (build 4A2002a) and notice that Xcode keeps on crashing whenever I right click on a core data model used in an iOS application:


The error message reads:

Xcode encountered an internal logic error. Choose "Continue" to continue running Xcode in an inconsistent state.  Choose "Crash" to halt Xcode and file a bug with Crash Reporter. Choosing "Crash" will result in the loss of all unsaved data.

ASSERTION FAILURE in /SourceCache/IDEKit/IDEKit-303/Framework/Classes/StructureEditingAdditions/IDEStructureEditing+Additions.m:587
Details:  Assertion failed: [indexes lastIndex] <= [_targetGroup.subitems count]
Object:   
Method:   -structureEditingCanGroupSubitemsAtIndexes:
Thread:   {name = (null), num = 1}
Hints:   None
Backtrace:
  0  0x0000000100949773 -[IDEAssertionHandler handleFailureInMethod:object:fileName:lineNumber:messageFormat:arguments:] (in IDEKit)
  1  0x000000010006d394 _DVTAssertionFailureHandler (in DVTFoundation)
  2  0x00000001009a683e -[IDEContainerItemStructureEditingTarget structureEditingCanGroupSubitemsAtIndexes:] (in IDEKit)
  3  0x000000011e9e5661 -[IDEStructureNavigator _testOrGroupSelected:useContextualMenuSelection:] (in IDEStructureNavigator)
  4  0x000000011e9e2617 -[IDEStructureNavigator validateUserInterfaceItem:] (in IDEStructureNavigator)
  5  0x00007fff834a538b -[NSMenu _enableItem:] (in AppKit)
  6  0x00007fff834a50e8 -[NSCarbonMenuImpl _carbonUpdateStatusEvent:handlerCallRef:] (in AppKit)
  7  0x00007fff8348899c NSSLMMenuEventHandler (in AppKit)
  8  0x00007fff884357f7 DispatchEventToHandlers(EventTargetRec*, OpaqueEventRef*, HandlerCallRec*) (in HIToolbox)
  9  0x00007fff88434d46 SendEventToEventTargetInternal(OpaqueEventRef*, OpaqueEventTargetRef*, HandlerCallRec*) (in HIToolbox)
 10  0x00007fff88452a81 SendEventToEventTarget (in HIToolbox)
 11  0x00007fff88481c35 SendHICommandEvent(unsigned int, HICommand const*, unsigned int, unsigned int, unsigned char, void const*, OpaqueEventTargetRef*, OpaqueEventTargetRef*, OpaqueEventRef**) (in HIToolbox)
 12  0x00007fff8849510f UpdateHICommandStatusWithCachedEvent (in HIToolbox)
 13  0x00007fff88431725 HIApplication::EventHandler(OpaqueEventHandlerCallRef*, OpaqueEventRef*, void*) (in HIToolbox)
 14  0x00007fff884357f7 DispatchEventToHandlers(EventTargetRec*, OpaqueEventRef*, HandlerCallRec*) (in HIToolbox)
 15  0x00007fff88434d46 SendEventToEventTargetInternal(OpaqueEventRef*, OpaqueEventTargetRef*, HandlerCallRec*) (in HIToolbox)
 16  0x00007fff88452a81 SendEventToEventTarget (in HIToolbox)
 17  0x00007fff8849476f SendMenuOpening(MenuSelectData*, MenuData*, double, unsigned int, __CFDictionary*, unsigned char, unsigned char*) (in HIToolbox)
 18  0x00007fff885b77ae PopUpMenuSelectCore(MenuData*, Point, double, Point, unsigned short, unsigned int, Rect const*, unsigned short, unsigned int, Rect const*, Rect const*, __CFString const*, OpaqueMenuRef**, unsigned short*) (in HIToolbox)
 19  0x00007fff885b7dc2 _HandlePopUpMenuSelection7 (in HIToolbox)
 20  0x00007fff835da99b _NSSLMPopUpCarbonMenu3 (in AppKit)
 21  0x00007fff835dad4b -[NSCarbonMenuImpl _popUpContextMenu:withEvent:forView:withFont:] (in AppKit)
 22  0x00007fff8372bb37 -[NSMenu _popUpContextMenu:withEvent:forView:withFont:] (in AppKit)
 23  0x00007fff836304c0 -[NSControl _rightMouseUpOrDown:] (in AppKit)
 24  0x00007fff834270c7 -[NSWindow sendEvent:] (in AppKit)
 25  0x00007fff8335bafa -[NSApplication sendEvent:] (in AppKit)
 26  0x000000010085b36e -[IDEApplication sendEvent:] (in IDEKit)
 27  0x00007fff832f26de -[NSApplication run] (in AppKit)
 28  0x00007fff832eb3b0 NSApplicationMain (in AppKit)
 29  0x0000000100000eec

I may click 'Continue' on the dialog box to ignore the error, but subsequently Xcode would just crash randomly and too unstable to be used. For example, clicking on the Help menu would crash Xcode, but not any other menu.

The root cause, after much investigation, was the hierarchy of the .h and .m files for the core data class objects. In my original project from Xcode 3, the .h and .m files stayed under the .xcdatamodeld file in the project tree:

This was the default behavior on Xcode 3. However, Xcode 4 does not allow you to place these files under the .xcdatamodeld file, which is perhaps why it crashed since the case was never handled. I removed all the .h and .m files, regenerated them and put them under the CoreData group, and Xcode stopped crashing (for now):

I thought that was it, but unfortunately the problem did not stop there. Xcode again crashed when I attempted to commit the project to SVN:

The reason for the crash was 'editorDocument should be an instance inheriting from IDE-QuickLookdocument, but...' Perhaps only Apple knows what this really means... Nothing really helped, including restarting Xcode, closing all open source code files or cleaning/rebuilding the project. In the end, I committed to SVN from command line, checked out a fresh copy of the project and Xcode no longer crashed when committing!

Finally, I am somewhat disappointed at Xcode 4.0.2. The UI changed significantly and some of the important behavior also changed, perhaps without any documentation at all! For example, for each core data relationship, the Edit>Create NSManagedObject subclass... creates the methods to add/delete objects to the relationship in the .m file, but forgets to to put them in the header file. This results in plenty of compilation warnings if your code is from Xcode 3 and the core data class files are regenerated using Xcode 4. I have to resort to manually modifying the header files to add the correct method signatures...

I hope these issues will soon be fixed in the next release of Xcode.
Read More »

Friday, August 12, 2011

HTC HD2 Android: Working with ext2 images under Windows

My HTC HD2 running on Android from SD card had been working well for some time until it could not boot to Android one day (booting to Windows Mobile was still fine). Obviously I could redownload the same Android build, but this would mean loss of text messages, applications, call history and any other data not backed up or synchronized with Google server. In an attempt to salvage these data, I looked at the Android folder on the SD card and notice the following files:

data.img: contains user data such as text messages, applications, etc.
system.ext2 and rootfs.img: Android system files

Although most guides suggest using a Linux live CD such as Ubuntu and use the mount command to mount them as drives, which actually works, I wanted to do the same in Windows. My first try is to use Daemon Tools. This obviously has no chance of success, as the tool is meant to mount CD/DVD images, and not disk images, not to mention the Ext2 filesystem used by Android. I then tried various Windows tools to open/mount Linux images, and summarized the results in the following table. Take note that the image may be easily damaged if mounted by a tool not supporting it, or if not unmounted properly. If you want to try this, remember to back up first!



data.img
rootfs.img
system.ext2
R
R
R
X
R
R
X
RW
RW
X
RW
RW
R
X
X
X
R
R

Legend
X=failed
R=read-only access
RW=read/write access

You may need the Ext2 driver for Windows for some of the tools to work. Also read this tutorial on using IMDisk.

Looking at the table, Ext2Explore does the best by supporting all 3 images, albeit with read-only access. GizMo and ImDisk support  read/write access for rootfs.img and system.ext2 (with IMDisk being more stable), but not for data.img, where most of the user data stays. DiskInternal Linux Reader supports data.img in read-only mode, but not the other two images.

So what is so special about data.img? Some websites say it is using YAFFS2 filesystem, which only applies for phones with Android on ROM. Since my Android is on SD card, I believe data.img still uses ext2, perhaps with some special format causing most Windows tools to fail mounting it.

Extending system.ext2

My next challenge is to use Windows to extend my system.ext2 as I want to add some fonts and background images and my system.ext2 does not have enough space. This time, despite finding no Windows tools that provide read/write access to data.img, I find a set of tools from xda-developers that does the job. The author compiled the source code of dd, dumpe2fs, e2fsck, mke2fs and resize2fs to run under Windows and built a user interface for it using Tclkit:

However, the TopoResize app is poorly written and crashes all the time. I resorted to resizing the image using the command line tools mentioned above:

dd if=/dev/zero bs=1M count=XXX >> system.ext2
XXX = Amount of space in MB to be added to the image

resize2fs -f system.ext2
Resize the filesystem in the image

e2fsck -f system.ext2
Perform a check on the new filesystem. Without this, Android may hang while booting up.

The set of command line tools I used can be downloaded here. Next thing on my wishlist is to open data.img read/write on Windows; if anyone knows of a way, do leave a comment here. :)
Read More »