I know this will be a frustrating answer for you, but from what I can see, this is (Most likely) going to be a configuration problem that you are having. A problem that is going to be very hard for me to pinpoint without seeing your library setup.
The insert_items() code you have posted is fine - this is not where your problem lies.
What I do when in your situation is to reverse engineer the demo (Which you suggest IS working for you).
Take one of the working examples from the demo, and piece by piece, start changing the code to match the codebase you're aiming to create. Keep testing everytime you remove or add code until something causes the error you are seeing - then you will have found your problem.
If your still having trouble you could send me your current setup zipped and I'll try and have a look at it when I get time.