memory leak problem...

Reply

Join Date: Mar 2006
Posts: 78
Reputation: gampalu is an unknown quantity at this point 
Solved Threads: 0
gampalu gampalu is offline Offline
Junior Poster in Training

memory leak problem...

 
0
  #1
Jun 1st, 2006
Hi!

I built this class, however when I executed my program the memory run out quickly, because I wasn't making any "deletes", so I added the delete in the destructor, but It crashes there... what's wrong?

  1. class CMatrix
  2. {
  3. public:
  4. void Identity();
  5. void SetMaxColRow(int nRow, int nCol);
  6. void SetElement(int nRow, int nCol, long double element);
  7. void Show();
  8.  
  9. long double GetElement(int nRow, int nCol);
  10.  
  11. CMatrix operator+(CMatrix tmpMatriz);
  12. CMatrix operator-(CMatrix tmpMatriz);
  13. CMatrix operator*(long double scalar);
  14. CMatrix operator*(CMatrix tmpMatriz);
  15. CMatrix Transpose();
  16. void CMatrix::Invert();
  17. CMatrix GetInverted() const;
  18. CMatrix(int nRows, int nCols);
  19. CMatrix();
  20.  
  21. virtual ~CMatrix();
  22.  
  23. int m_nRows;
  24. int m_nCols;
  25.  
  26.  
  27. private:
  28. long double *m_pMatriz;
  29.  
  30. };

  1. //////////////////////////////////////////////////////////////////////
  2. // Construction/Destruction
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. // constructor
  6. CMatrix::CMatrix(int nRows, int nCols)
  7. {
  8. SetMaxColRow(nRows, nCols);
  9. }
  10.  
  11. // constructor
  12. CMatrix::CMatrix()
  13. {
  14. m_nCols = NULL;
  15. m_nRows = NULL;
  16. m_pMatriz = NULL;
  17. }
  18.  
  19.  
  20. // destructor
  21. CMatrix::~CMatrix()
  22. {
  23. if (m_pMatriz != NULL)
  24. delete[] m_pMatriz;
  25. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: memory leak problem...

 
0
  #2
Jun 1st, 2006
What does SetMaxColRow() actually do?

If you just construct and destruct this object in a test program, does it still crash?

Have you looked at what other things are doing elsewhere? Just because it crashes here doesn't mean the problem is here, when dealing with dynamic memory.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,149
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: memory leak problem...

 
0
  #3
Jun 1st, 2006
Originally Posted by gampalu
Hi!

I built this class, however when I executed my program the memory run out quickly, because I wasn't making any "deletes", so I added the delete in the destructor, but It crashes there... what's wrong?
Nothing wrong with what you posted -- its impossible to tell what the proglem is because you didn't post enough code. Check for (1) writing outside allocated array bounds, (2) using uninitialized pointers.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 78
Reputation: gampalu is an unknown quantity at this point 
Solved Threads: 0
gampalu gampalu is offline Offline
Junior Poster in Training

Re: memory leak problem...

 
0
  #4
Jun 1st, 2006
Originally Posted by Salem
What does SetMaxColRow() actually do?
Here it is the code:

  1. void CMatrix::SetMaxColRow(int nRows, int nCols)
  2. {
  3. m_nCols = nCols;
  4. m_nRows = nRows;
  5.  
  6. m_pMatriz = new long double[m_nRows * m_nCols];
  7. memset(m_pMatriz, 0.0, sizeof(long double) * m_nRows * m_nCols);
  8. }

Am I doing the correct delete , concerning the "new's" I made?



Originally Posted by Salem
If you just construct and destruct this object in a test program, does it still crash?
I haven't tried that yet... I'll try now!

Originally Posted by Salem
Have you looked at what other things are doing elsewhere? Just because it crashes here doesn't mean the problem is here, when dealing with dynamic memory.
I have looked arround the code and found no problems, at least for me, an inexperienced programmer...
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: memory leak problem...

 
0
  #5
Jun 1st, 2006
> m_nCols = NULL;
> m_nRows = NULL;
NULL typically signifies a pointer, so this is sending the wrong message to the reader
Just do
  1. m_nCols = 0;
  2. m_nRows = 0;

> memset(m_pMatriz, 0.0, sizeof(long double) * m_nRows * m_nCols);
http://c-faq.com/malloc/calloc.html
Filling the memory with 0 (even though you said 0.0, this is not what you get) does not guarantee a useful floating point value.
A simple loop is enough
  1. int lim = m_nRows * m_nCols;
  2. for ( int i = 0 ; i < lim ; i++ ) m_pMatriz[i] = 0.0;

> I have looked arround the code and found no problems, at least for me, an inexperienced programmer
Without lots of tools, pros find this hard to do as well.
Lots of testing at all stages, and walkthroughs with the debugger help you to understand what's really happening.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 78
Reputation: gampalu is an unknown quantity at this point 
Solved Threads: 0
gampalu gampalu is offline Offline
Junior Poster in Training

Re: memory leak problem...

 
0
  #6
Jun 1st, 2006
Originally Posted by Ancient Dragon
Nothing wrong with what you posted -- its impossible to tell what the proglem is because you didn't post enough code. Check for (1) writing outside allocated array bounds, (2) using uninitialized pointers.
(1) Could be this, but I don't see how...

(2) I haven't uninitialized pointers.

I'll put all the code of the class:

  1. // constructor
  2. CMatrix::CMatrix(int nRows, int nCols)
  3. {
  4. SetMaxColRow(nRows, nCols);
  5. }
  6.  
  7. // constructor
  8. CMatrix::CMatrix()
  9. {
  10. m_nCols = 0;
  11. m_nRows = 0;
  12. m_pMatriz = NULL;
  13. }
  14.  
  15.  
  16. // destructor
  17. CMatrix::~CMatrix()
  18. {
  19. if (m_pMatriz != NULL)
  20. delete[] m_pMatriz;
  21.  
  22. m_pMatriz = NULL;
  23. }
  24.  
  25.  
  26. // adding 2 matrixes
  27. CMatrix CMatrix::operator+(CMatrix tmpMatriz)
  28. {
  29. // first check for same size
  30. if ((m_nCols != tmpMatriz.m_nCols) || (m_nRows != tmpMatriz.m_nRows))
  31. {
  32. printf("\nError in sum: Matrices do not have same size.\n\n");
  33. exit(1);
  34. }
  35.  
  36. CMatrix ResMatriz(m_nRows,m_nCols);
  37.  
  38. // now add in the other matrix
  39. for (int i=0; i<m_nRows; i++)
  40. {
  41. for (int j=0; j<m_nCols; j++)
  42. ResMatriz.SetElement(i,j, *(m_pMatriz + i * m_nCols + j) + tmpMatriz.GetElement(i,j));
  43. }
  44. return ResMatriz;
  45. }
  46.  
  47.  
  48. // subtracting 2 matrixes
  49. CMatrix CMatrix::operator-(CMatrix tmpMatriz)
  50. {
  51. // first check for same size
  52. if ((m_nCols != tmpMatriz.m_nCols) || (m_nRows != tmpMatriz.m_nRows))
  53. {
  54. printf("\nError in subtract: Matrices do not have same size.\n\n");
  55. exit(1);
  56. }
  57.  
  58. CMatrix ResMatriz(m_nRows, m_nCols);
  59.  
  60. for (int i=0; i<m_nRows; i++)
  61. for (int j=0; j<m_nCols; j++)
  62. ResMatriz.SetElement(i,j, *(m_pMatriz + i * m_nCols + j) - tmpMatriz.GetElement(i,j));
  63.  
  64. return ResMatriz;
  65. }
  66.  
  67.  
  68. // multiplication of a scalar with a matrix
  69. CMatrix CMatrix::operator*(long double scalar)
  70. {
  71. CMatrix ResMatriz(m_nRows, m_nCols);
  72.  
  73. for (int i=0; i<m_nRows; i++)
  74. for (int j=0; j<m_nCols; j++)
  75. *(ResMatriz.m_pMatriz + i * m_nCols + j) = scalar * (*(m_pMatriz + i * m_nCols + j));
  76.  
  77. return ResMatriz;
  78. }
  79.  
  80.  
  81. // multiplication of 2 matrixes
  82. CMatrix CMatrix::operator*(CMatrix tmpMatriz)
  83. {
  84. // first check for a valid multiplication operation
  85. if (m_nCols != tmpMatriz.m_nRows)
  86. {
  87. printf("\nError in multipication: Matrices do not have common size.\n\n");
  88. exit(1);
  89. }
  90.  
  91. // construct the object we are going to return
  92. CMatrix ResMatriz(m_nRows, tmpMatriz.m_nCols);
  93.  
  94. // e.g.
  95. // [A][B][C] [G][H] [A*G + B*I + C*K][A*H + B*J + C*L]
  96. // [D][E][F] * [I][J] = [D*G + E*I + F*K][D*H + E*J + F*L]
  97. // [K][L]
  98. //
  99.  
  100.  
  101. long double value = 0;
  102. for (int i=0; i<ResMatriz.m_nCols; i++)
  103. for (int j=0; j<ResMatriz.m_nRows; j++)
  104. {
  105. value = 0.0 ;
  106. for (int k=0; k<m_nCols; k++)
  107. value += *(m_pMatriz + j * m_nCols + k) * tmpMatriz.GetElement(k,i);
  108.  
  109. ResMatriz.SetElement(j, i, value) ;
  110. }
  111.  
  112. return ResMatriz;
  113. }
  114.  
  115.  
  116.  
  117. // sets a specific element
  118. void CMatrix::SetElement(int nRow, int nCol, long double element)
  119. {
  120. if ((nCol >= m_nCols) || (nRow >= m_nRows) || (nRow < 0 ) || (nCol < 0))
  121. {
  122. printf("\nError in SetElement.\n\n");
  123. exit(1);
  124. }
  125.  
  126. *(m_pMatriz + nRow * m_nCols + nCol) = element;
  127.  
  128. }
  129.  
  130.  
  131. // gets one specific element
  132. long double CMatrix::GetElement(int nRow, int nCol)
  133. {
  134. long double element = 0;
  135.  
  136. if ((nCol >= m_nCols) || (nRow >= m_nRows) || (nRow < 0 ) || (nCol < 0))
  137. {
  138. printf("\nError in GetElement.\n\n");
  139. exit(1);
  140. }
  141.  
  142.  
  143. element = *(m_pMatriz + nRow * m_nCols + nCol);
  144.  
  145. return element;
  146. }
  147.  
  148.  
  149.  
  150. // prints a matrix
  151. void CMatrix::Show()
  152. {
  153. for (int i=0; i<m_nRows; i++)
  154. {
  155. for (int j=0; j<m_nCols; j++)
  156. printf("%8lf\t", *(m_pMatriz + i * m_nCols + j));
  157. printf("\n");
  158. }
  159. printf("\n");
  160. }
  161.  
  162. void CMatrix::SetMaxColRow(int nRows, int nCols)
  163. {
  164. m_nCols = nCols;
  165. m_nRows = nRows;
  166.  
  167. m_pMatriz = new long double[m_nRows * m_nCols];
  168.  
  169. int lim = m_nRows * m_nCols;
  170. for ( int i = 0 ; i < lim ; i++ )
  171. m_pMatriz[i] = 0.0;
  172. }
  173.  
  174. void CMatrix::Identity()
  175. {
  176. // first check if matrix is square
  177. if (m_nCols != m_nRows)
  178. {
  179. printf("\nMatrix is not square.\n\n");
  180. exit(1);
  181. }
  182.  
  183. for (int i=0; i<m_nRows; i++)
  184. for (int j=0; j<m_nRows; j++)
  185. *(m_pMatriz + i * m_nCols + j) = 0.0;
  186.  
  187. //memset(m_pMatriz, 0.0, sizeof(long double) * m_nRows * m_nCols);
  188.  
  189. for (int k=0; k<m_nRows; k++)
  190. *(m_pMatriz + k * m_nCols + k) = 1.0;
  191.  
  192. }
  193.  
  194.  
  195.  
  196. // matrix tranpose
  197. CMatrix CMatrix::Transpose()
  198. {
  199. // construct the object we are going to return
  200. CMatrix ResMatriz(m_nCols, m_nRows);
  201.  
  202. // copy across the transposed data
  203. for (int i=0; i<m_nCols; i++)
  204. for (int j=0; j<m_nRows; j++)
  205. ResMatriz.SetElement(i, j, *(m_pMatriz + j * m_nCols + i));
  206.  
  207. return ResMatriz;
  208. }
  209.  
  210.  
  211.  
  212. CMatrix CMatrix::GetInverted() const
  213. {
  214. // matrix inversion will only work on square matrices
  215. if (m_nCols != m_nRows)
  216. {
  217. printf("\nMatrix is not square.\n\n");
  218. exit(1);
  219. }
  220.  
  221. // construct the object we are going to return
  222. CMatrix ResMatriz(m_nCols, m_nCols);
  223.  
  224. // return this matrix inverted
  225. CMatrix copy(*this);
  226. copy.Invert();
  227.  
  228. return copy;
  229. }
  230.  
  231. void CMatrix::Invert()
  232. {
  233. // matrix inversion will only work on square matrices
  234. // invert ourselves
  235. if (m_nCols != m_nRows)
  236. {
  237. printf("\nMatrix is not square.\n\n");
  238. exit(1);
  239. }
  240. long double e = 0;
  241.  
  242. for (int k = 0 ; k < m_nCols ; ++k)
  243. {
  244. e = GetElement(k, k) ;
  245. SetElement(k, k, 1.0) ;
  246. if (e == 0.0)
  247. {
  248. printf("\nMatrix inversion error\n");
  249. exit(1);
  250. }
  251.  
  252. for (int j = 0 ; j < m_nCols ; ++j)
  253. SetElement(k, j, GetElement(k, j) / e) ;
  254. for (int i = 0 ; i < m_nCols ; ++i)
  255. {
  256. if (i != k)
  257. {
  258. e = GetElement(i, k) ;
  259. SetElement(i, k, 0.0) ;
  260. for (j = 0 ; j < m_nCols ; ++j)
  261. SetElement(i, j, GetElement(i, j) - e * GetElement(k, j)) ;
  262. }
  263. }
  264. }
  265. }

Thanks in advance for helping
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 78
Reputation: gampalu is an unknown quantity at this point 
Solved Threads: 0
gampalu gampalu is offline Offline
Junior Poster in Training

Re: memory leak problem...

 
0
  #7
Jun 1st, 2006
Originally Posted by Salem
> m_nCols = NULL;
> m_nRows = NULL;
NULL typically signifies a pointer, so this is sending the wrong message to the reader
Just do
  1. m_nCols = 0;
  2. m_nRows = 0;
Thanks for the tip, I've already correct it in other places

Originally Posted by Salem
> memset(m_pMatriz, 0.0, sizeof(long double) * m_nRows * m_nCols);
http://c-faq.com/malloc/calloc.html
Filling the memory with 0 (even though you said 0.0, this is not what you get) does not guarantee a useful floating point value.
A simple loop is enough
  1. int lim = m_nRows * m_nCols;
  2. for ( int i = 0 ; i < lim ; i++ ) m_pMatriz[i] = 0.0;
Didn't know that memset wasn't reliable, updated

Originally Posted by Salem
> I have looked arround the code and found no problems, at least for me, an inexperienced programmer
Without lots of tools, pros find this hard to do as well.
Lots of testing at all stages, and walkthroughs with the debugger help you to understand what's really happening.
I've tried to test it with a single object as show and it worked, but not in my program...
  1. int main(int argc, char* argv[])
  2. {
  3.  
  4. CMatrix m1(2,2);
  5. m1.Identity();
  6. m1.Show();
  7. return 0;
  8. }

output:
  1. 1.000000 0.000000
  2. 0.000000 1.000000
  3.  
  4. Press any key to continue
Last edited by Dave Sinkula; Jun 1st, 2006 at 2:45 pm. Reason: Added quote tags to help with my confusion.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: memory leak problem...

 
0
  #8
Jun 1st, 2006
> *(m_pMatriz + k * m_nCols + k) = 1.0;
In many places in your class member functions, you have this complex row/col calculation.
In other places, you call SetElement() and GetElement()

With the exception of the Set and Get functions, nothing should be aware of how the data is actually accessed. That is, all your other member functions should use the Set and Get methods.

Which OS and compiler are you using?
For Linux, consider using 'valgrind' or 'electric fence'
  1. #include <iostream>
  2.  
  3. int main ( ) {
  4. int *mem = new int[5];
  5. for ( int i = 0 ; i <= 5 ; i++ ) { // oops, 1 too far
  6. mem[i] = 0;
  7. }
  8. delete [] mem;
  9. return 0;
  10. }
  11.  
  12. $ g++ foo.cpp
  13. $ ./a.out
  14. *** glibc detected *** ./a.out: free(): invalid next size (fast): 0x091ed008 ***
  15. ======= Backtrace: =========
  16. /lib/libc.so.6[0x5b4424]
  17. /lib/libc.so.6(__libc_free+0x77)[0x5b495f]
  18. /usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0x9a2669]
  19. /usr/lib/libstdc++.so.6(_ZdaPv+0x1d)[0x9a26b5]
  20. ./a.out(__gxx_personality_v0+0x134)[0x80485c4]
  21. /lib/libc.so.6(__libc_start_main+0xc6)[0x565de6]
  22. ./a.out(__gxx_personality_v0+0x51)[0x80484e1]
  23. ======= Memory map: ========
  24. 0052f000-00549000 r-xp 00000000 fd:00 5342221 /lib/ld-2.3.5.so
  25. 00549000-0054a000 r-xp 00019000 fd:00 5342221 /lib/ld-2.3.5.so
  26. 0054a000-0054b000 rwxp 0001a000 fd:00 5342221 /lib/ld-2.3.5.so
  27. 00551000-00675000 r-xp 00000000 fd:00 5342973 /lib/libc-2.3.5.so
  28. 00675000-00677000 r-xp 00124000 fd:00 5342973 /lib/libc-2.3.5.so
  29. 00677000-00679000 rwxp 00126000 fd:00 5342973 /lib/libc-2.3.5.so
  30. 00679000-0067b000 rwxp 00679000 00:00 0
  31. 0067d000-0069f000 r-xp 00000000 fd:00 5342974 /lib/libm-2.3.5.so
  32. 0069f000-006a0000 r-xp 00021000 fd:00 5342974 /lib/libm-2.3.5.so
  33. 006a0000-006a1000 rwxp 00022000 fd:00 5342974 /lib/libm-2.3.5.so
  34. 008ac000-008b5000 r-xp 00000000 fd:00 5342977 /lib/libgcc_s-4.0.0-20050520.so.1
  35. 008b5000-008b6000 rwxp 00009000 fd:00 5342977 /lib/libgcc_s-4.0.0-20050520.so.1
  36. 008b6000-008b7000 r-xp 008b6000 00:00 0
  37. 008ee000-009cd000 r-xp 00000000 fd:00 9460718 /usr/lib/libstdc++.so.6.0.4
  38. 009cd000-009d2000 rwxp 000df000 fd:00 9460718 /usr/lib/libstdc++.so.6.0.4
  39. 009d2000-009d7000 rwxp 009d2000 00:00 0
  40. 08048000-08049000 r-xp 00000000 fd:00 7274627 /home/me/a.out
  41. 08049000-0804a000 rw-p 00000000 fd:00 7274627 /home/me/a.out
  42. 091ed000-0920e000 rw-p 091ed000 00:00 0 [heap]
  43. b7e00000-b7e21000 rw-p b7e00000 00:00 0
  44. b7e21000-b7f00000 ---p b7e21000 00:00 0
  45. b7f38000-b7f3a000 rw-p b7f38000 00:00 0
  46. b7f5d000-b7f5e000 rw-p b7f5d000 00:00 0
  47. bfe49000-bfe5e000 rw-p bfe49000 00:00 0 [stack]
  48. Aborted
  49.  
  50. $ valgrind ./a.out
  51. ==29215== Memcheck, a memory error detector for x86-linux.
  52. ==29215== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et al.
  53. ==29215== Using valgrind-2.4.0, a program supervision framework for x86-linux.
  54. ==29215== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al.
  55. ==29215== For more details, rerun with: -v
  56. ==29215==
  57. ==29215== Invalid write of size 4
  58. ==29215== at 0x80485A2: main (in /home/me/a.out)
  59. ==29215== Address 0x1B93503C is 0 bytes after a block of size 20 alloc'd
  60. ==29215== at 0x1B9095DA: operator new[](unsigned) (vg_replace_malloc.c:138)
  61. ==29215== by 0x8048589: main (in /home/me/a.out)
  62. ==29215==
  63. ==29215== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 16 from 1)
  64. ==29215== malloc/free: in use at exit: 0 bytes in 0 blocks.
  65. ==29215== malloc/free: 1 allocs, 1 frees, 20 bytes allocated.
  66. ==29215== For counts of detected errors, rerun with: -v
  67. ==29215== No malloc'd blocks -- no leaks are possible.
  68.  
  69. $ g++ -g foo.cpp -lefence
  70. $ gdb ./a.out
  71. GNU gdb Red Hat Linux (6.3.0.0-1.21rh)
  72. Copyright 2004 Free Software Foundation, Inc.
  73. GDB is free software, covered by the GNU General Public License, and you are
  74. welcome to change it and/or distribute copies of it under certain conditions.
  75. Type "show copying" to see the conditions.
  76. There is absolutely no warranty for GDB. Type "show warranty" for details.
  77. This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".
  78.  
  79. (gdb) run
  80. Starting program: /home/me/a.out
  81. Reading symbols from shared object read from target memory...done.
  82. Loaded system supplied DSO at 0x135000
  83.  
  84. Electric Fence 2.2.0 Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>
  85.  
  86. Program received signal SIGSEGV, Segmentation fault.
  87. 0x0804869a in main () at foo.cpp:6
  88. 6 mem[i] = 0;
  89. (gdb) quit
  90. The program is running. Exit anyway? (y or n) y
  91. $

