Category Archives: Development

Drupal 7 – Debugging Illegal offset type in isset or empty in … and other errors

Though the title mentions Drupal 7, this article is really about the debugging technique and so is applicable to general php as well. I want to share a recent bug we had to solve at work. Though experienced developers would usually have an idea what new code is breaking, mysterious warnings and errors sometimes do slip through while working in teams (even when having a proper svn system in place!). You check with your co-developers and the one who broke it have no idea where this might be coming from, or worse: they have seen and have been ignoring this error for some time because they felt it didn’t matter. What do you do then?

Read the error
The error message “Illegal offset type in isset or empty in…” points to an illegal type used in a call to isset or empty, and it conveniently points you to the offending line – but with Drupal’s (and most of today’s frameworks’) complex page execution flows, it is hard to spot where is the real source of error, especially if the line is in one of the framework’s frequently called core routines (in my case it was in user_access). Since this is not much help in trying to deduce the real source, I resort to setting my own error handler (temporarily overriding Drupal’s) so that I can do a debug_back_trace().

To set your own error handler, define a function and feed the name to php’s set_error_handler():
[php]
// Your custom handler
function my_error_handler($errno, $errstr, $errfile, $errline
, array $errcontext)
{
// error was suppressed with the @-operator, ignore
if (0 === error_reporting()) {
return false;
}

// print the backtrace
echo ‘<pre>’.print_r(debug_backtrace(),true).'</pre>’;

// throw an error exception if you want to catch it at the caller
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}

// Back at the offending line:
set_error_handler(‘my_error_handler’);

try {
// original code
}
catch (ErrorException $e) {
// print your own debug log or what not
}

// Restore Drupal’s own error handler. You don’t want a backtrace
// on every other E_USER_NOTICE’s do you?
restore_error_handler();

[/php]

While you may find it useful to convert every error into an exception, note that even E_USER_NOTICE would then halt your page execution, so please use with care (refer to php manual on the ErrorException class for a handler that ignores non-fatal errors).

Now, what’s left is to carefully read through the debug backtrace and spot the problem. For my case, it was a line of code someone embedded in a view context. Hope this helped, share your own debugging methods and opinions in the comments.

End note
Some people may feel that my style of debugging isn’t very ‘elegant’ but my experience is that the fastest way to find the problem isn’t necessarily the most elegant way. Rather than going through hours of deducing which functions might or might not be involved, and later realising the problem was in one of those functions that “logically shouldn’t have mattered in this case”, I find that getting a solid backtrace – after a quick deduction fails – is usually my best bet in the long run.

Fixing Drupal’s Canonical link meta tag

