944,068 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 9921
  • C++ RSS
May 22nd, 2007
1

Heap block at 003D2CA0 modified at 003D2CB3 past requested size of b

Expand Post »
(I have 2 queries)

I have made a html parser but the memory leaks, as it seems from the message below; is killing me. I am getting this message from gdb debugger (cygwin build).

Heap block at 003D2CA0 modified at 003D2CB3 past requested size of b


Also now not all the output from cout and printf are getting printed. I am posted the code of main() function where all the pointer acrobatics takes place. The class Parser does the parsing. It can parse from a file or from a character array. It uses an object (buf) of vector<char> to store the output and dynamically instructs vector to resizes when needed using the code...

c++ Syntax (Toggle Plain Text)
  1. if(bufptr>=buf.size()) buf.resize(buf.size()+INITIAL_BUF_SIZE,'\0');
  2. buf[bufptr++]=c;

Query 1: What is the difference between vector's resize and reserve?

Quer 2: Do u see any possible memory leaks in the badly hacked code below?
c++ Syntax (Toggle Plain Text)
  1. int main(){
  2. Parser p;
  3. Tag tag;
  4. strcpy(tag.name,"div");
  5.  
  6. Property *px=new Property[2];
  7. strcpy(px[0].name,"id");
  8. strcpy(px[0].value,"res");
  9. tag.totalProperties=1;
  10. tag.property=px;
  11.  
  12. bool err;
  13. char *out=p.parse("g-ogre.htm",tag,0,NULL,0,err,true);
  14. //cout<<"div:: "<<out<<endl;
  15.  
  16.  
  17. strcpy(tag.name,"p");
  18. tag.totalProperties=0;
  19.  
  20. char *tout=p.parse(out,tag,1,NULL,0,err,false);
  21. //cout<<tout<<endl;
  22.  
  23. strcpy(tag.name,"a");
  24. char *rp=new char[300], *trp=NULL;
  25. rp[0]='\0';
  26. int i=1;
  27. do{
  28. if(trp) {delete [] trp; trp=NULL;}
  29. trp=p.parse(tout,tag,i++,NULL,0,err,false);
  30. strcat(rp,"; ");
  31. strcat(rp,trp);
  32. }while(strLen(trp)!=0);
  33. if(trp) {delete [] trp; trp=NULL;}
  34. if(tout) {delete [] tout; tout=NULL;}
  35. //cout<<"Related phrases::-\n"<<rp<<endl;
  36.  
  37. strcpy(tag.name,"ul");
  38. strcpy(px[0].name,"type");
  39. strcpy(px[0].value,"disc");
  40. tag.totalProperties=1;
  41.  
  42. Tag itag;//tags to ignore.
  43. strcpy(itag.name,"br");
  44. //strcpy(itag[0].name,"a");
  45.  
  46. tout=p.parse(out,tag,1,&itag,1,err);
  47. //cout<<"ul :: "<<tout<<endl;
  48.  
  49. tag.totalProperties=0;
  50. char *lp=new (nothrow) char[10000];
  51. if(lp==NULL){
  52. cout<<"Out of Memory!!!"<<endl;
  53. return 0;
  54. }
  55. lp[0]='\0';
  56. lp[9999]='@';
  57. i=1;
  58. trp=NULL;
  59. strcpy(itag.name,"a");
  60. do{
  61. if(trp) {delete [] trp; trp=NULL;}
  62. strcpy(tag.name,"font");
  63. trp=p.parse(tout,tag,i++,NULL,0,err);
  64. if(strLen(trp)==0) break;
  65. strcat(lp,"\n\n:");
  66. char *qp=NULL;
  67. strcpy(tag.name,"li");
  68. int j=1;
  69. do{
  70. if(qp) {delete [] qp; qp=NULL;}
  71. qp=p.parse(trp,tag,1,&itag,1,err);//The content of li ignoring the tag <a> (this contains the source url, will be extracted in following lines).
  72. cout<<">>>>>>>>>>>>>QP"<<qp<<endl;
  73. strcat(lp,"\n");
  74. strcat(lp,qp);
  75. }while(strLen(qp)!=0);
  76. if(qp) {delete [] qp; qp=NULL;}
  77. /*
  78.   strcpy(tag.name,"a");
  79.   char *krp=p.parse(trp,tag,1,NULL,0,err);//Extracting content of <a> for getting source url, but contains font tag too that encloses the data required.
  80.   strcpy(tag.name,"font");
  81.   char *ttrp=p.parse(krp,tag,1,NULL,0,err);//From the content extracted in the line above getting the content of <Font>. This effectively removes the font tag.
  82.   strcat(lp,"\nSource Url: ");
  83.   strcat(lp,ttrp);
  84.   delete [] krp; krp=NULL;
  85.   delete [] ttrp; ttrp=NULL;
  86.   delete [] trp; trp=NULL;*/
  87. //strcat(lp,trp);
  88. }while(1);
  89. if(trp) {delete [] trp; trp=NULL;}
  90. if(lp[4999]!='@')
  91. printf("lp overflowed!!!\n");
  92. else
  93. printf("Definitions:-\n%s\n",lp);
  94. printf("End of program.");
  95. //cout<<err;
  96. return 0;
  97. }
Similar Threads
Reputation Points: 25
Solved Threads: 0
Newbie Poster
applegrew is offline Offline
5 posts
since May 2007
May 22nd, 2007
0

Re: Heap block at 003D2CA0 modified at 003D2CB3 past requested size of b

