problem deleting char* used in threads

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi all,

My solution consists of 2 projects, 1 is a dll (contains sockets
functionality, multithreaded), and the other is a wtl gui project. Both are
totally unmanaged. The gui app has a edit box, acting as a console, where I
send messages from dll to be displayed. The way I am sending messages is:
for sending the message:

void CGlobal::SendMessage(char * chptr )
{

extern HWND c_main_window_handle;
char * tmp = new char [ strlen ( chptr) +1 ];
strcpy ( tmp, chptr );
::SendMessage( c_main_window_handle, WM_DISPLAY_MSG, 200, (LPARAM)tmp);

}
(this function is somewhere inside the dll) any thread can call this class
and pass it any character array, it will create its own buffer, copy and
send its pointer to the gui thread.
The gui application has a file that has this function declared:

LRESULT proc_display_message(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam,
BOOL& /*bHandled*/)

proc_display_message receives the message and does something like this:
......
case 200:
char * ptr = (char * )lParam ;

diagtext->AppendText (ptr);
diagtext->AppendText ("\r\n");

delete [] ptr ; // this was allocated somewhere else and I plan to delete
it over here .
break;
.....
I get an error on the delete [] ptr line. It says:
-
Windows has triggered a breakpoint in OtengoGUI.exe.

This may be due to a corruption of the heap, and indicates a bug in
OtengoGUI.exe or any of the DLLs it has loaded.

The output window may have more diagnostic information
-
and the debugger stops at the following line with a green pointer arrow:
-

void InsertText(int nInsertAfterChar, LPCTSTR lpstrText, BOOL bNoScroll =
FALSE, BOOL bCanUndo = FALSE)
{
SetSel(nInsertAfterChar, nInsertAfterChar, bNoScroll);
ReplaceSel(lpstrText, bCanUndo);
}<< ----------- this line

-
Why cant I delete a pointer that I got after casting from lParam parameter.
After all the allocation for this function was done on the heap and I should
be able to delete it anywhere else, thats what I was thinking.

Regards,

-ab.

Environment: visual studio 2k5, vc++ (unmanaged) WTL, windows 2000.


.



Relevant Pages

  • Re: PostMessage() Error
    ... reason that pgnuplot should need to open wgnuplot and start simulating GUI ... the plot code in the dll, because of the "windows" output driver. ...
    (microsoft.public.vc.language)
  • Re: PostMessage() Error
    ... There's no reason that pgnuplot should need to open wgnuplot and start simulating GUI input in order to process a file from stdin. ... Note that you won't be able to perfectly separate GUI processing from the plot code in the dll, because of the "windows" output driver. ... So you'd have to have a function for setting the HWND of the plot window or something to that effect, and then disable the "windows" output driver if it hasn't been properly associated to a window. ...
    (microsoft.public.vc.language)
  • RE: problem deleting char* used in threads
    ... My solution consists of 2 projects, 1 is a dll (contains sockets ... functionality, multithreaded), and the other is a wtl gui project. ... The gui app has a edit box, acting as a console, where I ... visual studio 2k5, vc++ WTL, windows 2000. ...
    (microsoft.public.dotnet.languages.vc)
  • Fatal error LNK1256: ALINK operation failed (8013110E)
    ... I'm creating a GUI using Windows Forms in C#. ... The problem I've encountered is when I try to build the DLL. ... and the version of .NET Framework I'm using? ...
    (microsoft.public.dotnet.framework)
  • LNK1256: ALINK operation failed
    ... I'm creating a GUI using Windows Forms in C#. ... The problem I've encountered is when I try to build the DLL. ... and the version of .NET Framework I'm using? ...
    (microsoft.public.dotnet.general)