L
lothar.behrens
Hi,
I have problems to delare a delete operator in a class and use it to
check
for valid pointer. Using release() with an additional validation
routine
from a separate malloc library avoids the double deletion.
The first delete will call the destructor and after it the delete
operator
will be called. It works.
At the second try, the assembler code seems to call an address at
0xBDBDBDBD.
My malloc library fills the freed buffer with 0xBD bytes.
Why does this not work ?
The same is when I define release as virtual.
Is this a compiler specific behaviour ?
I use Open Watcom 1.3.
Thanks, Lothar
class CTest {
public:
CTest() {}
virtual ~CTest() {}
void release();
static void* operator new(size_t size);
static void operator delete(void* p, size_t size);
char buf[100];
};
void CTest::release() { if (TRMemValidate(this)) delete this; }
void* CTest:perator new(size_t size) { return malloc(size); }
void CTest:perator delete(void* p, size_t size) { free(p); }
int main() {
printf("Hello world.\n");
CTest *ct = new CTest();
CTest *ct1 = ct;
ct->release();
ct1->release(); // Check works, if not virtual
ct = new CTest();
ct1 = ct;
delete ct;
delete ct1; // Crash
return 0;
}
I have problems to delare a delete operator in a class and use it to
check
for valid pointer. Using release() with an additional validation
routine
from a separate malloc library avoids the double deletion.
The first delete will call the destructor and after it the delete
operator
will be called. It works.
At the second try, the assembler code seems to call an address at
0xBDBDBDBD.
My malloc library fills the freed buffer with 0xBD bytes.
Why does this not work ?
The same is when I define release as virtual.
Is this a compiler specific behaviour ?
I use Open Watcom 1.3.
Thanks, Lothar
class CTest {
public:
CTest() {}
virtual ~CTest() {}
void release();
static void* operator new(size_t size);
static void operator delete(void* p, size_t size);
char buf[100];
};
void CTest::release() { if (TRMemValidate(this)) delete this; }
void* CTest:perator new(size_t size) { return malloc(size); }
void CTest:perator delete(void* p, size_t size) { free(p); }
int main() {
printf("Hello world.\n");
CTest *ct = new CTest();
CTest *ct1 = ct;
ct->release();
ct1->release(); // Check works, if not virtual
ct = new CTest();
ct1 = ct;
delete ct;
delete ct1; // Crash
return 0;
}