943,975 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 584
  • C++ RSS
Oct 7th, 2008
0

Help me find my memory leak!

Expand Post »
I'm not too sure why I'm getting memory leaks...hopefully someone here is wiser than me, and can help me find it .

C++ Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
780 posts
since Nov 2007
Oct 7th, 2008
1

Re: Help me find my memory leak!

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.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Oct 7th, 2008
0

Re: Help me find my memory leak!

But just before that is the Clean() function, which should delete the old CPt, should it not?
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
780 posts
since Nov 2007
Oct 7th, 2008
0

Re: Help me find my memory leak!

Never mind...I'm an idiot...it's fixed now! Thanks alot!
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
780 posts
since Nov 2007
Oct 7th, 2008
0

Re: Help me find my memory leak!

remove this line
cpp Syntax (Toggle Plain Text)
  1. _cptPoints = new CPt [++_iSize];
Reputation Points: 21
Solved Threads: 22
Junior Poster
ivailosp is offline Offline
129 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Any help with C++ payroll! -noob
Next Thread in C++ Forum Timeline: Search an array of objects.





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


Follow us on Twitter


© 2011 DaniWeb® LLC