"Good luck' follows careful preparation; 'bad luck' comes from sloppiness." - Robert Heinlein   |    "The more difficulties one has to encounter, within and without, the more significant and the higher in inspiration his life will be" - Horace Bushnell   |    "There is no such thing as luck; there is only adequate or inadequate preparation to cope with a statistical universe." - Robert Heinlein   |    "The more you learn, the more you need to learn." - Robert Heinlein   |    "Every artist was first an amateur." - Ralph Waldo Emerson   |    "Nothing will ever be attempted if all possible objections must first be overcome." - Samuel Johnson   |    "The big secret in life is that there is no big secret. Whatever your goal, you can get there if you're willing to work." - Oprah Winfrey   |    "Our doubts are traitors, and make us lose the good we oft might win, by fearing to attempt." - William Shakespeare   |    "Nothing can stop the man with the right mental attitude from achieving his goal; nothing on earth can help the man with the wrong mental attitude." - Thomas Jefferson   |    "We are what we repeatedly do. Excellence, therefore, is not an act but a habit." - Aristotle   |    "Perl - The only language that looks the same before and after RSA encryption." - Keith Bostic   |    "If Java had true garbage collection, most programs would delete themselves upon execution." - Robert Sewell   |    "We are still masters of our fate. We are still captains of our souls." - Winston Churchill   |    "Nothing great was ever achieved without enthusiasm." - Ralph Waldo Emerson   |    Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.   |    "The talent of success is nothing more than doing what you can do, well." - Henry W. Longfellow   |    "Take calculated risks. That is quite different from being rash." - George S. Patton   |    "Success is the sum of small efforts, repeated day in and day out." - Robert Collier   |   

Code Reactor

The best of modern Web development

Changing size of VirtualBox disks before and after deleting snapshots

If you search for guides for changing the size of VirtualBox .vdi disks, there are plenty, and really the solution is pretty simple, just

vboxmanage.exe modifyhd "<path-to-vdi>" --resize <newsize-in-MB>

But what some of the guides fail to mention (some of them don’t fail to mention it, but since you are reading this post you probably failed to see it in the guides that did mention it) is that you really should delete all the snapshots for the disks you are trying to change the size of, BEFORE changing the size. Otherwise you’ll pretty quickly notice that the guest OS doesn’t recognize the drive size change, and now you can’t delete the last snapshot, getting instead the error with something about “virtualbox could not merge the medium VERR_INVALID_PARAMETER”.

So this post is really for you that are now stuck with a virtual machine that has a disk that cannot be size-changed and a snapshot that cannot be deleted.

Basically the solution is to clone the whole virtual machine using the standard virtualbox functions, because the cloned version will be free of any snapshots and still represent the current state of the original VM.

When you are going to clone it you will be presented with some options which are self-explanatory. But one of those options will have you choose to keep or discard all the snapshots, and you obviously have to choose discard at that point.

One small drawback of this operation is that VirtualBox does not seem to let me choose where to put the disk of the cloned machine and creates it in the default location, which can be problematic if you keep your disks somewhere else.

Now you will still have to resize the new disk. This is where another small problem arises: if you just use the command line fromt he beginning of the post on the newly created disk, you may receive the error “VBoxManage.exe: error: Cannot register the hard disk … becase a hard disk …. with UUID … already exists”. Long story short, the solution is to first run

vboxmanage.exe internalcommands sethduuid "path-to-the-new-disk"

It will assign a new uuid to the disk which will solve the problem, and you can now resize the disk with your normal command (the modifyhd one, from the beginning of the post).

Now just boot into the guest and use Gparted (if you have *nix) or Computer Management -> Storage -> Disk Management in Windows 7 (probably Vista had this already) to make the guest OS understand the new disk change.

Of course, now would be also a good time to make sure the guest OS works properly and delete the original VM in VirtualBox gui to save space.

Watch out for empty constructors

Well, this is a pretty rare caveat, but took me some time find. And it is a very subtle case of wrong assuming of things.

I will show this with an Ogre example, but the principle is the same in many other similar situations.

Ogre is a 3D engine, and naturally, has some convenience math classes like Vector2 or Vector3. Each such class has a number of constructors, both in form of ::Vector3(float x, float y, float z), but also in form of just a ::Vector3().

