?
Solved

Threads and memory in MFC

Posted on 2002-06-12
13
Medium Priority
?
317 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 800 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

764 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