Program works only using a breakpoint in debug

This code only seems to work if I set a breakpoint where I set lClick to true.

In global.h:


1
2
3
#pragma once

extern bool lClick;



In global.cpp:


1
2
3
#include "global.h"

bool lClick = false;



In main.cpp:


1
2
3
4
5
6
7
8
9
10
11
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
	case WM_LBUTTONDOWN:
	{
		int iPosX = LOWORD(lParam);
		int iPosY = HIWORD(lParam);
		lClick = true;
		break;
	}



Finally, in Scrabble.cpp:



1
2
3
4
5
// display arrow if player left clicked somewhere on the board
	if (lClick)
	{
		DrawArrow(hdc, 0, 0);
	}



My "lClick" global variable gets set to true successfully when I set a breakpoint where it should be set to true, but not if run otherwise. Please help!
Last edited on
Are the two statements running in the same thread?
I don't know. If they are on separate threads, perhaps ensuring they are on the same thread could fix the problem. I'm using Visual Studio 2017. Maybe there's a project setting that can force single-threaded?

Unless you're talking about whether I've purposely set up multi-threading, then I have not. Here is more code in case it helps:

In main.cpp:

1
2
3
4
5
6
7
8
9
10
case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            
			// draw bitmaps
			DrawAll(hdc);

            EndPaint(hWnd, &ps);
        }


In Scrabble.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void DrawArrow(HDC hdc, int x, int y)
{
	Draw("arrowrt_mask.bmp", hdc, 110, 150, 1);
	Draw("arrowrt.bmp", hdc, 110, 150, 2);
}

void DrawAll(HDC hdc)
{
	InitPaint(hdc);

	Draw("board.bmp", hdc, 10, 50);
	Draw("racks.bmp", hdc, 770, 10);
	Draw("textarea.bmp", hdc, TEXT_AREAX, TEXT_AREAY);
	
	// display arrow if player left clicked somewhere on the board
	if (lClick)
	{
		DrawArrow(hdc, 0, 0);
	}


The goal is to draw an arrow somewhere on the Scrabble board when the user left clicks.
Last edited on
I got my answer. I needed to put

InvalidateRect(hwnd, NULL, TRUE);

in my code as the last line of WM_BUTTONDOWN
Taking care of those kind of errors is really important and it can steal a lot of your time later. If you need help dealing with those kind of problems there are programs you can use such as checkmarx and others but it's also recommended to learn how to do it on you own!
Good luck.
Ben.
Topic archived. No new replies allowed.