And at least for me, it is very easy to assume that a ::Vector3() constructor actually initializes the object to some default state, which for a vector would logically be (0,0,0). But when you think about it, it is not so certain at all… Also in my situation in release builds, these objects would be initialized to 0s anyway, even though I am sure it’s is technically a case of Undefined Behavior. In debug builds, this was not the case, and naturally, it created some nice bugs.

The moral of the story, be aware that empty constructors of such small data classes do not necessarily initialize any state of an object, especially when such classes have static members like Vector3::ZERO, which suggest that this is the default and best way to create a zero vector (like they do in Ogre).

Implementing win32-events with Boost

I’ve been trying to modify the Hydrax sources for a while. Hydrax is a great project that renders quite pretty water, but has some performance problems, the main of which being the fact that it renders the heightmap of the water using Perlin noise on the CPU, and does so synchronously. Ogre, which is the 3D-engine that Hydrax runs on, is already pretty single-threaded, which works out relatively well, but not when Hydrax adds the noise generation to the already bloated singlethreaded CPU-part of the Ogre render cycle.

So the basic idea was to take the heightmap-generation and put into another thread which will execute on another core while the main thread does the rest, including waiting for the GPU to render things. (Yep, that’s just how single-threaded a stock Ogre application is.)

Hydrax stores the height-map in a handy blob of vertex positions (also colloquially known as the vertex buffer), which is then just fed into the mesh class in it’s entirety every frame. This gave me a nice practical possibility to just have a secondary vertex buffer which would be updated in the background, so that the primary one will be free for usage by the mesh in the main thread (I didn’t have any control over how it’s used, nor wanted to dig into the details of that), and then the only place I would need to synchronize the threads would be the point of switching the primary vertex buffer to the secondary buffer which has been generated/updated in the background.
Read the rest of this entry »

SQLite in Groovy crash course

When creating a small Groovy project lately, I required a good data storage, and plain old text files seemed just too damn inflexible. Yet the project was very small and command-line based, and was supposed to be portable, so installing a full database engine seemed even more inflexible. It was great to find out that using SQLite in Groovy can really be perhaps even more simple than text files.

So here is a basic crash course, the code is so simple and plain that it speaks for itself:

First you will need the SQLite driver. The latest library for this I could find was the Xerial SQLiteJDBC. Based on my browsing around, this is the one that should be used nowadays (end of 2012) for SQLite in Java. But the usage is very simple: just download the .jar (mine was called sqlite-jdbc-3.7.2.jar) and add it to the project.

This .jar somehow magically contains native libraries for Windows, Mac OS X, Linux, and automatically knows when to use each, to get the best performance. If you are on another platform, it will use the pure Java implementation.

You can add the .jar to your project in any way you want, but I’ve found that for a cli groovy script, the easiest way is just to put it in the same folder as the script and add this in the beginning of the code:

    new File("sqlite-jdbc-3.7.2.jar").toURL())

That’s it, you are ready to use the SQLite features:


import groovy.sql.Sql
def sql = Sql.newInstance( 'jdbc:sqlite:databasefile.sqlite', 
  'org.sqlite.JDBC' )

Replace the “databasefile.sqlite” with whatever name you want.

Check if a table exists:

def metadata = sql.connection.getMetaData()
def tables = metadata.getTables(null, null, "tablename", null)
if (!tables.next()) {
    // table does not exist
} else {
    // table exists.

Run SQL:

sql.execute("CREATE TABLE  .......  ")

Select data:

sql.rows("select * from .....").each{

Basically just the same syntax as the rest of SQL operations in Groovy.

Remember to backup :)

Drop-in fix for ‘placeholder’ attribute of input tags in Internet Explorer (even IE9)

I’ve been using this drop-in replacement for adding the placeholder support for some time now, but now I noticed that it doesn’t work as expected in IE9. What is worse is that it creates problems: while the placeholder functionality works as advertised, it touches inputs that have no placeholder attribute set and creates all sort of havoc. (In my case users could not log in because the script removed the username from forms just because that input did not have it’s placeholder attribute set.)

Nevertheless, here is the revised version that does work as expected even in IE9: (still requires jQuery)

jQuery(function() {
	jQuery.support.placeholder = false;
	test = document.createElement('input');
	if('placeholder' in test) jQuery.support.placeholder = true;
$(function() {
	if(!$.support.placeholder) { 
		var active = document.activeElement;
		$(':text').focus(function () {
			if (($(this).attr('placeholder') != "") 
			&& ($(this).val() == $(this).attr('placeholder'))) {
		}).blur(function () {
			if (($(this).attr('placeholder') != "") 
			&& (typeof ($(this).attr('placeholder')) == 'string') 
			&& ($(this).val() == "" || $(this).val() == $(this).attr('placeholder'))) {
		$('form').submit(function () {
			$(this).find('.hasPlaceholder').each(function() { $(this).val(''); });

Ninite: aptitude install for windows

While there are arguably both advantages and disadvantages with the debian package system (or any linux package system for that matter), it is certainly usefull to be able to “aptitude install whatever” by basically only having to know the name of the program you wish to install.

I don’t think that windows will ever have quite as big library of automatically installed programs, but Ninite certainly does create that feeling, as it is one of most successful and user-friendly attempts to do automatic installing in Windows.

It is an automatic and customizable installer of mostly used programs for windows. After configuration, it will automatically download latest versions of the software such as Skype, Open Office, Mozilla software, other browsers, messengers and such and such, automatically click away all adware/toolbars. It is mostly useful on new installations, but it can also keep your usual software up to date. It is of course completely free, definitely worth a try if you haven’t already!

All the Visual Studio color themes you will ever need

Combining two great concepts in programming world – color themes and the StackOverflow engine, http://studiostyl.es gives you a one-stop place for all the nice Visual Studio color themes. No more searching “the ogl” for themes and typing them in manually into the settings window.

All the themes can be downloaded for all the latest versions of VS, however it is still not particularly user friendly to install them, at least not in VS2008. I hope their coders are at least a little bit ashamed, but hey, it’s not like this is the primary point of VS.

In VS2008, you just download the file, and in Options > Environment > Import And Export Settings point the “team settings file” to the file you’ve downloaded. If you already had a team setting file, I can’t really help you, but the settings are just an XML so you could easily migrate them into your personal settings file using your favorite text editor.

(Update: found it, it is much simpler to install those using Tools > Import And Export Settings.)

(Yet Another Update: here is where I took my theme needs before the studiostyl.es came along: http://www.hanselman.com/blog/VisualStudioProgrammerThemesGallery.aspx)

Some MS-lovin

Aah, good old MS api class for representing the “system enclosure”, ie, what type of computer you have. Except:

Low Profile Desktop
Pizza Box
Mini Tower
Hand Held
Docking Station
All in One
Sub Notebook
Lunch Box
Main System Chassis
Expansion Chassis
SubChassis …

Pizza Box!? Lunch box!?

“Missing good old 80es when you ran dos on a pizza box…

(as seen on http://msdn.microsoft.com/en-us/library/Aa394474 and http://stackoverflow.com/questions/2837152/how-can-i-tell-if-a-user-is-using-a-laptop

Hardcore debugging crashes in C++ release builds in Visual Studio and StackWalker

Ever encountered an error which just shows up in a release build? And when you are debugging everything is just fine? Don’t those errors just make you happy and loving the programming, computers, the world and everything in it? 😛

So, long story short, I had to come up with some reliable debugging methods for the release builds, and there are quite a few.
Read the rest of this entry »

Your connection appears to be too slow, automatically disabling HeavyAjax (TM) for better performance...
You seem to run a browser without JavaScript support or it has been disabled. To fully experience Code Reactor please enable JavaScript. (It is not 1995 anymore :)
You seem to be using Internet Explorer. If you want to experience both Code Reactor and the rest of the web to their fullest and fastest, you are advised to download and install a real browser, like Opera, Firefox or Google Chrome.
You seem not only to use Internet Explorer, which is by far a joke when it comes to browsers, but to even use an old version of it!
If you want to experience both Code Reactor and the rest of the web to their fullest and fastest, you are STRONGLY advised to download and install a real browser, like Opera, Firefox or Google Chrome.