Jump to content
  • Advertisement


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

Red Ghost

char pointers problem

This topic is 5858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Whoa ! I should rest more often, bugs would be easier to catch.
Thanks Mats, I had overlooked the couple of delete ...

Red Ghost.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!