Do you have a BG-E2 grip on the camera? If so, it's a common problem. Over time the grip loosens. Some of the early ones needed a fairly simple modification to keep them in contact. Canon had a white paper on their website with the serial numbers involved and a how-to description of the repair. There were also websites that showed this. It occurs mostly with original BG-E2, and the early ones at that, whether used on 20D, 30D, 40D or 50D.
However, even though Canon improved the fit and that helped a lot, I've seen it occur with later BG-E2 and even BG-E2N on various models of camera when they simply get a little loose.
It is possible that the microswitches under the battery or memory card doors are causing problems. Those are there to turn off the camera when the door is opened. Sometimes a little dirt in there will make the switch stick. But usually it happens right after opening one or the other of those doors. And it's pretty rare.
It also can be the main power switch of the camera itself. Do you leave it on all the time, and just let the camera go into sleep mode? Those switches sometimes get corrosion in them if not used for a long time, just left in one position or another. But it's self-cleaning, so turning it on and off a few times might clear the problem.
You might try cleaning the battery/camera contacts. Put a few drops of isopropyl alcohol (standard "rubbing" alcohol) on a clean cloth, then wipe the contacts on both the battery and corresponding ones inside the camera or inside the battery drip. If you have a battery grip on the camera, also remove it and clean the contacts on the grip and the corresponding ones up inside the camera. You might need to wrap the cloth over the end of something to reach the contacts up inside the camera (wooden coffee stir sticks from Starbucks work... or similar).
A new main battery might be needed. Eventually they wear out and can make the camera act strangely. It's not so much the age of the battery, as it is the number of charge/drain/recharge cycles it's been through. I seem to recall that typical BP511/511A are good for about 400 recharges, then might be expected to lose capacity and have trouble taking a charge.
It's probably not related, but the small "memory" battery might be due for replacement too. Usually it just effects date/time, and some menu/custom function settings might be lost... but who knows. It's cheap and easy to replace. It's a CR2016 lithium battery, that cost $2 or $3 at local stores, and fits into a little drawer alongside the recharceable battery. If yours has never been replaced, or it's been 4 or 5 years, it might be due! Wipe it off and try to keep finger prints off it, when you reinsert the battery.
You also can force a "reboot" of the camera, sort of like rebooting a computer to clear the memory, by removing all the batteries, including that memory battery, turning the camera on and pressing the shutter release button once. Though it won't fire the shutter without any batteries in the cameras, this should drain all power and the camera should reset and memory should clear. Turn the camera off, put the batteries back in, turn the camera back on and check it the date/time need to be reset. If so, you got a proper reboot. If not, you might try simply taking the batteries out and leaving them out for a minimum of an hour, maybe overnight. Then repeat the process of checking the date/time.
You also might reinstall the firmware, even if it's up to date.
Other than that, it might be a more serious issue such as an electronic component overheating and shutting down, or a loose connection inside the camera.