Subj : Re: Multi-Thread and memory allocation error (or bug in 5.02). To : borland.public.cpp.borlandcpp From : "Martin Catney" Date : Mon Sep 01 2003 08:18 pm Hi Ed, Thanks for all your suggestions. I will go through each of them. > you mean that you still did the allocation and de-allocation of the arrays. I just removed the thread creation and deletion, the array were still allocated and deallocated. > I am curious. Why do you do this: > > for (int j=0; j < mLength ; j++){ > array = thearray[j]; > delete[] array; > } I used array only to test what thearray[j] was and by looking at the 4 byte preceding it to see what the compiler thought the size of the deallocation should be. I also tried testing the return codes for the 3 functions you mentioned but they were all fine. Increasing the stack size did not help either. I delayed the thread termination and the array deletion using Sleep and by switching on the CodeGuard but still the same problem. I added a delete function to the class to delete the arrays outside the destructor. By calling the delete function for each instance before deleting any of the class instances still caused the program to crash. As an alternative we will look at using Builder 6 to generate the production code as a DLL which we will link into our Borland C++ application. However, the change in the name mangling of C++ functions means that we will have to develop a C interface which is a bit of a pain. If you have any other suggestion I would still be glad to hear them. Thanks for the help Martin "Ed Mulroy [TeamB]" wrote in message news:3f4fbc69$1@newsgroups.borland.com... > I see nothing obviously wrong in the code and give no guarantees as to if my > ideas will solve the problem. I also found the code did generated no > problem under BCB6 (XP Pro, 512M, PIII Mobile). > > I assume that by this: > > > If the creation and termination of the threads > > removed the example runs fine. > > you mean that you still did the allocation and de-allocation of the arrays. > If not then the test without threads didn't proove much since the code which > the OS complains of does not execute. > > > If it were my code, I try a couple of things. > > Increase the stack size to something like 16384, comment out creation of all > but 2 of the 9 image lists (list2 through list7, list8 is already commented > out) and put in code to check the return values from _beginthreadNT and > SetThreadPriority. > > If I were getting very frustrated I'd probably also code to detect if > WaitForSingleObject(mThread, INFINITE); returned WAIT_ABANDONED. > > Obviously some of the changes I mentioned may be difficult for the > production code. My goal in suggesting this is to find the problem or at > least generate a symptom related to a change so that the problem source can > be identified. > > The RTL has undergone many changes in the 6 years since BC++ 5.02 was new. > If it still does not yield to debugging then I suggest you try one of these > two things. (Note that I've not used BC++ 5.02 for anything multi-threaded > in years, so consider the source.) > > Set up a main thread (main program thread) delete function. > Call that routine with the address of the int** array from the class. > Let that function do the array deletion of the int** array and the int* > arrays it contains instead of deleting in the destructor. > Somehow arrange for a slight delay between when the thread ends and the > deletion. > > or > > Create a class which is essentially a container for the int** array and the > int* arrays it contains. That class should do the allocation and deletion. > Use an auto_pointer of that class as a member variable in the TVMImageList > class. > > Or you might try both (but of course not together). > > > I am curious. Why do you do this: > > for (int j=0; j < mLength ; j++){ > array = thearray[j]; > delete[] array; > } > > Instead of this: > > for (int j=0; j < mLength ; j++) > delete[] thearray[j]; > > . Ed > > > > Martin Catney wrote in message > > news:3f4f7adf$1@newsgroups.borland.com... > > > > I am experience some problems with Borland 5.02 with > > multitheads and memory allocation and deallocation. > > The same code on Borland Builder 6 appears to work > > fine. I have included an example program which creates > > multiple instances of the same class then deletes them. > > The class constructor creates a new thread and allocates > > a large block of memory while the destructor > > deallocates the memory and terminates the thread. > > > > I get an access violation at 0x634b5e: read of address > > 0x4cdc058 when I run the example. This corresponds to > > the line delete[] array; in the class destructor. If the > > creation and termination of the threads is removed the > > example runs fine. > > > > Does anyone spot an obvious error in my example or is this a bug in the > > Run-Time Library? > > .