Why is Windows pointer pThread null?

Bryan Kelly 556 Reputation points
2026-01-03T04:14:18.9366667+00:00

This gets deep into the MFC/GUI overhead and what I think of as boilerplate of MFC and a GUI project.

Began with a working project.  I added two classes, one to do my work and a second example class to see the effects of a reply in this forum and learn something about how to add simple classes to an MFC project.  This caused some completely unexpected errors about pointers.  Tried to go back to the state before adding those two classes.  For each of those four files, select the file, use tool File -> Remove -> Delete.  Delete all references to those files   Run without debug, it simply exits.  Run in Debug and get a null pointer error.  Build, get error, Clean Solution, get error.

Go into debug, stepping in, wind up int winmain.cpp line 37.  I presume the reader can find this file and presume it will be the same as mine.

Side Problem: I keep getting my posts censored. When I don't use the <> tool for code, that problem does not manifest. My apologies for this.

            if (!pThread->InitInstance())

pThread is Null.

It is set in line 25

CWinThread* pThread = AfxGetThread();

Step in to get to thrdcore.cpp line 135.  It looks like this

CWinThread* AFXAPI AfxGetThread()

{

            // check for current thread in module thread state

            AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();

            CWinThread* pThread = pState->m_pCurrentWinThread;

            return pThread;

}

pState looks ok but m_pCurrentWinthread is Null.  

Search for pCurrentWinThread.  Set a breakpoint where an “if” statement can set it to Null, line 518

Found a second place that might set it to Null, line 381, set a breakpoint

There are only two places to set m_pCurrentWinthread.  Neither one stops on the breakpoint.

Search Current project for m_ pCurrentWinthread, not found. 

OOPS, MAJOR PROBLEM

The search cannot find pCurrentWinthread, not even in the file I am looking at, even when I highlight that name the search for it.

Now I have two problems.  First, Search does not work.

Second: Did the same for pThread and Search cannot find it anywhere. What???!!!

My knowledge is this area is flat out zero.  Does anyone know what can cause these behaviors?

Closed the solution, exited Visual Studio, restart it.  Search works.  Search for  pCurrentWinthread.  Many places work on this pointer.

This is getting too long.  I hope, this is enough information for some knowledgeable in Visual Studio 2022 and in MFC.  If not, I will do more searches and breakpoints.

 

Developer technologies | Visual Studio | Debugging
{count} vote

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.