new&delete in different threads 
Author Message
 new&delete in different threads

Hi;

I need to pass information among threads.
Can I create something with new in one thread and delete it in another
thread.
In my case, when I delete it, the original thread has already died.  Can
I do this.
It seems that I cannot.  I get debug error on 'delete' line of code.

If I cannot 'new' in one thread and 'delete' in another thread, then how
should I pass information between threads?

My code is like this:

char * gpMsg = NULL;  //global
CRITICAL_SECTION gCriticalSection;  //global
typedef struct tagThreadMsg
{
    UINT iID;
    UINT iData;

Quote:
}THREADMSG;

UINT CMyApp::ThreadTwo()  //this is a static thread function
{
 if ( gpMsg != NULL )
 {
  delete[] gpMsg;    //I got error here, saying:
                              //Debug Error!
                              //DAMAGE: after Normal block(#84) at
0x00DB1B80
 }

THREADMSG ThreadMsg;
ThreadMsg.iID = 2;
ThreadMsg.iData = 0;

 EnterCriticalSection(&gCriticalSection); //already initialized
 gpMsg = new char (256);
 memcpy(gpMsg, &ThreadMsg, sizeof(THREADMSG));
 CString StrMsg = _T("I am in ThreadOne");
 strcpy(gpMsg+sizeof(THREADMSG), StrMsg);

 //pThreadOne is another CWinThread object
 pThread->PostThreadMessage(IDM_THREAD_MSG, (WPARAM)gpMsg, NULL);
 LeaveCriticalSection(&gCriticalSection); //already initialized

 return 0;

Quote:
}

void CMyApp::OnThreadMessage(void * spMsg)
{
 //This is in ThreadOne
EnterCriticalSection(&gCriticalSection);
THREADMSG * pMsg = (THREADMSG*)spMsg;
UINT iData = pMsg->iData;
UINT iID = pMsg->iID;
LPCTSTR pData = pMsg+sizeof(THREADMSG);
MessageBox(NULL, pData, NULL, MB_OK);  //It works OK here.
LeaveCriticalSection(&gCriticalSection);

Quote:
}

Thanks in advance.

frank



Mon, 11 Jun 2001 03:00:00 GMT  
 new&delete in different threads
Frank,

   You certainly can allocate in one thread and delete in another.  If
you're getting a debug error, its possible that you are either

a) deleting the memory block more than once

b) deleting memory that was not allocated with 'new'

c) overwriting the memory block either before or after the allocated
region.

You should be able to tell by the comments in the runtime source code
which rule you're violating, and then come up with a way to debug it.

One thing you should watch out for is if you're allocating the memory in
a DLL or EXE, and trying to delete it with code from a different module (
e.g. allocate in the DLL and delete in the EXE) -- this only works if you
are
dynamically linking the runtime library in both modules.

Les

Quote:

>Hi;

>I need to pass information among threads.
>Can I create something with new in one thread and delete it in another
>thread.
>In my case, when I delete it, the original thread has already died.  Can
>I do this.
>It seems that I cannot.  I get debug error on 'delete' line of code.

>If I cannot 'new' in one thread and 'delete' in another thread, then how
>should I pass information between threads?

>My code is like this:

>char * gpMsg = NULL;  //global
>CRITICAL_SECTION gCriticalSection;  //global
>typedef struct tagThreadMsg
>{
>    UINT iID;
>    UINT iData;
>}THREADMSG;

>UINT CMyApp::ThreadTwo()  //this is a static thread function
>{
> if ( gpMsg != NULL )
> {
>  delete[] gpMsg;    //I got error here, saying:
>                              //Debug Error!
>                              //DAMAGE: after Normal block(#84) at
>0x00DB1B80
> }

>THREADMSG ThreadMsg;
>ThreadMsg.iID = 2;
>ThreadMsg.iData = 0;

> EnterCriticalSection(&gCriticalSection); //already initialized
> gpMsg = new char (256);
> memcpy(gpMsg, &ThreadMsg, sizeof(THREADMSG));
> CString StrMsg = _T("I am in ThreadOne");
> strcpy(gpMsg+sizeof(THREADMSG), StrMsg);

> //pThreadOne is another CWinThread object
> pThread->PostThreadMessage(IDM_THREAD_MSG, (WPARAM)gpMsg, NULL);
> LeaveCriticalSection(&gCriticalSection); //already initialized

> return 0;
>}

