Solved

Threads and memory in MFC

Posted on 2002-06-12
13
313 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

839 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