How To: Downgrade libcairo2 in Debian (and fix Evince)

Evince crashing with a libcairo segfault like this?

evince[3750]: segfault at 0 ip 00007f1af3de6640 sp 00007fffb9e0af38 error 4 in libcairo.so.2.11200.2[7f1af3d6e000+f4000]

It’s a libcairo2 1.12.2-2 issue – which you can thankfully fix by downgrading libcairo2 to a 1.10.2 incarnation – here’s how…

Check package availability

First check you have an alternate version to downgrade to. To do this in Synaptic select the libcairo2 package then click on the Versions tab at the bottom, or you can do the same check via a swift apt-cache policy libcairo2 as shown below:

$ apt-cache policy libcairo2
libcairo2:
  Installed: 1.12.2-2
  Candidate: 1.12.2-2
  Version table:
 *** 1.12.2-2 0
        500 http://ftp.us.debian.org/debian/ testing/main amd64 Packages
        100 /var/lib/dpkg/status
     1.10.2-2ubuntu2 0
        500 http://packages.linuxmint.com/ debian/upstream amd64 Packages

In this case the older (but functional) package 1.10-2-2ubuntu2 is available. Let’s assume there’s an older package you can use – if not, go get one from elsewhere.

Uninstall and force a different version

Uninstall libcairo2 using any manner you like – this will also force removal of things like Evince as dependencies, unless you see anything spectacular that you know you need just let it.

Then in Synaptic, search for libcairo2, select it (i.e. click on it), then from the Synaptic menu choose Package > Force Version and choose the older, working package, and install it.

Next, you’ll want to lock to that version so it doesn’t get upgraded (at least until there’s a newer working version), so again from the menu just select Package > Lock Version.

Reinstall libcairo-using things

Finally, reinstall what was ailing ya and everything should work fine. In my case at least, this means I can reinstall the Evince PDF viewer and it’ll now print to PDF without segfaulting – Huzzah! =D

I’ve no doubt you can do this entire process from the command line, but it’s easy and works through Synaptic so I’ll leave that as an exercise for the reader.

As an added bonus, if you’re experiencing slow display and/or corrupted text in Iceweasel or Chromium then the downgrade should fix that up too. You can read more about which here.

Derived class issues with arrays and casting in C++

I’m working my way through C++ FAQs book by Cline and Lomow, and it’s excellent. There’s lots of issues going on with inheritance, arrays and casting that could be a real pain to deal with towards the end of system development, but that you can nip in the bud and make life easier for yourself. For example, just knowing that an array of objects of a Derived class is NOT a kind-of array of objects of the Base class can prevent you a lot of headaches…

// Book: C++ FAQs by Cline & Lomow
// FAQ 136 & 137 - Is array-of Derived a kind-of array-of Base?
// Answer: NO!
 
#include <iostream>
using namespace std;
 
class Base
{
	protected:
		int i;
 
	public:
		Base() : i(42*42)      { }
		virtual ~Base()        { }
		virtual void service() { cout << "Base::service() called.\n" << flush; }
};
 
class Derived : public Base
{
	protected:
		// Add some extra things so that an object of type Derived is a different
		// size to an object of type Base
		int j;
		float k;
		unsigned long l;
 
	public:
		Derived() : Base(), j(42*42*42) { }
		virtual void service()          { cout << "Derived::service() called.\n" << flush; }
};
 
// Userland function
void useSubscript(Base *b)
{
	cout << "b[0].service(): " << flush; b[0].service();
	cout << "b[1].service(): " << flush; b[1].service(); // BOOM! Segfault!
 
	// This fails because the size of Base and the size of Derived are different:
	// "The fundamental problem is that a pointer to the first of an array-of-things
	// has exactly the same type as a pointer to a single thing. This was inherited
	// from C."
	//
	// In essence, as we're passing in Base pointers, in this case Base moves from
	// element to element in 16 byte intervals, but we actually provided an array
	// of Derived objects, which have a size of 32 bytes each, so b[1] starts
	// at b[0]+16, which is really only half-way through our first Derived element d[0]!
	//
	// The way to do this properly is to use a templatised container class instead.
	// If you tried to pass Array<Derived> as Array<Base> this would be caught at compile time.
}
 
int main()
{
    Derived d[10];
 
    cout << "Base has a size of   : " << sizeof(Base)    << " bytes." << endl;         // 16 bytes
    cout << "Derived has a size of: " << sizeof(Derived) << " bytes." << endl << endl; // 32 bytes
 
    useSubscript(d);
 
    return 0;
}

Good to know – now I just have to keep it in mind when I’m coding!