Memory leak in game

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 1,460
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 121
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Memory leak in game

 
0
  #1
Mar 21st, 2008
Hi

I have a memory leak somewhere in this code but I have no idea where. Can anybody help me out ?
  1. #include <windows.h>
  2. #include <mmsystem.h>
  3. #pragma comment(lib, "winmm.lib")
  4. #include <fstream>
  5. #include <time.h>
  6. #include <cstring>
  7. #include "resource.h"
  8.  
  9. using namespace std;
  10.  
  11. const char cn[] = "SameGame"; // ClassName
  12.  
  13. HBITMAP BLUEBMP = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BLUE1));
  14. HBITMAP GREENBMP = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_GREEN1));
  15. HBITMAP REDBMP = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_RED1));
  16. HBITMAP YELLOWBMP = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_YELLOW1));
  17. HBITMAP BLUEBMP_S = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BLUE2));
  18. HBITMAP GREENBMP_S = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_GREEN2));
  19. HBITMAP REDBMP_S = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_RED2));
  20. HBITMAP YELLOWBMP_S = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_YELLOW2));
  21.  
  22. #define EMPTY 0
  23.  
  24. #define IS_SELECTED(B) (B > 4)
  25. #define IS_EMPTY(B) (B == EMPTY)
  26.  
  27. #define SELECTED 8 // 1000
  28. #define BLUE 1 // 0001
  29. #define GREEN 2 // 0010
  30. #define RED 3 // 0011
  31. #define YELLOW 4 // 0100
  32.  
  33. int width = 15; // Grid Width
  34. int height = 15; // Grid Height
  35.  
  36. #define ballSize 30 // Ball Diameter
  37.  
  38. #define ID_DELAY 1
  39. #define ID_DELAY2 2
  40.  
  41. int score = 0;
  42. int tempScore = 0;
  43.  
  44. bool MEGASHIFT = 0;
  45.  
  46. unsigned char grid[100][100];
  47.  
  48. HPEN pen = CreatePen(PS_SOLID , 0 , RGB(255,255,255));
  49. HPEN panel = CreatePen(PS_SOLID , 0 , RGB(0,0,0));
  50. HDC hdc;
  51. HBITMAP bmp;
  52. BITMAP bm;
  53. PAINTSTRUCT ps;
  54. HDC _hdc;
  55. HDC _hdcMem;
  56. HBITMAP hbmOld;
  57.  
  58. int randNum;
  59. int RandomNumber(int high, int low) {
  60. return ((rand() % high - low) + low + 1);
  61. }
  62.  
  63. int bi; // Ball Index
  64. int bc; // Ball count
  65.  
  66. int TB; // SHIFTING TOP BALL
  67. int BB; // SHIFTING BOTTOM BALL
  68. int RB; // SHIFTING RIGHT BALL
  69. int LB; // SHIFTING LEFT BALL
  70.  
  71. UINT _w = width;
  72. UINT _h = height;
  73.  
  74. char *_score = new char();
  75. char *_tempscore = new char();
  76.  
  77. bool idg = 0;
  78.  
  79. void (*delayFunction)(HWND);
  80.  
  81. int GETINTLENGTH(int num) {
  82. int curMax = 10;
  83. int noOfChs = 1;
  84. while (num > curMax) {
  85. curMax *= 10;
  86. noOfChs++;
  87. }
  88. return noOfChs;
  89. }
  90.  
  91. char *INTTOCHARP(int num) {
  92. char *val = new char();
  93. _itoa_s(num, val, GETINTLENGTH(num)+1, 10);
  94. return val;
  95. }
  96. int GET_TRUE_COL(int ball) {
  97. return ball ^ SELECTED;
  98. }
  99. void REMOVEALLSELECTED() {
  100. for (int y = 0; y < height; y++) {
  101. for (int x = 0; x < width; x++) {
  102. if (IS_SELECTED(grid[y][x])) {
  103. grid[y][x] = EMPTY;
  104. }
  105. }
  106. }
  107. }
  108. void UNSELECTALL() {
  109. for (int y = 0; y < height; y++) {
  110. for (int x = 0; x < width; x++) {
  111. if (IS_SELECTED(grid[y][x])) {
  112. grid[y][x] ^= SELECTED;
  113. }
  114. }
  115. }
  116. }
  117. void REPAINT(HWND hwnd) {
  118. InvalidateRect(hwnd, NULL, 1);
  119. }
  120. void NEWGAME(HWND hwnd) {
  121. srand((int)time(NULL));
  122. score = 0;
  123. tempScore = 0;
  124. for (int y = 0; y < height; y++) {
  125. for (int x = 0; x < width; x++) {
  126. randNum = RandomNumber(4,0);
  127. grid[y][x] = randNum;
  128. }
  129. }
  130. REPAINT(hwnd);
  131. }
  132. void FLOOD(int x, int y, int col) {
  133. grid[y][x] |= SELECTED;
  134. bc++;
  135. if (x >= 1) if (col == (int) grid[y][x-1] && int( grid[y][x-1] ) ^ (int) col == SELECTED) FLOOD(x-1, y, col);
  136. if (x <= width-2) if (col == (int) grid[y][x+1] && int( grid[y][x+1] ) ^ (int) col == SELECTED) FLOOD(x+1, y, col);
  137. if (y >= 1) if (col == (int) grid[y-1][x] && int( grid[y-1][x] ) ^ (int) col == SELECTED) FLOOD(x, y-1, col);
  138. if (y <= height-2) if (col == (int) grid[y+1][x] && int( grid[y+1][x] ) ^ (int) col == SELECTED) FLOOD(x, y+1, col);
  139. }
  140. void SHIFTALLDOWN() {
  141. for (int y = height - 2; y >= 0; y--) {
  142. for (int x = 0; x < width; x++) {
  143. TB = grid[y][x];
  144. BB = grid[y+1][x];
  145. if (IS_EMPTY(BB) && (!IS_EMPTY(TB))) {
  146. grid[y+1][x] = grid[y][x];
  147. grid[y][x] = EMPTY;
  148. x--;
  149. if (y+1 != height-1) y++;
  150. else y = height-2;
  151. }
  152. }
  153. }
  154. }
  155. void SHIFTALLRIGHT() {
  156. for (int y = 0; y < height; y++) {
  157. for (int x = width - 2; x >= 0; x--) {
  158. LB = grid[y][x];
  159. RB = grid[y][x+1];
  160. if (IS_EMPTY(RB) && (!IS_EMPTY(LB))) {
  161. grid[y][x+1] = grid[y][x];
  162. grid[y][x] = EMPTY;
  163. y--;
  164. x = width - 2;
  165. }
  166. }
  167. }
  168. }
  169. bool ADDNEWCOLUMNS() {
  170. bool anyNew = 0;
  171. for (int x = 0; x < width; x++) {
  172. if (IS_EMPTY(grid[height-1][x])) {
  173. anyNew = 1;
  174. int rand = RandomNumber(height + 1, 1);
  175. int rand2;
  176. for (int y = height - 1; y >= height - (rand - 1); y--) {
  177. rand2 = RandomNumber(4, 0);
  178. grid[y][x] = rand2;
  179. }
  180. }
  181. }
  182. return anyNew;
  183. }
  184. bool CHECKFORFINISH() {
  185. bool fin = 1;
  186. for (int y = 0; y < height; y++) {
  187. for (int x = 0; x < width; x++) {
  188. if (IS_EMPTY(grid[y][x])) continue;
  189. if (!IS_EMPTY(grid[y][x+1]) && x < width - 2) if (grid[y][x] == grid[y][x+1]) return 0;
  190. if (!IS_EMPTY(grid[y][x-1]) && x > 1) if (grid[y][x] == grid[y][x-1]) return 0;
  191. if (!IS_EMPTY(grid[y+1][x]) && y < height - 2) if (grid[y][x] == grid[y+1][x]) return 0;
  192. if (!IS_EMPTY(grid[y-1][x]) && y > 1) if (grid[y][x] == grid[y-1][x]) return 0;
  193. }
  194. }
  195. return 1;
  196. }
  197. void COUNTBALLS(HWND hwnd) {
  198. int ballcount[4]; // Red, Green, Blue, Yellow
  199. for (int i = 0; i < 4; i++) ballcount[i] = 0;
  200. for (int y = 0; y < height; y++) {
  201. for (int x = 0; x < width; x++) {
  202. switch (grid[y][x]) {
  203. case RED: ballcount[0]++; break;
  204. case GREEN: ballcount[1]++; break;
  205. case BLUE: ballcount[2]++; break;
  206. case YELLOW: ballcount[3]++; break;
  207. }
  208. }
  209. }
  210. string txt;
  211. txt = "Reds:\t\t";
  212. txt += INTTOCHARP(ballcount[0]);
  213. txt += "\nGreens:\t\t";
  214. txt += INTTOCHARP(ballcount[1]);
  215. txt += "\nBlues:\t\t";
  216. txt += INTTOCHARP(ballcount[2]);
  217. txt += "\nYellows:\t\t";
  218. txt += INTTOCHARP(ballcount[3]);
  219. MessageBox(hwnd, (char *) txt.c_str(), "Ball count", MB_OK);
  220. }
  221. HBITMAP GETBALLBMP(UINT ball) {
  222. switch (ball) {
  223. case IDB_BLUE1: return BLUEBMP; break;
  224. case IDB_GREEN1: return GREENBMP; break;
  225. case IDB_RED1: return REDBMP; break;
  226. case IDB_YELLOW1: return YELLOWBMP; break;
  227. case IDB_BLUE2: return BLUEBMP_S; break;
  228. case IDB_GREEN2: return GREENBMP_S; break;
  229. case IDB_RED2: return REDBMP_S; break;
  230. case IDB_YELLOW2: return YELLOWBMP_S; break;
  231. }
  232. return NULL;
  233. }
  234. UINT GETBALLINDEX(int ball, bool selected) { // Retrieves Ball ID
  235. bi = ball ^ (selected ? SELECTED : 0);
  236. switch (bi) {
  237. case BLUE: return IDB_BLUE1; break;
  238. case GREEN: return IDB_GREEN1; break;
  239. case RED: return IDB_RED1; break;
  240. case YELLOW: return IDB_YELLOW1; break;
  241. case BLUE | SELECTED: return IDB_BLUE2; break;
  242. case GREEN | SELECTED: return IDB_GREEN2; break;
  243. case RED | SELECTED: return IDB_RED2; break;
  244. case YELLOW | SELECTED: return IDB_YELLOW2; break;
  245. }
  246. return EMPTY;
  247. }
  248.  
  249. void CLIENTRESIZE(HWND hWnd, int nWidth, int nHeight) {
  250. RECT rcClient, rcWindow;
  251. POINT ptDiff;
  252. GetClientRect(hWnd, &rcClient);
  253. GetWindowRect(hWnd, &rcWindow);
  254. ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right;
  255. ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom;
  256. MoveWindow(hWnd,rcWindow.left, rcWindow.top, nWidth + ptDiff.x, nHeight + ptDiff.y, TRUE);
  257. }
  258.  
  259. void DRAWSTATUS(HDC hdc) {
  260. _itoa_s(score, _score, 10, 10);
  261. TextOut(hdc, 10, 3 + ballSize * height, "Score:", 6);
  262. TextOut(hdc, 70, 3 + ballSize * height, _score, GETINTLENGTH(score));
  263. if (width < 11) return;
  264. _itoa_s(tempScore, _tempscore, 10, 10);
  265. TextOut(hdc, 150, 3 + ballSize * height, "Selection value:", 16);
  266. TextOut(hdc, 270, 3 + ballSize * height, _tempscore, GETINTLENGTH(tempScore));
  267. }
  268. void StartDelay(HWND hwnd, int dur, int id) {
  269. idg = 1;
  270. SetTimer(hwnd, id, dur, NULL);
  271. }
  272. void EndDelay(HWND hwnd, int id) {
  273. KillTimer(hwnd, id);
  274. }
  275. void FuncAfterDelay(HWND hwnd, void (*f)(HWND), int dur, int id) {
  276. delayFunction = f;
  277. StartDelay(hwnd, dur, id);
  278. }
  279. //
  280. int CALLBACK Dimensions(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) {
  281. UNREFERENCED_PARAMETER(lParam);
  282. switch (message) {
  283. case WM_INITDIALOG:
  284. {
  285. SetDlgItemText(hDlg, IDC_WIDTH, INTTOCHARP(width));
  286. SetDlgItemText(hDlg, IDC_HEIGHT, INTTOCHARP(height));
  287. return (INT_PTR)TRUE;
  288. }
  289. case WM_COMMAND:
  290. {
  291. switch (LOWORD(wParam)) {
  292. case IDOK:
  293. {
  294. _w = GetDlgItemInt(hDlg, IDC_WIDTH, 0, 0);
  295. _h = GetDlgItemInt(hDlg, IDC_HEIGHT, 0, 0);
  296. _w = _w > 40 ? 40 : _w < 5 ? 5 : _w;
  297. _h = _h > 30 ? 30 : _h < 5 ? 5 : _h;
  298. EndDialog(hDlg, LOWORD(wParam));
  299. return IDOK;
  300. }
  301. break;
  302. case IDCANCEL:
  303. {
  304. EndDialog(hDlg, LOWORD(wParam));
  305. return IDCANCEL;
  306. }
  307. break;
  308. }
  309. }
  310. break;
  311. }
  312. return (INT_PTR)FALSE;
  313. }
  314. void SHIFTR(HWND hwnd) {
  315. SHIFTALLRIGHT();
  316. REPAINT(hwnd);
  317. EndDelay(hwnd, ID_DELAY2);
  318. if (CHECKFORFINISH()) {
  319. MessageBox(hwnd, "Finished!", "End", MB_OK);
  320. if (MessageBox(hwnd, "Do you want to start a new game?",
  321. (char*) L"", MB_YESNO | MB_ICONWARNING) == IDYES) {
  322. CLIENTRESIZE(hwnd, width * ballSize, 23 + (height * ballSize));
  323. NEWGAME(hwnd);
  324. }
  325. }
  326. idg = 0;
  327. }
  328. void SHIFT(HWND hwnd) {
  329. SHIFTALLDOWN();
  330. if (MEGASHIFT) {
  331. while (ADDNEWCOLUMNS());
  332. REPAINT(hwnd);
  333. FuncAfterDelay(hwnd, SHIFTR, 100, ID_DELAY2);
  334. }
  335. REPAINT(hwnd);
  336. score += tempScore;
  337. tempScore = 0;
  338. if (!MEGASHIFT) idg = 0;
  339. EndDelay(hwnd, ID_DELAY);
  340. }
  341. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  342. switch (msg) {
  343. case WM_TIMER:
  344. {
  345. delayFunction(hwnd);
  346. }
  347. break;
  348. case WM_COMMAND:
  349. {
  350. switch (LOWORD(wParam)) {
  351. case ID_OPTIONS_DIMENSIONS:
  352. {
  353. int ret = (int) DialogBox(NULL, MAKEINTRESOURCE(IDD_DIMENSIONS), hwnd, Dimensions);
  354. if (ret == IDOK) {
  355. if (MessageBox(hwnd, "Do you want to start a new game?",
  356. "Warning", MB_YESNO | MB_ICONWARNING) == IDYES) {
  357. CLIENTRESIZE(hwnd, _w * ballSize, 23 + (_h * ballSize));
  358. width = _w;
  359. height = _h;
  360. NEWGAME(hwnd);
  361. }
  362. }
  363. }
  364. break;
  365. case ID_OPTIONS_MEGASHIFT:
  366. {
  367. if (MessageBox(hwnd, "Do you want to start a new game?",
  368. "Warning", MB_YESNO | MB_ICONWARNING) == IDYES) {
  369. NEWGAME(hwnd);
  370. if (!MEGASHIFT) {
  371. SetWindowText(hwnd, "SameGame - Megashift");
  372. MEGASHIFT = 1;
  373. } else {
  374. SetWindowText(hwnd, "SameGame");
  375. MEGASHIFT = 0;
  376. }
  377. }
  378. }
  379. break;
  380. case ID_GAME_NEWGAME:
  381. {
  382. NEWGAME(hwnd);
  383. }
  384. break;
  385. case ID_GAME_EXIT:
  386. {
  387. DestroyWindow(hwnd);
  388. }
  389. break;
  390. case ID_HELP_ABOUT:
  391. {
  392. MessageBox(hwnd, "William Hemsworth\n2008 March 7",
  393. "About", MB_OK | MB_ICONINFORMATION);
  394. }
  395. break;
  396. case ID_HELP_RULES:
  397. {
  398. MessageBox(hwnd,
  399. " Same Game\n\n\
  400. The aim of the game is to try and get the\n\
  401. highest score possible and to do that you\n\
  402. need to try and gather as many same coloured balls\n\
  403. together to retrieve the highest amounts of\n\
  404. points once clicked.", "Rules", MB_OK);
  405. }
  406. break;
  407. case ID_TOOLS_COUNTCOLOURS:
  408. {
  409. COUNTBALLS(hwnd);
  410. }
  411. break;
  412. }
  413. }
  414. break;
  415. case WM_KEYDOWN:
  416. {
  417. switch (LOWORD(wParam)) {
  418. case 112:
  419. {
  420. try {
  421. COUNTBALLS(hwnd);
  422. } catch (void *v) {
  423. }
  424. }
  425. break;
  426. case 113:
  427. NEWGAME(hwnd);
  428. break;
  429. case 27:
  430. DestroyWindow(hwnd);
  431. break;
  432. }
  433. }
  434. break;
  435. case WM_LBUTTONDOWN:
  436. {
  437. if (idg) break; // If in a delay
  438. bc = 0;
  439. POINT pt;
  440. GetCursorPos(&pt);
  441. ScreenToClient(hwnd, &pt);
  442. int x = pt.x / ballSize;
  443. int y = pt.y / ballSize;
  444. if (IS_EMPTY(grid[y][x])) {
  445. tempScore = 0;
  446. UNSELECTALL();
  447. REPAINT(hwnd);
  448. break;
  449. }
  450. bool alreadySelected = IS_SELECTED((int)grid[y][x]);
  451. if (!alreadySelected) {
  452. UNSELECTALL();
  453. FLOOD(x,y,(int)grid[y][x]);
  454. tempScore = ((bc*bc)+(3*bc)+4);
  455. if (bc==1) {
  456. UNSELECTALL();
  457. tempScore = 0;
  458. REPAINT(hwnd);
  459. break;
  460. }
  461. REPAINT(hwnd);
  462. }
  463. else {
  464. REMOVEALLSELECTED();
  465. PlaySound((char*)IDR_POP,NULL,SND_RESOURCE|SND_ASYNC);
  466. REPAINT(hwnd);
  467. FuncAfterDelay(hwnd, SHIFT, 100, ID_DELAY);
  468. }
  469. }
  470. break;
  471. case WM_CLOSE:
  472. {
  473. DeleteObject(BLUEBMP);
  474. DeleteObject(GREENBMP);
  475. DeleteObject(REDBMP);
  476. DeleteObject(YELLOWBMP);
  477. DeleteObject(BLUEBMP_S);
  478. DeleteObject(GREENBMP_S);
  479. DeleteObject(REDBMP_S);
  480. DeleteObject(YELLOWBMP_S);
  481. DeleteObject(pen);
  482. DeleteObject(panel);
  483. DeleteObject(hdc);
  484. DeleteObject(bmp);
  485. DeleteObject(_hdc);
  486. DeleteObject(_hdcMem);
  487. DeleteObject(hbmOld);
  488. DestroyWindow(hwnd);
  489. }
  490. break;
  491. case WM_ERASEBKGND:
  492. {
  493. hdc = GetDC(hwnd);
  494. SelectObject(hdc , pen);
  495. for (unsigned int y = 0; y < (unsigned int) height; y++) {
  496. for (unsigned int x = 0; x < (unsigned int) width; x++) {
  497. if (!IS_EMPTY(grid[y][x])) continue;
  498. Rectangle(hdc, x * ballSize, y * ballSize,
  499. (x * ballSize) + ballSize, (y * ballSize) + ballSize);
  500. }
  501. }
  502. SelectObject(hdc , panel);
  503. Rectangle(hdc, 0, height * ballSize, width * ballSize, 23 + (height * ballSize));
  504. MoveToEx(hdc, 0, height * ballSize, NULL);
  505. LineTo(hdc, ballSize * width, ballSize * height);
  506. DRAWSTATUS(hdc);
  507. }
  508. break;
  509. case WM_PAINT:
  510. {
  511. _hdc = BeginPaint(hwnd, &ps);
  512. _hdcMem = CreateCompatibleDC(_hdc);
  513.  
  514. for (unsigned int y = 0; y < (unsigned int) height; y++) {
  515. for (unsigned int x = 0; x < (unsigned int) width; x++) {
  516. if (IS_EMPTY(grid[y][x])) continue;
  517. bmp = GETBALLBMP(GETBALLINDEX(grid[y][x], 0));
  518. hbmOld = (HBITMAP) SelectObject(_hdcMem, bmp);
  519. GetObject(bmp, sizeof(bm), &bm);
  520. BitBlt(_hdc, x * ballSize, y * ballSize, bm.bmWidth, bm.bmHeight, _hdcMem,0, 0, SRCCOPY);
  521. }
  522. }
  523. SelectObject(_hdcMem, hbmOld);
  524. DeleteDC(_hdcMem);
  525. EndPaint(hwnd, &ps);
  526. }
  527. break;
  528. case WM_CREATE:
  529. {
  530. CLIENTRESIZE(hwnd, width * ballSize, 23 + (height * ballSize));
  531. NEWGAME(hwnd);
  532. }
  533. break;
  534. case WM_DESTROY:
  535. {
  536. PostQuitMessage(0);
  537. }
  538. break;
  539. default:
  540. return DefWindowProc(hwnd, msg, wParam, lParam);
  541. }
  542. return 0;
  543. }
  544.  
  545. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrev, char *cmdl, int nShowCmd) {
  546. WNDCLASSEX wc;
  547. HWND hwnd;
  548. MSG msg;
  549. wc.cbClsExtra = 0;
  550. wc.cbSize = sizeof(WNDCLASSEX);
  551. wc.cbWndExtra = 0;
  552. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  553. wc.hbrBackground = (HBRUSH) CreateSolidBrush(RGB(255,255,255));
  554. wc.hInstance = hInstance;
  555. wc.hIcon = LoadIcon(wc.hInstance, MAKEINTRESOURCE(IDI_ICON2));
  556. wc.hIconSm = LoadIcon(wc.hInstance, MAKEINTRESOURCE(IDI_ICON2));
  557. wc.lpfnWndProc = WndProc;
  558. wc.lpszClassName = cn;
  559. wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
  560. wc.style = 0;
  561. if (!RegisterClassEx(&wc)) {
  562. MessageBox(NULL, (char*)L"Error, coulden't register window class", (char*)L"Error", MB_OK);
  563. return 1;
  564. }
  565. hwnd = CreateWindowEx(WS_EX_APPWINDOW,
  566. cn,
  567. "SameGame",
  568. WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
  569. 300, 200, width * ballSize, height * ballSize,
  570. NULL, NULL, hInstance, NULL);
  571. if (hwnd == NULL) {
  572. MessageBox(NULL, (char*)L"Error, coulden't create the window", (char*)L"Error", MB_OK);
  573. return 1;
  574. }
  575. ShowWindow(hwnd, nShowCmd);
  576. UpdateWindow(hwnd);
  577. while (GetMessage(&msg, NULL, 0, 0) > 0) {
  578. TranslateMessage(&msg);
  579. DispatchMessage(&msg);
  580. }
  581. return (int) msg.wParam;
  582. }
