Take the 2-minute tour ×

I have a program where i ask the user for the name of a text file, I open the text file do stuff with it (read, write), then I close the file and exit the program.


class Program
     char* fileName;
     void ReadFile(void);



     cout << "in destructor" ;
     delete []fileName;

void Program::ReadFile(void){
     fileName = new char[40];

     cout <<"Please enter the name of the file to open: ";
     cin.getline(fileName, 40);

     ifstream file (fileName);

          //do stuff

right now when i put "delete []fileName;" in the destructor it outputs ""in destructor" on the screen but fileName does not get delete. If i take "delete []fileName;" and put it in ReadFile() after file.close() fileName gets delete. Why is that?

The rest of my program works perfectly which is why none of that code is pasted. I am just trying to rid any memory leaks and fileName is the only one I am having trouble with so therefore I only pasted the code where fileName is used.

Any help is appreciated. Thanks in advanced.

Additional information: I am using Visual Studio to write this and am using the Memory Leak Detection. This is what it outputs

Detected memory leaks! Dumping objects -> {132} normal block at 0x005D49A0, 40 bytes long. Data: 6E 61 6D 65 73 2E 74 78 74 00 CD CD CD CD CD CD Object dump complete. The program '[10772] program1.exe: Native' has exited with code 0 (0x0).

which is why I suspect delete []fileName; didn't work.

also, this is what the int main() looks like

int main(){
     Program abc;

oh and Program.h can not be changed. only .cpp can be change it's part of my requirements.

share|improve this question
Why not use std::string? –  chris Oct 20 '12 at 23:30
not allowed to >_>, i have to use a char array –  livelaughlove Oct 20 '12 at 23:31
How do you know fileName isn't being deleted? –  nneonneo Oct 20 '12 at 23:35
how did you check that filename is not deleted in destructor? –  PiotrNycz Oct 20 '12 at 23:35
This is pretty sick. –  Kerrek SB Oct 20 '12 at 23:36
show 3 more comments

3 Answers

up vote 1 down vote accepted

Are you sure your main is exactly as posted here? If you just define abc globally, it will be freed after memory dump report memory leaks and you may see invalid reports! You can insert a break point in the destructor and see if memory leaks reported after or before destructor

share|improve this answer
ty! exactly what the problem was. it did get free cept the leak msg printed before entering the destructor –  livelaughlove Oct 21 '12 at 0:40
add comment

If filename is used only in readFile - then I advise you to remove it from Program class and make it automatic variable in that function:

void Program::ReadFile(void){
     char fileName[40];
     // no delete [] necessary

Your problem might be related to

  1. Not initializing filename to nullptr in constructor
  2. Not defining copy c-tor, assignment operator
  3. Your are not deleting old filename in readFile

So, do not use your member variables as automatic variables to your methods.

If you have to have this member varible - change it to array - do not allocate it:

class Program {
  // char* filename;
  char filename[40];


Your .h file is incorrect - it breaks rule of three (see http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)) - copy constructor and assignment operator is missing. So be aware not to copy your Program class in any way of you cannot change this header.

After update only one thing is missing in your program:

Either add delete[] filename at the beginning of your readFile:

void Program::ReadFile(void){
     delete [] filename;
     fileName = new char[40];

Or (better) - do not re-allocate every time readFile is called:

void Program::ReadFile(void){
     if (!filename)
         fileName = new char[40];

Or (best) - allocate this memory in constructor only:

Program::Program() : filename(new char[40]) {}
void Program::ReadFile(void){
   // fileName = new char[40];
share|improve this answer
i have to use it as a member variable cause it was part of my requirements. but gonna double check the null thing. i have it initialized to NULL atm, going to see if nullptr will make a difference. –  livelaughlove Oct 20 '12 at 23:45
@livelaughlove Then see list of current problems in your code. Start from point 3, then 1, then 2 –  PiotrNycz Oct 20 '12 at 23:47
@livelaughlove or see my update - just do not do dynamic allocation - if it is not absolutely necessary... –  PiotrNycz Oct 20 '12 at 23:50
tried the nullptr, it gave the same memory leak msg. there is no old fileName because the user only inputs the fileName once in the entire program. so fileName=NULL, in the beginning, then fileName="user input" and that's it. not sure about 2. and fileName is a char pointer. –  livelaughlove Oct 20 '12 at 23:54
i added some more additional information >.< –  livelaughlove Oct 20 '12 at 23:56
show 2 more comments

fileName clearly is getting deleted: the code in the destructor says so. But if the code you didn't show calls ReadFile more than once, the class will leak memory because each call to ReadFile allocates a new memory block and overwrites the pointer to the previous block.

Think RAII: Resource Allocation Is Initialization. In the constructor, allocate the memory block. In the destructor, delete it. Then ReadFile doesn't have to worry about allocating the block.

Or, even better, do as @PiotrNycz says, and change the pointer into an array. No need for dynamic allocation.

share|improve this answer
i cannot change the header file because it is the requirements I have to follow. which is why fileName cannot be a char array, but a char pointer. and ReadFile() is called only once in the entire program. and fileName is only ever used in ReadFile(). –  livelaughlove Oct 20 '12 at 23:59
oh and fileName is not getting deleted for sure. because when I take delete []fileName; out of the destructor and put it at the end of ReadFile() after file.close() i don't get any memory leak msg. –  livelaughlove Oct 21 '12 at 0:01
@livelaughlove - you've just logically proved that your problem does not exist. So you have to figure out which of your premises is wrong. Sorry, but handwaving assertions won't get you any meaningful help. Post real code, the smallest example you can put together that compiles, runs, and demonstrates the problem. –  Pete Becker Oct 21 '12 at 0:02
the only code I didn't post is what I do with the file. but that has nothing to do with the fileName. the fileName is only used to store what the user inputs and use that to open the file. like i said the rest of the program works fine and I am just trying to get rid of memory leaks. when delete []fileName; is in destructor, I get a memory leak msg. when delete []fileName; is at the end of ReadFile() I get no memory leak msg. that is what I don't understand. –  livelaughlove Oct 21 '12 at 0:07
Don't know who downvoted this; do you disapprove of RAII? –  Pete Becker Oct 21 '12 at 1:52
add comment

Your Answer