Fblint complained that one of my page’s canonical link mismatches the og:url meta tag. Turns out Drupal 7 outputs relative canonical links by default and although there’s nothing strictly forbidden about relative links, both facebook and google seems to prefer absolute links. Rather than altering the offending tag through hook_html_head_alter() or even worse, mess with the core files, I decided to install the Metagtag module (http://drupal.org/project/metatag).

The default configuration looked fine (the module even supports og meta tags!) so I ran the page through fblint again, hoping it to be another fine install-set-go Drupal 7 module. 58827 sites were using this module at last count, that’s pretty reliable. I was dismayed when I got the same results and a quick check revealed that apparently the module modified none of my head tags. I also noticed that its only the front page that was having this issue. The module works fine on all other pages. A few drupal.org threads later I arrived at the solution: calling render($page['content']); is all it needs to get it to work on overidding page–*.tpl.php’s. Yup just calling render() will do, no need to print.

Solution thread: http://drupal.org/node/1293214

Tips while profiling your app to solve low memory warnings and crashes

Just a quick post to share what I learnt while investigating crashes in my iPad app. The crash reports consistently showed low memory warnings before the crash so I started using instruments to profile the app. Here’s some tips that may help you out when you are stuck using the instruments tool:

Turn off Zombies. Zombies are great for debugging messages sent to de-allocated objects, but they come at a cost of memory – NSZombies are never deallocated. Turning this off immediately showed great reductions in terms of live bytes in the allocations instrument. Apple’s documentation advises zombies to be used only on the simulator while debugging and testing.

Mark your heap before and after performing actions on your app to inspect the memory changes introduced by the action. A simple evaluation to keep in mind is that if any set of actions returns the app to the same state as before the actions, the changes in heap before and after the set of actions should be close to 0. Any significant amounts detected here would signal a leak, which need not necessarily reflect in the leaks instrument profile. Many conditions, like circular references, may cause actual leaks to be ignored in the leaks instrument, but that’s the focus of another article.

The Allocations and Activity Monitor operates differently, and gives very different results. I made the mistake of assuming the ‘live bytes’ column in the allocations report to be what my app was consuming. While allocations reported that my app is using a consistant amount of memory, I was still receiving low memory warnings, so I finally decided to run activity monitor. Surprise – the low 6.3mb consumption in allocations actually translated to 180mb in activity monitor. Why is that so? Turns our when you deallocate memory in your app, or even if you are using ARC, the memory is only released at undetermined intervals when iOS feels like it. While allocations track every memory your app allocates and deallocates, the activity monitor gives a true reflection of your app’s memory footprint.

But of course, those latent released memory wouldn’t chalk up to 100 over mb’s. The real culprit turns out to be CoreGraphics, while it handles all on screen drawings and image operations behind the scenes. The real memory consumed by these operations are not reflected in allocations at all. ARC also does not track CG objects so do remember to release them properly.

Now knowing the real issue, I proceed to refactor and restructure my drawing code to improve reuse of bitmap contexts and reduce repainting areas by keeping track of dirty rectangles. Hope these tips would help give some ideas to those of you debugging your crashes at 3 in the morning. Do also share some of your own tips or experiences in the comments!

Storing images on file system vs database

While researching efficient methods of storing large numbers of images on my server, I came across this post detailing the process of returning an image from a mysql server. The process described is common, except that it is inspected at a much lower level, complete with mode switching between kernel and user on a linux server:

Your systems network buffer fills with a new packet containing a new request from the network card. The system delivers the packet to your Apache, switching from kernel mode into user mode.

Apache processes the packet, handing it internally to its mod_php for processing. The mod_php parses the request, constructing a SQL query for the image blob, and pushes the SQL query into a kernel buffer, switching from user mode into kernel mode.

Check out the full post at http://mysqldump.azundris.com/archives/36-Serving-Images-From-A-Database.html

Galaxy Note 10.1 to include Photoshop Touch

The new Galaxy Note 10.1 will be released to the public on 16th August and Samsung has preloaded it with Photoshop Touch. This will be the first time that a feature of this kind has been included on a mobile device, so it marks a significant milestone in the technology world. This was made possible, after Samsung teamed up with Adobe Systems in order to load Photoshop Touch onto the new version of the Nexus. With this feature, users will be able to sync files to the Adobe Creative Cloud, as well as having access to 2GB of free storage space.

One of the advantages of the Galaxy Note 10.1 is its affordability when compared with computer systems. With its Stylus Pen, users can still use great apps, without having to spend a lot of money in order to obtain them. So while desktop computers may be useful for some things, like typing out documents and playing party poker, the new Galaxy Note 10.1 is a good choice for app lovers. Users can edit photos while they are on the move, with their fingers or the Stylus Pen, as there are many features within Photoshop Touch that make this easy.

The preloaded version of Photoshop Touch includes some great features, such as selection tools, layers, filters and adjustments. Users can edit images to create wonderful works of art, or simply to tweak the photograph and make it appear how they want it to. The unique camera fill feature uses the tablet’s camera to fill in an area on a layer, which is a very useful tool. There is also an integrated Google Image Search, with which users can look for images, as well as the ability to share images to Facebook. This new release signals the start of something great for app lovers and should go a long way to making many image editor’s lives easier.

Free Transform Manager As3 v1.5.1

Many of you have been emailing me about supporting objects with non-top-left registration points. Here’s an update that recognizes and compensates for all registration points. Give it a try and let me know if it works. You can find the older version here.

[kml_flashembed movie=”http://ryantan.net/content/FTM/TestFTM.v1.5.2.swf” height=”600″ width=”630″ /]

Download:
v1.5.1 Source + Examples
v1.5.2 Source + Examples

Added a dragArea property in 1.5.2 that accepts a flash.geom.Rectangle that specifies the area the objects a confined to. But it has not been fully tested so please do report bugs when you are using them.

Some of you also asked about how to use FTM in flex, here’s a simple .mxml demonstration: Continue reading

error: Unsupported Feature: to-many relationship … option requires Mac OS X 10.7 or later

While tinkering with the relationship properties in my Core Data model, my app suddenly failed to compile with this error:

error: Unsupported Feature: to-many relationship [Entity].[attribute]
option requires Mac OS X 10.7 or later

While it says to-many relationship is not supported, I know that’s not the real problem as I have been compiling fine with the ‘to-many‘ option turned on for a few weeks. What it’s really trying to tell you is an ‘ordered‘ AND ‘to-many‘ relationship is not supported. Once I unchecked the ‘ordered‘ option the app build fine again.

Recovering lost animation on referenced characterSets in Maya 2011

A friend recently lost all of his animation on a referenced character rig. The file still opens without Maya complaining, but the keys just went missing. A quick check of the .ma in a text editor confirmed that the animCurve nodes were still present.

Feeling adventurous, I fired up notepad++ and did a text comparison of a working file containing the same referenced rig and the problematic file. Sure enough, the animCurve nodes seem to be connected to the wrong .phl (placeHolderList) of the reference file, or were not Continue reading