Here is the link to download the poject.
http://www.2shared.com/file/3022171/.../SameGame.html
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,494
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: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Memory leak in game

 
0
  #2
Mar 21st, 2008
>>txt += INTTOCHARP(ballcount[0]);
In COUNTBALLS() -- INTTOCHARP() allocates memory with new but is never deleted.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,460
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 121
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Memory leak in game

 
0
  #3
Mar 21st, 2008
Thanks for finding it!, spent ages looking.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,460
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 121
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Memory leak in game

 
0
  #4
Mar 21st, 2008
Actually, that was a problem but its not the main memory leak. I think it takes place in WM_LBUTTONDOWN but I cant find exactly where abouts.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,494
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: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Memory leak in game

 
0
  #5
Mar 21st, 2008
variables _score and _tempscore -- why use new to allocate only a single character? That seems like such as misuse of dynamic memory allocation. And those two variables appear to be used without ever initializing them to anything. Is there code that you didn't post that actually sets them to something ?
Last edited by Ancient Dragon; Mar 21st, 2008 at 7:15 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,460
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 121
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Memory leak in game

 
0
  #6
Mar 21st, 2008
I used them to turn the score (int) to a char array in this function, its in the code.
void DRAWSTATUS(HDC hdc)
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Memory leak in game

 
0
  #7
