944,049 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1905
  • C++ RSS
Jan 31st, 2007
0

can't find my memory leak

Expand Post »
>_<

I've been searching for nearly an hour and trying to find this memory leak. If you guys could help me find it it would be great. Thx.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstring>
  3. #include <crtdbg.h>
  4.  
  5. using std::cout;
  6. using std::cin;
  7. using std::ios_base;
  8.  
  9. /////////////// user defined data /////////////////////
  10. /////////////// function prototypes ////////////////////
  11. class State
  12. {
  13. public:
  14. int initState (char * name, int year, char * cap, int popRank);
  15. void deleteState ();
  16. void copyState (State & tempState);
  17. void printState ();
  18. private:
  19. char * name; // state name
  20. int year; // year of entry into the union
  21. char * cap; // state capital
  22. int popRank; // population ranking
  23. };
  24.  
  25. class Database
  26. {
  27. public:
  28. int initDatabase (char * file = "states.txt");
  29. void deleteDatabase ();
  30. void fillDatabase ();
  31. void insertDatabase (int index, State & tempState);
  32. void printDatabase ();
  33. private:
  34. State * list; // array of states
  35. int count; // total count of states
  36. char * filename; // input filename
  37. };
  38.  
  39.  
  40. ////////////////////////// program start ///////////////////////////////////////
  41. int main(void)
  42. {
  43. Database db;
  44.  
  45. if ( db.initDatabase() )
  46. {
  47. // then initialize fields of Database struct
  48. db.fillDatabase(); // read from file
  49. db.printDatabase(); // print data
  50. db.deleteDatabase(); // free memory in Database struct
  51.  
  52. cout << '\n' << (_CrtDumpMemoryLeaks() ? "Memory Leak\n" : "No Leak");
  53.  
  54. return 0;
  55. }
  56. else // not successful
  57. {
  58. return 1;
  59. }
  60. }
  61.  
  62. ////////////////////// Database functions ////////////////////////////////
  63.  
  64. //initDatabase - allocates memory for Database struct and initializes
  65. // each field
  66. //input: input filename
  67. //return: pointer to Database struct if success
  68. // NULL if fail
  69. int Database::initDatabase (char * file)
  70. {
  71. FILE * fpIn;
  72. char * string;
  73. State * states;
  74.  
  75. // open input file and check for open success
  76. if ( ( fpIn = fopen( file, "r" ) ) )
  77. {
  78. // allocate memory for Database struct and check for success
  79. // allocate memory for filename field and initializes with filename
  80. if ( string = new char[strlen( file ) + 1] )
  81. {
  82. strcpy( string, file );
  83. filename = string;
  84. }
  85. else
  86. {
  87. cout << "Not enough memory!" << '\n';
  88. delete [] string;
  89. return 0;
  90. }
  91.  
  92. // initialize count field to 50
  93. count = 50;
  94.  
  95. // initialize list field by allocating memory for array of 50 states
  96. // and check for success
  97. if ( states = new State[50] )
  98. {
  99. list = states;
  100. }
  101. else
  102. {
  103. cout << "Not enough memory!" << '\n';
  104. delete [] states;
  105. return 0;
  106. }
  107. }
  108.  
  109. // close input file
  110. fclose( fpIn );
  111.  
  112. return 1;
  113. }
  114.  
  115. //deleteDatabase - frees allocated memory inside a Database struct
  116. //input: pointer to Database struct
  117. //return: nothing
  118.  
  119. void Database::deleteDatabase ()
  120. {
  121. for ( int cnt = 0; cnt < count; cnt++ )
  122. list[cnt].deleteState();
  123.  
  124. delete [] filename;
  125. delete [] list;
  126. }
  127.  
  128. //fillDatabase - reads info from file and stores in array of State structs
  129. //input: pointer to Database struct
  130. //return: nothing
  131. void Database::fillDatabase ()
  132. {
  133. State tempState; // temp State struct to store data from file
  134. char name[80]; // temp variables
  135. int year; // to store
  136. char cap[80]; // input data
  137. int popRank; // from file
  138. int index = 0; // index into array of State structs
  139.  
  140. // open file from filename field of db
  141. // don't need to check for file open success because initDatabase already checked
  142. FILE *fpIn = fopen( filename, "r" );
  143.  
  144. // loop through all the states
  145. for ( int cnt = 0; cnt < count; cnt++ )
  146. {
  147. // read and parse one line of file into 4 input data fields
  148. if ( fscanf( fpIn, "%[^,], %d, %[^,], %d", name, &year, cap, &popRank ) == 4 )
  149. {
  150. // if tempState is created successfully from the 4 input data
  151. if ( tempState.initState (name, year, cap, popRank) )
  152. { // then copy tempState data into array of states at the current index
  153. insertDatabase (index, tempState);
  154. }
  155. index++;
  156. }
  157. else
  158. {
  159. cout << "Error reading file!" << '\n';
  160. exit(0);
  161. }
  162. }
  163.  
  164. // close file
  165. fclose( fpIn );
  166. }
  167.  
  168. //insertDatabase - copy state data from tempState into appropriate place in array
  169. //input: pointer to Database struct
  170. // current index in array
  171. // pointer to tempState containing state data
  172. //return: nothing
  173. void Database::insertDatabase (int index, State & tempState)
  174. {
  175. // set pCurrent to location to be inserted in array
  176. State * pCurrent = list + index;
  177. // copy from state to pCurrent
  178. (*pCurrent).copyState( tempState );
  179.  
  180. return;
  181. }
  182.  
  183. //printDatabase - print data of state array
  184. //input: pointer to Database struct
  185. //return: nothing
  186. void Database::printDatabase ()
  187. {
  188. cout << " State name Year Capital Pop. rank" << '\n';
  189. cout << "------------------------------------------------" << '\n';
  190.  
  191. for (int i = 0; i < count; i++)
  192. list[i].printState();
  193.  
  194. return;
  195. }
  196.  
  197. //////////////////////// State functions //////////////////////////////
  198.  
  199. //initState - initializes a State struct with input data
  200. //input: pointer to State struct
  201. // name, year, capital, and population rank of state
  202. //return: true for success
  203. // false for fail
  204. int State::initState (char * tempName, int tempYear, char * tempCap, int tempPopRank)
  205. {
  206. char * stateName;
  207. char * capital;
  208. // if memory allocation is successful, store name
  209. if ( stateName = new char[strlen( tempName ) + 1 ] )
  210. {
  211. strcpy( stateName, tempName );
  212. name = stateName;
  213. }
  214. else
  215. {
  216. cout << "Not enough memory!" << '\n';
  217. delete [] stateName;
  218. return 0;
  219. }
  220.  
  221. // store year
  222. year = tempYear;
  223.  
  224. // if memory allocation is successful, store capital
  225. if ( capital = new char[strlen( tempCap ) + 1 ] )
  226. {
  227. strcpy( capital, tempCap );
  228. cap = capital;
  229. }
  230. else
  231. {
  232. cout << "Not enough memory!" << '\n';
  233. delete [] capital;
  234. return 0;
  235. }
  236.  
  237. // store population ranking
  238. popRank = tempPopRank;
  239.  
  240. return 1;
  241. }
  242.  
  243. //deleteState - frees allocated memory inside a State struct
  244. //input: pointer to State struct
  245. //return: nothing
  246. void State::deleteState ()
  247. {
  248. delete [] name;
  249. delete [] cap;
  250. }
  251.  
  252. //copyState - copies contents between two State structs
  253. //input: pointer to source State struct
  254. // pointer to destination State struct
  255. //return: nothing
  256. void State::copyState (State & tempState)
  257. {
  258. name = tempState.name;
  259. year = tempState.year;
  260. cap = tempState.cap;
  261. popRank = tempState.popRank;
  262. }
  263.  
  264. //printState - displays State data
  265. //input: pointer to State struct
  266. //return: nothing
  267. void State::printState ()
  268. {
  269. cout.setf(ios_base::left);
  270. cout.width(17);
  271. cout << name;
  272.  
  273. cout.setf(ios_base::left);
  274. cout.width(8);
  275. cout << year;
  276.  
  277. cout.setf(ios_base::left);
  278. cout.width(18);
  279. cout << cap ;
  280.  
  281. cout.setf(ios_base::left);
  282. cout.width(2);
  283. cout << popRank << '\n';
  284.  
  285. return;
  286. }
  287. ////////////////////////// program end ////////////////////////////////////