Q2: Yes -- there are lots of places that look like memory leeks --
line 24: where is that object deleted?
lines 13, 20, and 63: does parse() return a char pointer that needs to be deleted? If it does, then these lines are memory leeks too.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,955 posts
since Aug 2005
May 22nd, 2007
0

Re: Heap block at 003D2CA0 modified at 003D2CB3 past requested size of b

Q2: Yes -- there are lots of places that look like memory leeks --
line 24: where is that object deleted?
lines 13, 20, and 63: does parse() return a char pointer that needs to be deleted? If it does, then these lines are memory leeks too.
parse returns pointer to dynamically allocated character array using the 'new' operator. As for lines 24,etc. They have not been deleted because they will be used to print the result (later, when debug the code).

Anyway I found the source of the warning from gdb. It is is giving that warning whenever I use the 'delete' operator. I found this after painfully tracing the main() function. e.g. the code

if(out) {delete [] out; out=NULL;}

which I later inserted at line no. 22 gave this warning from gdb
C++ Syntax (Toggle Plain Text)
  1. warning: Heap block at 003D4FB8 modified at 003D647D past requested size of 14bd
Though I confirmed that 'out' did contain valid data at that moment and the starting address of the block it was pointing to was had the address 0x3d4fc0. I am totally confused why is this happening? Furthermore if I comment out the lines with the 'delete' instructions then I get no warning and while stepping through the code I also get the correct code, but as soon as I run this program directly from the command prompt then it crashes with Windows giving me the message
C++ Syntax (Toggle Plain Text)
  1. The instruction at xyz location referenced memory at abc location. The memory could not be read.
Pls help. I am at my wits end.
Last edited by applegrew; May 22nd, 2007 at 6:36 pm.
Reputation Points: 25
Solved Threads: 0
Newbie Poster
applegrew is offline Offline
5 posts
since May 2007
May 22nd, 2007
0

Re: Heap block at 003D2CA0 modified at 003D2CB3 past requested size of b

does parse() modify the contents of the char array in the first argument to the function ? If it does, then maybe the parse function is overrunning that buffer.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,955 posts
since Aug 2005
May 23rd, 2007
0

Re: Heap block at 003D2CA0 modified at 003D2CB3 past requested size of b

does parse() modify the contents of the char array in the first argument to the function ? If it does, then maybe the parse function is overrunning that buffer.
Nope. I fact the 1st argument is 'const char *'.

Prototype of classes:-
c++ Syntax (Toggle Plain Text)
  1. struct Property{
  2. char name[MAX_NAME_SIZE];
  3. char value[PROP_VAL_SIZE];
  4. bool getValue; //Set this to true if value of 'value' is unknown and is needed by the calling function.
  5. read in 'name' and then in value (if set).
  6. Property(){
  7. name[0]='\0';
  8. value[0]='\0';
  9. getValue=false;
  10. }
  11. };
  12.  
  13. struct Tag{
  14. char name[MAX_NAME_SIZE];
  15. Property *property;
  16. int totalProperties;//Size of array pointed to by *property.
  17. int dc;//for internal use.
  18. Tag(){
  19. name[0]='\0';
  20. property=NULL;
  21. totalProperties=0;
  22. dc=0;
  23. }
  24. };
  25. //-------------------------------CLASS------------------
  26. class Parser{
  27. private:
  28. ifstream *f;
  29. const char *src;//Only one of the two would be used at a time.
  30. long srcSize;
  31. bool eof;
  32. static const char singleton[NO_OF_SINGLETONS][MAX_NAME_SIZE];
  33.  
  34. char get(long &pos);//Byte offset when in files and index for arrays.
  35. char* parse(Tag tag, int NthTag, Tag *tagsToIgnore, int totalNoOfTagsToIgnore, bool &err);
  36. Tag* findTag(char tbuf[],Tag *iT,int itc);
  37. Property* findProp(char pbuf[],Tag *tag);
  38. bool strCmpi(const char*,const char*);
  39. bool isSingleton(char[]);//Returns true if given tag is hr, br, input or any singleton whose name is in singleton array;
  40. //bool isAllPropChecked(Tag);
  41. inline bool isAlphaNum(char);
  42. inline bool isEvery_Except_Minus(char);
  43. inline bool isEvery_Except_LT(char);//is Everything except <
  44. inline bool isEvery_Except_Quote(char);
  45.  
  46. public:
  47. Parser();
  48. ~Parser();
  49. /*Set isFileName to true if Src is filename and to false if Src is an array.
  50.   */
  51. char* parse(char const *Src, Tag tag, int NthTag, Tag *tagsToIgnore, int totalNoOfTagsToIgnore, bool &err, bool isFileName=false);
  52. void release();
  53. };
Reputation Points: 25
Solved Threads: 0
Newbie Poster
applegrew is offline Offline
5 posts
since May 2007
May 23rd, 2007
0

Re: Heap block at 003D2CA0 modified at 003D2CB3 past requested size of b

An update. I tried compiling this using MS VC++ 6.0. It compiled well and also runs perfectly (whereas when compiled in GCC it crashes while running. I tried MinGW and Cygwin build of gcc 3.4). But the problem of it crashing, when the 'delete' instructions are uncommented, persists even here.
Reputation Points: 25
Solved Threads: 0
Newbie Poster
applegrew is offline Offline
5 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: cant paint in mFC
Next Thread in C++ Forum Timeline: calculate angle???





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC