Archived

This topic is now archived and is closed to further replies.

Red Ghost

char pointers problem

Recommended Posts

Red Ghost    368
Hi all, I have developped a token system to separate the parsing of initialization files from the actual initialization of my app: my app reads up a file entry and outputs a token, then this token is used to initialize the function. The advantage of this system is that I can use one parsing library to load/save plain text tokens while profiling my app, and when releasing my app use another parsing library to load/save compiled tokens. On with my problem: My program crashes because of a memory leak within that system. I have checked many times all the functions and analyzed my debug file, I cannot tell exactly where it is. I only know that it is closely linked to the data members of my token struct. Here is a short version of my Token struct:
  
#define NB_TEXT 2

struct Token {
   char* text[NB_TEXT];
   Token* n_token;
};
  
When I get a new token, I set each Token.text variable to NULL.
  
   Token* My_Token;

   My_Token = new Token;
   for(int i=0; i<NB_TEXT; i++)
      My_Token->text[i] = NULL;
  
When parsing a line (stored in my Buffer variable) in my file using the input string stream library, I do:
  
   istrstream ins(Buffer, strlen(Buffer));

   My_Token->text[0] = new char [strlen(Buffer)-1];
   ins >> My_Token->text[0];
  
This token is then passed to my function for initialization and deleted afterwards:
  
   char* Caption;

   Caption = new char[strlen(my_token->text[0])];
   strcpy(Caption, my_token->text[0]);

/////////////////////////////////////////

   for(int i=0; i<NB_TEXT; i++)
      if (My_Token->text[i]!=NULL)
         delete My_Token->text[i];

   delete My_Token;
  
When the function is not used anymore, it is deleted in memory:
  
   delete Caption;
  
My program crashes just after deleting my token on the 4th or 5th use of that function. The log files show that the function is correctly initialized, and that the initilization file is correctly parsed. The structure seems correctly deleted. I also that before using that code, the program was perfectly running (I was not using tokens, the parser was directly initializing the function instead of creating a token). I am running out of ideas for the moment. Does anyone have an idea ? Thanks in advance. Red Ghost.

Share this post


Link to post
Share on other sites