I have found this problem in my programs a lot, so I will post it here to hear your opinions. Currently Im having a class that handles files (CFileManager). What it does is: Open file, alloc memory for file buffer, return buffer. (this buffer will be used by other derived classes to, for example, handle images or sounds).
The problem, as you might guess is the allocation of memory. If this class allocates memory, some other class is responsible and must deallocate it. I found this horrible to mantain, and its an easy thing to forget.
I will tell you the solutions I have in my mind, and tell me what do you think:
1) Do not let the file manager class to allocate memory, pass a buffer with memory already allocated. This has the drawback that you need to know the size of the file previously.
2) Create a CFileBuffer class with two functions: AllocMem and DeleteMem.
Thx in advance. All comments are welcome.