Seems like you got your answer (anti-virus). I've been struggling with this for creating a temp file, then deleting. Disabling AV isn't a solution so I have it run delete when the program exits :P
Also, I've been using system("pause") to do waiting ha, if the user closes the console with the X, this command reads it as "any key" and will execute the next command, which can complete before the application receives TERM. So now all my system("pause")s have a thread wait so nothing is run. MS needs to get rid of the damn file locks!