Thanks Chris,

Not valid pointer, that was the problem and yes it was NULL.
It was just the case of changing NULL to this, and now works fine.