Help me find my memory leak!

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Help me find my memory leak!

 
0
  #1
Oct 7th, 2008
I'm not too sure why I'm getting memory leaks...hopefully someone here is wiser than me, and can help me find it .

  1. #include "Canvas.h"
  2.  
  3. CDrawer CCanvas::gCanvas(RGB(0,0,0), 1);
  4. int const GREEN(20000),BLUE(50), RED(200), THICK(3);
  5.  
  6. CCanvas::CCanvas(void): _iSize(0), _cptPoints(0)
  7. {
  8. }
  9.  
  10. CCanvas::CCanvas(CCanvas const & tmpCanvas): _iSize(tmpCanvas._iSize)
  11. {
  12. if (_iSize > 0)
  13. {
  14. _cptPoints = new CPt [_iSize];
  15. for (int i(0); i < _iSize; ++i)
  16. {
  17. _cptPoints[i]._iX = tmpCanvas._cptPoints[i]._iX;
  18. _cptPoints[i]._iY = tmpCanvas._cptPoints[i]._iY;
  19. _cptPoints[i]._COLOR = GREEN;
  20. }
  21. }
  22. }
  23.  
  24. CCanvas CCanvas::operator =(const CCanvas & RHS)
  25. {
  26. if (RHS._iSize > 0)
  27. {
  28. _iSize = RHS._iSize;
  29. _cptPoints = new CPt [RHS._iSize];
  30. for (int i(0); i < _iSize; i++)
  31. {
  32. _cptPoints[i]._iX = RHS._cptPoints[i]._iX;
  33. _cptPoints[i]._iY = RHS._cptPoints[i]._iY;
  34. _cptPoints[i]._COLOR = RED;
  35. }
  36. }
  37. return *this;
  38. }
  39.  
  40. CCanvas & CCanvas::Show(void)
  41. {
  42. if (_iSize > 1)
  43. {
  44. gCanvas.Clear();
  45. for (int i(0); i < _iSize - 1; ++i)
  46. {
  47. for (int ii(i + 1); ii < _iSize; ++ii)
  48. if (i != ii)
  49. ShowPts(gCanvas, _cptPoints[i], _cptPoints[ii]);
  50. }
  51. gCanvas.Render();
  52. }
  53. return *this;
  54. }
  55.  
  56. CCanvas::~CCanvas(void)
  57. {
  58. Clean();
  59. }
  60.  
  61. CCanvas & CCanvas::AddLines(int iX, int iY, COLORREF tmpCol)
  62. {
  63. CPt * Tmp = new CPt [_iSize + 1];
  64. for (int i(0); i < _iSize; ++i)
  65. {
  66. Tmp[i]._iX = _cptPoints[i]._iX;
  67. Tmp[i]._iY = _cptPoints[i]._iY;
  68. Tmp[i]._COLOR = _cptPoints[i]._COLOR;
  69. }
  70. Clean();
  71. _cptPoints = new CPt [++_iSize];
  72. _cptPoints = Tmp;
  73. Tmp = 0;
  74. _cptPoints[_iSize-1]._iX = iX;
  75. _cptPoints[_iSize-1]._iY = iY;
  76. _cptPoints[_iSize-1]._COLOR = tmpCol;
  77. Show();
  78. return *this;
  79. }
  80.  
  81. void CCanvas::Clean(void)
  82. {
  83. delete [] _cptPoints;
  84. _cptPoints = 0;
  85. }

this is the class where it's occurring. If anyone needs the other classes/files just ask. Thanks!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Help me find my memory leak!

 
1
  #2
Oct 7th, 2008
These lines here

  _cptPoints = new CPt [++_iSize];
  _cptPoints = Tmp;

You allocate space for _cptPoints, and then you assign it to Tmp, which means you've now lost the memory pointer assigned by new CPt [++_iSize]; .. therefore not sure how you can delete it. That may be causing one of your leaks.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Help me find my memory leak!

 
0
  #3
Oct 7th, 2008
But just before that is the Clean() function, which should delete the old CPt, should it not?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Help me find my memory leak!

 
0
  #4
Oct 7th, 2008
Never mind...I'm an idiot...it's fixed now! Thanks alot!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 129
Reputation: ivailosp is an unknown quantity at this point 
Solved Threads: 22
ivailosp ivailosp is offline Offline
Junior Poster

Re: Help me find my memory leak!

 
0
  #5
Oct 7th, 2008
remove this line
  1. _cptPoints = new CPt [++_iSize];
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 448 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC