Type Hiding With “Template Impl” Pattern
I recently encountered an design requirement for a library which required relatively fast handling of certain callbacks. I wanted, if possible, to avoid any vtable lookups introduced by virtual function calls. The scenario goes like this:
There exists a class called Connection. Connection will receive data from the network and handle certain message events (it is responsible for parsing and getting these messages from the network somehow). On certain messages, it relies on a delegate object to act as a functor or handler it can invoke callbacks on. These callbacks will handle and future processing of handling of the messages. Also, since these methods get called frequently, we want these calls to be as fast as possible, i.e. avoid function calls. The question is, can we create Connection such that he invokes callbacks without vtable lookups and such that we can create multiple instances of him and store them all in a common Connection collection? The answer is yes, with a little bit of ingenuity.
The trick lies in using Pimpl idiom and some templating. Here is an example:
Connection.h
class Connection {
public:
template<typename T> Connection(const T & callbackobj);
private:
class Impl;
Impl * impl_;
};
Connection.cpp
#include "Connection.h"
class Connection::Impl {
};
template <typename T>
class NotifyingImpl : public Connection::Impl {
public:
NotifyingImpl(const T & cbobj) : cb_(cbobj) {}
void handle_message(const T & m) { cb_(m); }
private:
const T & cb_;
};
template<typename T> Connection::Connection(const T & cbobj) : impl_(new NotifyingImpl<T>(cbobj)) {}
What we’ve done here is taken advantage of the implementations polymorphism to allow the storage of internal templates in a common template-free container. So long as the calls and behavior of NotifyingImpl
This model is useful since it separates having to statically know the handler type or define a virtual interface, so we can still perform testing cleanly.
Note: you’ll have to reference included the Templated Impl in the header or include the cpp file when you build, since all template definitions need to be known before you build object files (remember the compiler is creating code for you).
I find this method useful when the implementation needs to handle callbacks quickly and you don’t need to expose this in the interface or need to store multiple implementations in a single stl container.
[Boost.Python] Calling back into python from native C++ threads
So I came across a particularly frustrating problem dealing with a boost python wrapper around a library which is calling python functions from native c++ threads (boost threads in this case). The problem was that some calls to release the global interpreter lock would cause python to through fatal errors and segmentation fault, despite seeming to acquire the GIL properly. This is how I solved the problem.
As a quick overview, you need to make sure that any native threads acquire the GIL (global interpreter lock) before any sort of python resources are touched. You may not use any C Python API functions or call into python until the GIL is acquired. A good practice is to follow the RIAA method for acquiring the GIL like so:
class ScopedGILRelease
{
public:
inline ScopedGILRelease()
{
d_gstate = PyGILState_Ensure();
}
inline ~ScopedGILRelease()
{
PyGILState_Release(d_gstate);
}
private:
PyGILState_STATE d_gstate;
};
void onData(const boost::shared_ptr<Data>::P & data, const void * closure)
{
ScopedGILRelease gil_lock;
// invoke call_method to python
}
In the above example we call the lock which invokes PyGILState_Ensure() to acquire the lock. Additionally we call PyGILState_Release() on the same thread state object when we go out of scope.
What bit me when developing this solution was that the api was not being properly initialized. Originally I was calling two functions in my one of my modules objects which I assumed was going to be called before any threads were made.
PyEval_InitThreads(); PyEval_ReleaseLock();
Now according to the python documentation, the first thread should:
Initialize and acquire the global interpreter lock. It should be called in the main thread before creating a second thread or engaging in any other thread operations such as PyEval_ReleaseLock() or PyEval_ReleaseThread(tstate). It is not needed before calling PyEval_SaveThread() or PyEval_RestoreThread().
However, that documentation is either incorrect or something very subtle was going on in my library, since PyEval_InitThreads() was being called before any thread operations and the object was being created in the main thread of the python interpreter. Perhaps I couldn’t guarantee that python wasn’t spawning some sub-thread to handle that part of the script. Anyway I found that I had to instead call PyEval_InitThreads() once in the module definition macro for boost python. Looking something like this:
BOOST_PYTHON_MODULE(ModuleName)
{
PyEval_InitThreads();
...
}
As always a super simple solution to a super frustrating problem. This information was extremely hard to find, so hopefully this helps someone out there from losing hours of time. Also, here is a link to the stack overflow question I eventually posted and quickly subsequently answered.
Enjoy.
Implementing jQuery tablesorter natural sort plugin as text sort
I have been doing quite a bit of web programming lately and was recently trying to figure out how to get the jquery plugin ‘tablesorter‘ to perform a natural sort.
For those not aware of what natural sort is, it is the way in which human beings tend to sort things intuitively (hence the prefix ‘natural’). In fact, coding horror as a pretty good article about natural sort and how tough it can be. To cut the story short, it ensures that word19 comes after word2 in a sorted list (a typical sort would place word2 after, since the ascii characters in the 5th position define the sort order).
Now, normally implementing a natural sort is not so difficult depending on the language, you basically split up the areas of the string that represent digits, and then sort each substring piece in order (string order if two strings, numeric order if two digits). Unfortunately, the javascript tablesorter plugin allows you to sort by providing a function that takes an element to be sorted and transforms it into a different string or number that must be sorted EITHER numerical OR textually, but not both.
Looking around online didn’t yield any good solutions aside from, “implement a jquery plugin”. So I got to wondering what how it could be done. These plugins could take the either table or cell as an argument but then you’d have to remain stateless and re-perform the entire natural sort over again. It may be possible to also use the jquery data api to store data about a table to be sorted and then fetch it on each call (performing the natural sort once and assigning ids to each cell or something).
It got me wondering about formulating a natural sorting algorithm as a numeric or textual sort. So I tried to prove if it could be done. I tend to get carried away and engrossed by certain little challenges like this:
1.) Natural Sort As Numeric Sort
This should become clear shortly that natural sort cannot be easily implemented as a stateless numerical sort. Since you are ultimately converting the string to a number, you can simply make the string longer to produce a bigger number and mess up the sort order.
2.) Natural Sort As Textual Sort
This seemed like it could work. A textual sort is just a sequential comparison of ascii coded bytes, each with a numeric value. Natural sort is also a set of sequential comparison operations that ultimately break down to numeric comparisons. One simply needs to convert the numeric pieces into a string of characters preserving the same order as the numeric sort, then create a new string with these numeric characters replaced.
I decided to follow the original idea of ascii code comparisons, and I decided to convert each number to a fixed length string in which each digit is a base 127 or 256 number. Why 127 or 256? They represent standard and extended ascii code length and ensure that the values always fall without the proper range. String needs to be fixed length so that all numerals are compared equally (longer numbers won’t be incorrectly compared against smaller numbers). The length of your string determines the maximum size of any numeric segment. That means that using a N digit base 127 string gives you 127^(N-1) numbers. At 10 digits, thats 3×10^18 numbers. If you use base 256 you get 21 decimal places. Technically you can go even higher with UTF8.
Anyway, here is my solution (excuse my probably ugly javascript, truthfully its one of the first javascript functions I’ve written from scratch).
ts.addParser({
id: "natural",
is: function(s) {
return false;
},
format: function(s) {
var ret = "";
var num_str = "";
var base_num = 256;
var fixed_len = 10;
var num_to_ascii_len_str = function (base, max_len, num)
{
var tmp = num;
var ret = "";
for (var i = 0; i < max_len; i++)
{
if (tmp > base)
{
var div = tmp / base;
tmp = tmp - div * base;
ret = String.fromCharCode(div) + ret;
}
else
{
ret = String.fromCharCode(tmp) + ret;
tmp = 0;
}
}
return ret;
};
for(var i = 0; i < s.length; i++)
{
code = s.charCodeAt(i);
if (code > 47 && code < 58) {
num_str = num_str + s.charAt(i);
}
else{
if (num_str.length > 0)
{
ret = ret + num_to_ascii_len_str(base_num, fixed_len, parseInt(num_str));
num_str = "";
}
else
{
ret = ret + s.charAt(i);
}
}
}
if (num_str.length > 0)
{
ret = ret + num_to_ascii_len_str(base_num, fixed_len, parseInt(num_str));
}
return ret;
},
type: "text"
});
Hope this helps someone out there, as this kind of thing is useful but not easily found. Oh and if anyone can figure out if its possible for formulate this as a numeric sort or knows a better way to do this in the context of the tablesorter plugin, please share!
How To Fix iOS Memory Leaks Coming From UIKit
It looks as though I’m going to be posting a number of troubleshooting iOS SDK and objective-c guides in the coming weeks. The first deals with memory leaks coming from UIKit and QuartzCore.
If you are using the leaks tool included with Instruments and XCode, you might notice leaks. This is a pretty amazing tool that will tell you when you leak, how much and where the allocations occurred.
In instruments, you can select View -> Extended Detail. This will provide you with the stack trace. You can then see where the leak originated. If you see errors coming straight from main, or from a call to pushViewController, you likely had the same problem I had — you are not cleaning up your View or ViewController.
To be safe in memory management, be careful to do the following:
- In viewDidUnload:, you should set all your retain and IBOutlet properties to nil like so:
- Secondly, and very important, you should release and set all the IBOutlet backing properties to nil, like so:
- Make sure you have no references to parent views or controllers in the leaking view controller.
self.myProperty = nil;
[myProperty release], myProperty = nil;
This may seem totally counterintuitive since retain properties should properly free all held memory when nil is set, but actually the second step is required when dealing with UI components, otherwise it will result it leaks that appear to originate from UIKit or other Apple libraries. Be very careful that all IBOutlets are set to nil.
iOS EXC_BAD_ACCESS error when navigating to/from nib
As I have begun the trek towards mastering objective-c, I’ve come to appreciate the memory management nuances. I came across a problem where when navigating backwards in a table view, the application would just crash with an EXC_BAD_ACCESS error. If you see this it is most likely due to accidently mis-managing memory. I fixed my particular error by referring to the memory management rules with NIBs. Because of the way nibs are access you need to unload IBOutlet properties in the viewDidUnload method. Here are examples from apple’s documentation:
- (void)viewDidUnload {
self.anOutlet = nil;
[super viewDidUnload];
}
Also, for some reason you also have to release them in the dealloc for the controller you are in:
- (void)dealloc {
// Release outlets and set outlet variables to nil.
[anOutlet release], anOutlet = nil;
[super dealloc];
}
Hope this helps someone out there. Here are the details of how to manage memory in NIBS for Mac OS or iOS.

Recent Comments