> int main
It would be really useful if you could produce a simple main() which did show up a problem.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 78
Reputation: gampalu is an unknown quantity at this point 
Solved Threads: 0
gampalu gampalu is offline Offline
Junior Poster in Training

Re: memory leak problem...

 
0
  #9
Jun 1st, 2006
Originally Posted by Salem
> *(m_pMatriz + k * m_nCols + k) = 1.0;
In many places in your class member functions, you have this complex row/col calculation.
In other places, you call SetElement() and GetElement()

With the exception of the Set and Get functions, nothing should be aware of how the data is actually accessed. That is, all your other member functions should use the Set and Get methods.
I understand your point, but there's one problem, the variable m_pMatriz is double* and the GetElement and SetElement are methods for CMatrix types... So I can't use it, right? At least not directly... But how I don't know...

Which OS and compiler are you using?
Windows XP


  1. #include <iostream>
  2.  
  3. int main ( ) {
  4. int *mem = new int[5];
  5. for ( int i = 0 ; i <= 5 ; i++ ) { // oops, 1 too far
  6. mem[i] = 0;
  7. }
  8. delete [] mem;
  9. return 0;
  10. }
I've tested this exact code and I now understand the problem when we allocate less space than necessary
So the problem should be that, having nothing to do with delete!

How do you suggest me to find?

Thank you for all your attention and patiente
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: memory leak problem...

 
0
  #10
Jun 1st, 2006
> the variable m_pMatriz is double* and the GetElement and SetElement are methods for CMatrix types
But those functions take and return doubles, so there should be no problem.
They're used fine in some contexts already anyway.

I've attached your code with Set/Get used in many more places, and it compiles fine here.
The //!! comments are what my g++ compiler spotted with
$ g++ -c -W -Wall -ansi -pedantic -O2 foo.cpp

Oh, you might want to rename all your loop variables from i,j to be say r,c to denote row and column. It's not always clear whether you're accessing the correct element of the array (when you're doing it directly), and r/c would make it more readable IMO.
At least by always using the set/get functions, every access is properly range checked (as it should be).
Attached Files
File Type: cpp foo.cpp (6.4 KB, 2 views)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC