Board index » cppbuilder » Multi-Thread and memory allocation error (or bug in 5.02).
Martin Catney
![]() CBuilder Developer |
Multi-Thread and memory allocation error (or bug in 5.02).2003-08-30 12:10:07 AM cppbuilder79 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? Regards Martin #include <windows.h> #include <process.h> //#include <iostream.h> //#define ASIZE 159727 #define ASIZE 159727 void _USERENTRY ThreadFunction(HANDLE hEvent) { WaitForSingleObject(hEvent, INFINITE); } class TVMImageList { public: TVMImageList(unsigned long noImages, unsigned long size); ~TVMImageList(); public: unsigned long mSize; unsigned long mLength; int* array; int** thearray; HANDLE hEvent; HANDLE mThread; unsigned long mThreadID; }; TVMImageList::TVMImageList(unsigned long noImages, unsigned long size) :mSize(size), mLength(noImages) { hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); mThread = (HANDLE)_beginthreadNT( ThreadFunction, // pointer to thread function 4096, // stack size hEvent, // argument for new thread NULL, 0, &mThreadID ); SetThreadPriority((void*)mThread,THREAD_PRIORITY_NORMAL); thearray = new int*[mLength]; for (int i =0; i<mLength;i++) thearray[i] = new int[size]; for (int j=0; j < mLength ; j++){ array = thearray[j]; for (int i=0;i<mSize;i++) array[i]=i; } } TVMImageList::~TVMImageList() { SetEvent(hEvent); WaitForSingleObject(mThread, INFINITE); CloseHandle(hEvent); for (int j=0; j < mLength ; j++){ array = thearray[j]; for (int i=0;i<mSize;i++) if(array[i]!=i){}; // cout << "Error"; } for (int j=0; j < mLength ; j++){ array = thearray[j]; delete[] array; } delete[] thearray; } void main() { TVMImageList* list = new TVMImageList(10, ASIZE); TVMImageList* list1 = new TVMImageList(10, ASIZE); TVMImageList* list2 = new TVMImageList(10, ASIZE); TVMImageList* list3 = new TVMImageList(10, ASIZE); TVMImageList* list4 = new TVMImageList(10, ASIZE); TVMImageList* list5 = new TVMImageList(10, ASIZE); TVMImageList* list6 = new TVMImageList(10, ASIZE); TVMImageList* list7 = new TVMImageList(10, ASIZE); delete list7; delete list6; delete list5; delete list4; delete list3; delete list2; delete list1; delete list; // delete list8; } |