Hi, Wilfried - thanks for the explanation - I've been looking at the code for hours and just couldn't see the cause. I've now changed the code to this which should solve the problem if I understood correctly:
function ReadMessage: PAnsiChar; stdcall; var Note: AnsiString; begin EnterCriticalSection(CritSectn); if (NotesList.Count > 0) then begin Note := NotesList.Strings[0]; NotesList.Delete(0); Result := PAnsiChar(Note); end else Result := ''; LeaveCriticalSection(CritSectn); end; Regarding the critical section issue, there are 2 threads involved ... the main DLL thread which services the calls from the client application and the worker thread which is running the TWSocket message loop. As I saw things, there were two areas that could be problematic: 1) the Readmessage function above which is checking the list count and deleting the string after reading it. 2) the worker thread that would be trying to add strings to the end of a list which might be 1 string shorter at the end of the process than it was at the beginning i.e. the string count is 5 as the worker thread prepares to add NotesList.Strings[5] but before that task has completed, the main thread has deleted NotesList.Strings[0] so the count is then only 4 Is that a possibility or is the Strings class smart / thread-safe enough to prevent those situations causing an issue? Regards, Adam -----Original Message----- From: twsocket-boun...@elists.org [mailto:twsocket-boun...@elists.org] On Behalf Of Wilfried Mestdagh Sent: 15 September 2011 22:34 To: 'ICS support mailing' Subject: Re: [twsocket] DLL implementation advice (or example)... Hi Adam, > despite tearing out some hair and losing a Hopefully you have spare :) > Result := PAnsiChar(NotesList.Strings[0]); > NotesList.Delete(0); The problem is that you give a pointer to the return value of that function to something that not exists anymore after the functions exit: > Result := PAnsiChar(NotesList.Strings[0]); You give a pointer to a string > NotesList.Delete(0); And here you destroy the string you point to > critical section code or not... I thought I might as the worker thread You only need Critical section if you access same data from out different thread context. So if this is not the case then you don't need it. Was this clear to answer your questions? -- mvg, Wilfried http://www.mestdagh.biz http://www.comfortsoftware.be http://www.expertsoftware.be -- To unsubscribe or change your settings for TWSocket mailing list please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be -- To unsubscribe or change your settings for TWSocket mailing list please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be