here is the states.txt file:
C++ Syntax (Toggle Plain Text)
  1. Connecticut,1788,Hartford,29
  2. Maine,1820,Augusta,40
  3. Massachusetts,1788,Boston,13
  4. New Hampshire,1788,Concord,41
  5. Rhode Island,1790,Providence,43
  6. Vermont,1791,Montpelier,49
  7. Delaware,1787,Dover,45
  8. Maryland,1788,Annapolis,19
  9. New Jersey,1787,Trenton,10
  10. New York,1788,Albany,3
  11. Pennsylvania,1787,Harrisburg,6
  12. Alabama,1819,Montgomery,23
  13. Arkansas,1836,Little Rock,32
  14. Florida,1845,Tallahassee,4
  15. Georgia,1788,Atlanta,9
  16. Kentucky,1792,Frankfort,26
  17. Louisiana,1812,Baton Rouge,24
  18. Mississippi,1817,Jackson,31
  19. Missouri,1821,Jefferson City,17
  20. North Carolina,1789,Raleigh,11
  21. South Carolina,1788,Columbia,25
  22. Tennessee,1796,Nashville,16
  23. Virginia,1788,Richmond,12
  24. West Virginia,1863,Charleston,37
  25. Illinois,1818,Springfield,5
  26. Indiana,1816,Indianapolis,14
  27. Iowa,1846,Des Moines,30
  28. Kansas,1861,Topeka,33
  29. Michigan,1837,Lansing,8
  30. Minnesota,1858,St. Paul,21
  31. Nebraska,1867,Lincoln,38
  32. North Dakota,1889,Bismarck,11
  33. Ohio,1803,Columbus,7
  34. South Dakota,1889,Pierre,46
  35. Wisconsin,1848,Madison,20
  36. Arizona,1912,Phoenix,18
  37. New Mexico,1912,Santa Fe,36
  38. Oklahoma,1907,Oklahoma City,28
  39. Texas,1845,Austin,2
  40. Alaska,1959,Juneau,47
  41. California,1850,Sacramento,1
  42. Colorado,1867,Denver,22
  43. Hawaii,1959,Honolulu,42
  44. Idaho,1890,Boise,39
  45. Montana,1889,Helena,44
  46. Nevada,1864,Carson City,35
  47. Oregon,1859,Salem,27
  48. Utah,1896,Salt Lake City,34
  49. Washington,1889,Olympia,15
  50. Wyoming,1890,Cheyenne,50
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
Jan 31st, 2007
0

Re: can't find my memory leak

It outputs a list of all the blocks to the debug console, like
{42} block ....

You take the 42 and assign it to something like _dbgBreakAlloc (read the manual to find out what it is actually called), then the allocator will stop when that block is allocated.

You then look at the stack contexts to find out where in your code that block is being allocated, then figure out why it isn't being freed.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

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: .txt/array problems
Next Thread in C++ Forum Timeline: C++ Help





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


Follow us on Twitter


© 2011 DaniWeb® LLC