Its a nodeMCU I'm using, so disconnecting anything from the ESP is difficult.
Following some further testing tonight, I think I might have solved the problem.
It looks likely that due to the quantity of code I've written, it's having an effect on the execution of the line which attempts the broker connection. I stripped everything out, and it started working. Then I reintroduced code a section at a time - when all the code was added back, it started failing again.
So looks like it's time to try and make the code more efficient, compile it and go from there. Still a strange error though!
Thanks for all your advice.