Mar 21st, 2008
Because of the way how you use _itoa_s(...) and the related buffers, _score, _tempscore and val, the program runs currently on good luck.
You could change all the _itoa_s() calls to use a single array of chars of sufficient size,
i.e.
  1. static char itoa_conv_buff[MAX_BUF_LEN_HERE];
  2. _itoa_s(num, itoa_conv_buff, MAX_BUF_LEN_HERE, 10);
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,460
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 121
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Memory leak in game

 
0
  #8
Mar 21st, 2008
If I open up task manager and view the memory that SameGame is using, everytime a selection of balls are clicked, the memory usage goes up by 4-6k until it is eventually quite high. It slows down windows alot after a while.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,460
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 121
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Memory leak in game

 
0
  #9
Mar 21st, 2008
thanks mitrmkar, I did what you sead but I am still leaking memory =[
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Memory leak in game

 
0
  #10
Mar 21st, 2008
Originally Posted by williamhemswort View Post
I am still leaking memory =[
The _itoa_s() did not actually have much to do with the memory leaking, it was just a safety measure so that your program will not crash.

About the memory leaks, I think there's at least two places where it happens

1) For each SetTimer() call there should to be counterpart KillTimer() to use the timer resources properly.
2) You are not calling ReleaseDC(hwnd, hdc) in your WM_ERASEBKGND handler, i.e. every hdc you get using GetDC() is to be released by ReleaseDC(hwnd, hdc).
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC