Problem in deleting the dynamic memory in a parent thread that is allocated from a child thread

M

Manisha

Hi,

I am creating a C++ dll which is used to process data passed to it
through one of its exported functions. It should be able to process
160 simultaneous requests. For this reason, I have initiated 10 child
threads from the main thread and maintaining a queue of nodes
containing the data to be processed.

The queue node strucute is as follows:
struct SQueueNode
{
SData *m_pData;
bool m_bInProcess;
char* m_strReturnInfo;
SQueueNode *m_pNextNode;
};

As a node is created and added to queue, one of the free child threads
processes the data. It allocates memory for the char* m_strReturnInfo
of the queue node being processed and puts some info into that and
sets the boolean field (m_bInProcess) of the queue node structure to
false and then waits for more requests.

But when the processed node is deleted from the main thread, the
program crashes at the statement where the dynamic memory allocated
for m_strReturnInfo is deleted.

Can I not delete the dynamic memory in a parent thread that is
allocated from a child thread ?

Thx. in advance.
Manisha
 
J

John Harrison

Can I not delete the dynamic memory in a parent thread that is
allocated from a child thread ?

Who knows? Your question has nothing to do with C++ since the C++ standard
is completely silent on the subject of threads.

Try asking again on a windows programming group, for instance

John
 
A

Andy

A win 32 group is a better place for this query.

I think it is a bad idea to have memory allocation / deallocation
scattered across threads. Can you use a class like
std::basic_string<char> instead of a char*? If you have to use a char*
for some function parameters, you could use the c_str() member
function to access the char* within std::basic_string. This would save
you from memory allocation / deallocation worries.

Cheers,
Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top