>void CMyApp::OnThreadMessage(void * spMsg)
>{
> //This is in ThreadOne
>EnterCriticalSection(&gCriticalSection);
>THREADMSG * pMsg = (THREADMSG*)spMsg;
>UINT iData = pMsg->iData;
>UINT iID = pMsg->iID;
>LPCTSTR pData = pMsg+sizeof(THREADMSG);
>MessageBox(NULL, pData, NULL, MB_OK);  //It works OK here.
>LeaveCriticalSection(&gCriticalSection);
>}

>Thanks in advance.

>frank



Mon, 11 Jun 2001 03:00:00 GMT  
 new&delete in different threads
may be you have encountered a problem relating to dll functions..
when you make a regular dll, and link it to a client exe, the dll will have
its own heap, so you cant new memory in a dll function and delete it in
your client exe or reverse.


Quote:
>Hi;

>I need to pass information among threads.
>Can I create something with new in one thread and delete it in another
>thread.
>In my case, when I delete it, the original thread has already died.  Can
>I do this.
>It seems that I cannot.  I get debug error on 'delete' line of code.

>If I cannot 'new' in one thread and 'delete' in another thread, then how
>should I pass information between threads?

>My code is like this:

>char * gpMsg = NULL;  //global
>CRITICAL_SECTION gCriticalSection;  //global
>typedef struct tagThreadMsg
>{
>    UINT iID;
>    UINT iData;
>}THREADMSG;

>UINT CMyApp::ThreadTwo()  //this is a static thread function
>{
> if ( gpMsg != NULL )
> {
>  delete[] gpMsg;    //I got error here, saying:
>                              //Debug Error!
>                              //DAMAGE: after Normal block(#84) at
>0x00DB1B80
> }

>THREADMSG ThreadMsg;
>ThreadMsg.iID = 2;
>ThreadMsg.iData = 0;

> EnterCriticalSection(&gCriticalSection); //already initialized
> gpMsg = new char (256);
> memcpy(gpMsg, &ThreadMsg, sizeof(THREADMSG));
> CString StrMsg = _T("I am in ThreadOne");
> strcpy(gpMsg+sizeof(THREADMSG), StrMsg);

> //pThreadOne is another CWinThread object
> pThread->PostThreadMessage(IDM_THREAD_MSG, (WPARAM)gpMsg, NULL);
> LeaveCriticalSection(&gCriticalSection); //already initialized

> return 0;
>}

>void CMyApp::OnThreadMessage(void * spMsg)
>{
> //This is in ThreadOne
>EnterCriticalSection(&gCriticalSection);
>THREADMSG * pMsg = (THREADMSG*)spMsg;
>UINT iData = pMsg->iData;
>UINT iID = pMsg->iID;
>LPCTSTR pData = pMsg+sizeof(THREADMSG);
>MessageBox(NULL, pData, NULL, MB_OK);  //It works OK here.
>LeaveCriticalSection(&gCriticalSection);
>}

>Thanks in advance.

>frank



Wed, 13 Jun 2001 03:00:00 GMT  
 new&delete in different threads
In addition, make sure you are creating the thread(s) with _beginthreadex, not
with CreateThread or _beginthread.

HTH

Quote:

> I need to pass information among threads.
> Can I create something with new in one thread and delete it in another
> thread.

[...]

-- Aaron [MVP]

---------------------------------
Aaron J Margosis
LCC International

Work phone:  NOT WORKING AT THIS TIME:  703-873-2622 (703-USE-A-MAC ??!!)



