From: semi@cc.tut.fi (Malinen Semi)
Subject: Re: OWL & transfer to Combobox
Organization: Tampere University of Technology, Finland
Distribution: comp
Date: Tue, 15 Dec 92 14:29:56 GMT


In <HAGELIN.92Dec14141029@elixir.elixir.e.kth.se> hagelin@elixir.e.kth.se (Martin Hagelin) writes:



>I have a problem with OWL and transfer to/from Comboboxes in a dialog.
>I try to display a combobox in a dialog and fill it with information
>but somehow the Gods aren't with me. I'm running BC++ v3.1 with OWL.



>The code looks like this:

	(Code deleted)

>Because I create a TComboBoxData with new, I must delete it
>afterwards. But if I do this, everything crashes the second time I run
>DoTheBox(), and if I omit the delete statement everything works fine
>for maybe 10 or 20 times then it crashes (probably because more and
>memory is taken by the TComboBoxData:s).

>The manual is not very informative about transfering and especially
>not to/from comboboxes. Am I doing this totally backwards ?

No, you are doing it just right.

I can't see anything wrong with your code. I also had a very
similar problem with a program I am currently writing. Fortunately
I managed to be able to fix it after some research.

As it turns out there seems to be a bug in OWL's code that
handles transfer to and from comboboxes. If you are using the same
subversion of BC++ 3.1 as I am then you can fix the problem by
patching file COMBOBOX.CPP in .../OWL/SOURCE directory and recompiling
OWL.

You can find the bug on lines 151 to 153 in COMBOBOX.CPP. On line
151 (in Transfer method of TComboBox) OWL asks for the length of the
currently selected string by sending the combobox a CB_GETLBTEXTLEN
message. The value is placed into a variable called StringSize.
New is then used to allocate StringSize bytes for the selection
string on line 152. On line 153 the selection string is copied from
the combobox to the newly allocated vector. Copying is done by sending
the combobox a CB_GETLBTEXT message with the address of the buffer as
lParam.

The problem is that the terminating zero of the selection string is
written past the string over some valuable data. 

According to the manuals message CB_GETLBTEXTLEN returns the length
of the selection string, in bytes, *excluding* the terminating zero.
When OWL allocates space for the selection string it does not take
the terminating zero into account. Again according to the manuals
the message CB_GETLBTEXT causes the string, *including* the terminating
zero, to be copied to the address pointed by lParam. What happens is
that the terminating zero is probably written over some bookkeeping
information that new and delete use.

I had to recompile OWL with debugging information included and run my
program with debugger before I could convince myself that Borland
had left such a trivial error in their code. It seems to me that
Borland has not tested OWL very well, because this bug is something
that you *will* run into if you use TComboBox class for anything
serious.

You can kill the bug by simply increasing StringSize by one before
it is used. I just made the line 152 of COMBOBOX.CPP to read:

	ComboBoxData->Selection = new char[StringSize+1];

After recompiling OWL and relinking my program the problem disappeared
and my dialog box ran fine.

Hope this helps!

>/Martin

>--
>--------
>Martin Hagelin
>hagelin@elixir.e.kth.se


		Semi
		semi@cc.tut.fi