Solved

Threads and memory in MFC

Posted on 2002-06-12
13
312 Views
Last Modified: 2013-12-14
I have a standard queue and a flush queue thread.  I have a CConnection object, and a CMessage object.

The flush queue thread pops the queue and then creates a SendMessageThread.

struct SEND_PARAMS
{
   CConnection* pConnection;
   CMessage* pMessage;
} SEND_PARAMS;

static void SendMessageThread(void* p)
{
    SEND_PARAMS pSp = (SEND_PARAMS*) p;
    CConnection* pConn = pSp->pConnection;
    CMessage* pMess = pSp->pMessage;

    // I would do send the message here
    // BUT, pMessage is an points to garbage memory

    // delete pMess; (throws an exception)
    // delete pSp; (throws an exception)
}

static void FlushQueue(void* p)
{
   while (bNotDone)
   {
       message = queue.top();

       SEND_PARAMS* pSp = new SEND_PARAMS;
       pSp->pConnection = conn;
       pSp->pMessage = new CMessage("stuff");

       HANDLE hThread = (HANDLE) _beginthread(SendMessageThread, 0, (void*) &pSp);

       queue.pop();
   }
}


The problem is that by the time the SendMessageThread function runs, the memory has already be de-allocated by the program. This behavior is only happening when using MFC.  In a normal Win32 app (non MFC) this does not happen, ie, the memory allocated in the loop for the SEND_PARAMS is not deleted until I puposefully delete it in the SendMessageThread.

So I can tell MFC has built in garbage collection.  But I need to get these params into the thread on the fly.  I don't want that memory deleted until I say so.  At this point taking MFC out of the app is not possible.  Is there any way to get the memory to 'stick'?

If you need more clarification, let me know.  Thanks, Chase707
0
Comment
Question by:Chase707
  • 6
  • 5
  • 2
13 Comments
 
LVL 4

Author Comment

by:Chase707
ID: 7074107
Sorry, in FlushQueue

pSp->pMessage = new CMessage("stuff");
should be
pSp->pMessage = new CMessage(message);

Chase707
0
 
LVL 4

Author Comment

by:Chase707
ID: 7074108
Also, the CConnection and CMessage class do not use any MFC classes inside of them, so that is not the problem.

I know that you can't send pointers to MFC classes across thread without problems.

Chase707
0
 
LVL 86

Expert Comment

by:jkr
ID: 7074109
I guess that "conn" and "message" are related to the queue - what kind of queue is this? As you say that you are haveing problems with these objects and they are sored in a queue, the "pop()" operation might invaidate them...
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 86

Expert Comment

by:jkr
ID: 7074112
>>Also, the CConnection and CMessage class do not use any MFC classes inside of them

That is another pointer to the queue...
0
 
LVL 22

Expert Comment

by:nietod
ID: 7074119
Typically the way parameters are passed to a new thread are via the heap..  In other words, the 1st thread allocates the new parameters in a structure using operator new.   Then a pointer to this structure is passed to the new thread.  The new thread then is responsible for deleting the structure when it no longer needs the parameters.

There are other methods for doing this, but this tends to be the easiest and often the best.
0
 
LVL 4

Author Comment

by:Chase707
ID: 7074124
No, that is not the problem, as you can see, the following creates a copy of the Message class (copying every data member expicitly), before passing it in as a param to the SendMessageThread function.
pSp->pMessage = new CMessage(message);

The CConnection class is always valid for the life of the program.

Chase707
0
 
LVL 4

Author Comment

by:Chase707
ID: 7074126
neitod, that's exactly what i'm doing, isn't it?

Chase707
0
 
LVL 22

Expert Comment

by:nietod
ID: 7074128
Wait a second, that looks liek what you are doing at least in part.   Except why not with the connection object?  The other objects should be fine, but you say they are not.  Are you sure?

0
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 7074136
AH.

You are passing a POINTER TO A POINTER to you send parameters
0
 
LVL 22

Expert Comment

by:nietod
ID: 7074141
>>    HANDLE hThread = (HANDLE) _beginthread(SendMessageThread, 0, (void*) &pSp);

should be

   HANDLE hThread = (HANDLE) _beginthread(SendMessageThread, 0, (void*) pSp);

Then it should work.

except I am a little concerned about the connection object.  That should be dynamcially allocated too (probably).   Can you post a little more of the code?
0
 
LVL 4

Author Comment

by:Chase707
ID: 7074142
argh....that's it exactly.  Sometimes it takes someone elses eye to see it.

Thanks.
0
 
LVL 4

Author Comment

by:Chase707
ID: 7074145
The connection object is actually located within a static list of CConnection objects, and is retrieved dynamically before the call to SendMessageThread.  So that's OK.

Thanks again.
0
 
LVL 22

Expert Comment

by:nietod
ID: 7074160
>> Sometimes it takes someone elses eye to see it.
It took my eyes a long while to adjust.   I'm out of pratice at reading other people's code.   Its much harder than reading one's own code.

thanks.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question