Wed, 13 Jun 2001 03:00:00 GMT  
 new&delete in different threads


Fri, 19 Jun 1992 00:00:00 GMT  
 new&delete in different threads
Hi;

Thanks for your reply.

I just find out that the problem is: When I allocate buffer, I used
        char * pMsg = new char (MSG_LENGTH);
instead of
        char * pMsg = new char [MSG_LENGTH];

I think when I declared  ' new char () ', the system only allocate one byte
space.

Happy Holiday.

frank

Quote:

> Hi;

> I need to pass information among threads.
> Can I create something with new in one thread and delete it in another
> thread.
> In my case, when I delete it, the original thread has already died.  Can
> I do this.
> It seems that I cannot.  I get debug error on 'delete' line of code.

> If I cannot 'new' in one thread and 'delete' in another thread, then how
> should I pass information between threads?

> My code is like this:

> char * gpMsg = NULL;  //global
> CRITICAL_SECTION gCriticalSection;  //global
> typedef struct tagThreadMsg
> {
>     UINT iID;
>     UINT iData;
> }THREADMSG;

> UINT CMyApp::ThreadTwo()  //this is a static thread function
> {
>  if ( gpMsg != NULL )
>  {
>   delete[] gpMsg;    //I got error here, saying:
>                               //Debug Error!
>                               //DAMAGE: after Normal block(#84) at
> 0x00DB1B80
>  }

> THREADMSG ThreadMsg;
> ThreadMsg.iID = 2;
> ThreadMsg.iData = 0;

>  EnterCriticalSection(&gCriticalSection); //already initialized
>  gpMsg = new char (256);
>  memcpy(gpMsg, &ThreadMsg, sizeof(THREADMSG));
>  CString StrMsg = _T("I am in ThreadOne");
>  strcpy(gpMsg+sizeof(THREADMSG), StrMsg);

>  //pThreadOne is another CWinThread object
>  pThread->PostThreadMessage(IDM_THREAD_MSG, (WPARAM)gpMsg, NULL);
>  LeaveCriticalSection(&gCriticalSection); //already initialized

>  return 0;
> }

> void CMyApp::OnThreadMessage(void * spMsg)
> {
>  //This is in ThreadOne
> EnterCriticalSection(&gCriticalSection);
> THREADMSG * pMsg = (THREADMSG*)spMsg;
> UINT iData = pMsg->iData;
> UINT iID = pMsg->iID;
> LPCTSTR pData = pMsg+sizeof(THREADMSG);
> MessageBox(NULL, pData, NULL, MB_OK);  //It works OK here.
> LeaveCriticalSection(&gCriticalSection);
> }

> Thanks in advance.

> frank



Thu, 14 Jun 2001 03:00:00 GMT  
 new&delete in different threads


Fri, 19 Jun 1992 00:00:00 GMT  
 new&delete in different threads

Quote:

> In addition, make sure you are creating the thread(s) with _beginthreadex, not
> with CreateThread or _beginthread.

why ?


Mon, 18 Jun 2001 03:00:00 GMT  
 new&delete in different threads

Quote:
>> In addition, make sure you are creating the thread(s) with
_beginthreadex, not
>> with CreateThread or _beginthread.

The original C runtime is not thread-safe. Routines like strtok() use global
variables that either need to be made thread specific or protected from
multiple simultaneous access. CreateThread() is a language-neutral operating
system function which can't help with C thread safety issues. _beginthread()
has a small bug that can cause resource leaks.

Regards,
Will



Mon, 18 Jun 2001 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. new&delete in different threads

2. STL: new in thread A, delete in thread B

3. new/delete in different module

4. Cross-thread new/delete?

5. error when using delete & new in class

6. new & delete

7. new&delete - DLL with own heap...

8. Error Using new & delete in class

9. new & delete on arrays

10. Overloading new & delete

11. new/delete operator & multithread

12. Use of new & delete

 

 
Powered by phpBB® Forum Software