Delphi4 string handling finally catches up with me.

From: Martin Harvey (Demon account) (martin_at_nospam_pergolesi.demon.co.uk)
Date: 12/17/04


Date: Fri, 17 Dec 2004 18:48:16 GMT


Hey folks,

Beautifully obscure bug here, and I have a fix that I *think* will
work, but just want to check.

Multithreaded app - normally at least 6 or 7 threads running at any
time.
Never seen an error on my machine. Run it on a 4xCPU (2x xeon
hyperthreaded box) at work, and it works ... for about a day, and then
dies horribly - one of the threads raises an exception and quits.

Looked into it with the meager debug information I had with me: it
ends up dying in SysAllocMem. All the hallmarks of heap corruption.

Now the one thing I'm *sure* of is that my memory allocation is OK -
In the debug build, I actually log *every* memory allocation in a big
AVL tree that I can then use to check for leaks, and also display an
estimate of memory fragmentation.

So, after eliminating all other causes I can think of, this looks very
much like Delphi 4 string handling. Typically, strings aren't shared,
except in cases where "file descriptors" are passed between one thread
and another. These happen to contain a string: a relative pathname.

So anyways, I now have a scheme to avoid this based on "UniqueString".

Basically, resources always get passed from one thread to another via
a buffer (actually a queue). My plan is that at the point where a
resource is passed into the buffer by the thread that originally
created the string, then I call "UniqueString" so ensure that there
are no references shared across threads.

Sounds OK?

Note that it's probably a BAD idea to call UniqueString when taking
strings out of buffers, because the original "putting in" thread will
have definitely left the critical section, and is probably in an
indeterminate state.

MH.



Relevant Pages

  • Re: Discovering variable types...
    ... >- but I suppose MS expect us to use wrappers ... memory allocations for your variables from disk as well. ... >They most certainly are of fixed size, changing the size of a String ... >>me to keep buffer size and current postion right in the memory block. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Secure C library
    ... I read much of the new "security TR", and gee, I don't know. ... the buffer from the buffer size. ... It is not hard to design a better form of buffer and string handling. ... but this is just one example of how thoughtful interface design can ...
    (comp.std.c)
  • Re: Secure C library
    ... >> string functions don't make much sense once you add bounds-checking ... >> designing an interface just for the purpose of reducing the frequency ... > make buffer size decisions more visible, ... Bstrlib is also very interoperable with char *'s, ...
    (comp.std.c)
  • Re: why I can not write to the file after initialize the MFC in a service program
    ... you check EVERY return from a call that can fail, ... Why do you need an intermedate buffer to write literal strings anyway? ... For example, if AfxWinInit fails, you copy a 45-character string into a ... So you are going to try to initialize MFC EACH TIME THROUGH THE LOOP? ...
    (microsoft.public.vc.mfc)
  • Re: Calling dll functions from vb.net with pointer returns!
    ... (ByRef pulLen As Integer, ByVal pszFilter As String, ByVal ulFlags As ... OUT PTCHAR Buffer, ... Address of a buffer to receive a set of NULL-terminated device instance ... pszFilter must specify the name of a device ...
    (microsoft.public.dotnet.languages.vb)