diff --git a/include/wx/defs.h b/include/wx/defs.h index 36be2abf2f..7c89f53ef7 100644 --- a/include/wx/defs.h +++ b/include/wx/defs.h @@ -611,15 +611,33 @@ typedef short int WXTYPE; #include #endif -/* delete pointer if it is not NULL and NULL it afterwards */ -/* (checking that it's !NULL before passing it to delete is just a */ -/* a question of style, because delete will do it itself anyhow, but it might */ -/* be considered as an error by some overzealous debugging implementations of */ -/* the library, so we do it ourselves) */ -#define wxDELETE(p) if ( (p) != NULL ) { delete p; p = NULL; } +#ifdef __cplusplus + // delete pointer if it is not NULL and NULL it afterwards + template + inline void wxDELETE(T*& ptr) + { + typedef char TypeIsCompleteCheck[sizeof(T)]; -/* delete an array and NULL it (see comments above) */ -#define wxDELETEA(p) if ( (p) ) { delete [] (p); p = NULL; } + if ( ptr != NULL ) + { + delete ptr; + ptr = NULL; + } + } + + // delete an array and NULL it (see comments above) + template + inline void wxDELETEA(T*& ptr) + { + typedef char TypeIsCompleteCheck[sizeof(T)]; + + if ( ptr != NULL ) + { + delete [] ptr; + ptr = NULL; + } + } +#endif /*__cplusplus*/ /* size of statically declared array */ #define WXSIZEOF(array) (sizeof(array)/sizeof(array[0])) diff --git a/tests/Makefile.in b/tests/Makefile.in index 7de6fe5e88..e10f222a21 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -71,6 +71,7 @@ TEST_OBJECTS = \ test_longlongtest.o \ test_convautotest.o \ test_mbconvtest.o \ + test_misctests.o \ test_ipc.o \ test_regextest.o \ test_wxregextest.o \ @@ -395,6 +396,9 @@ test_convautotest.o: $(srcdir)/mbconv/convautotest.cpp $(TEST_ODEP) test_mbconvtest.o: $(srcdir)/mbconv/mbconvtest.cpp $(TEST_ODEP) $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/mbconv/mbconvtest.cpp +test_misctests.o: $(srcdir)/misc/misctests.cpp $(TEST_ODEP) + $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/misc/misctests.cpp + test_ipc.o: $(srcdir)/net/ipc.cpp $(TEST_ODEP) $(CXXC) -c -o $@ $(TEST_CXXFLAGS) $(srcdir)/net/ipc.cpp diff --git a/tests/makefile.bcc b/tests/makefile.bcc index f5d622f02e..6915d58ff4 100644 --- a/tests/makefile.bcc +++ b/tests/makefile.bcc @@ -57,6 +57,7 @@ TEST_OBJECTS = \ $(OBJS)\test_longlongtest.obj \ $(OBJS)\test_convautotest.obj \ $(OBJS)\test_mbconvtest.obj \ + $(OBJS)\test_misctests.obj \ $(OBJS)\test_ipc.obj \ $(OBJS)\test_regextest.obj \ $(OBJS)\test_wxregextest.obj \ @@ -424,6 +425,9 @@ $(OBJS)\test_convautotest.obj: .\mbconv\convautotest.cpp $(OBJS)\test_mbconvtest.obj: .\mbconv\mbconvtest.cpp $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\mbconv\mbconvtest.cpp +$(OBJS)\test_misctests.obj: .\misc\misctests.cpp + $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\misc\misctests.cpp + $(OBJS)\test_ipc.obj: .\net\ipc.cpp $(CXX) -q -c -P -o$@ $(TEST_CXXFLAGS) .\net\ipc.cpp diff --git a/tests/makefile.gcc b/tests/makefile.gcc index e53f54bb2e..2d220fceff 100644 --- a/tests/makefile.gcc +++ b/tests/makefile.gcc @@ -49,6 +49,7 @@ TEST_OBJECTS = \ $(OBJS)\test_longlongtest.o \ $(OBJS)\test_convautotest.o \ $(OBJS)\test_mbconvtest.o \ + $(OBJS)\test_misctests.o \ $(OBJS)\test_ipc.o \ $(OBJS)\test_regextest.o \ $(OBJS)\test_wxregextest.o \ @@ -402,6 +403,9 @@ $(OBJS)\test_convautotest.o: ./mbconv/convautotest.cpp $(OBJS)\test_mbconvtest.o: ./mbconv/mbconvtest.cpp $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< +$(OBJS)\test_misctests.o: ./misc/misctests.cpp + $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< + $(OBJS)\test_ipc.o: ./net/ipc.cpp $(CXX) -c -o $@ $(TEST_CXXFLAGS) $(CPPDEPS) $< diff --git a/tests/makefile.vc b/tests/makefile.vc index 225bda8f92..280abafcf0 100644 --- a/tests/makefile.vc +++ b/tests/makefile.vc @@ -50,6 +50,7 @@ TEST_OBJECTS = \ $(OBJS)\test_longlongtest.obj \ $(OBJS)\test_convautotest.obj \ $(OBJS)\test_mbconvtest.obj \ + $(OBJS)\test_misctests.obj \ $(OBJS)\test_ipc.obj \ $(OBJS)\test_regextest.obj \ $(OBJS)\test_wxregextest.obj \ @@ -509,6 +510,9 @@ $(OBJS)\test_convautotest.obj: .\mbconv\convautotest.cpp $(OBJS)\test_mbconvtest.obj: .\mbconv\mbconvtest.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\mbconv\mbconvtest.cpp +$(OBJS)\test_misctests.obj: .\misc\misctests.cpp + $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\misc\misctests.cpp + $(OBJS)\test_ipc.obj: .\net\ipc.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_CXXFLAGS) .\net\ipc.cpp diff --git a/tests/makefile.wat b/tests/makefile.wat index c177331470..ece7379d06 100644 --- a/tests/makefile.wat +++ b/tests/makefile.wat @@ -262,6 +262,7 @@ TEST_OBJECTS = & $(OBJS)\test_longlongtest.obj & $(OBJS)\test_convautotest.obj & $(OBJS)\test_mbconvtest.obj & + $(OBJS)\test_misctests.obj & $(OBJS)\test_ipc.obj & $(OBJS)\test_regextest.obj & $(OBJS)\test_wxregextest.obj & @@ -455,6 +456,9 @@ $(OBJS)\test_convautotest.obj : .AUTODEPEND .\mbconv\convautotest.cpp $(OBJS)\test_mbconvtest.obj : .AUTODEPEND .\mbconv\mbconvtest.cpp $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< +$(OBJS)\test_misctests.obj : .AUTODEPEND .\misc\misctests.cpp + $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< + $(OBJS)\test_ipc.obj : .AUTODEPEND .\net\ipc.cpp $(CXX) -bt=nt -zq -fo=$^@ $(TEST_CXXFLAGS) $< diff --git a/tests/misc/misctests.cpp b/tests/misc/misctests.cpp new file mode 100644 index 0000000000..75b1b55c9d --- /dev/null +++ b/tests/misc/misctests.cpp @@ -0,0 +1,71 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: tests/misc/misctests.cpp +// Purpose: test miscellaneous stuff +// Author: Peter Most +// Created: 2008-07-10 +// RCS-ID: $Id$ +// Copyright: (c) 2008 Peter Most +/////////////////////////////////////////////////////////////////////////////// + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +#include "testprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#include "wx/defs.h" + +// ---------------------------------------------------------------------------- +// test class +// ---------------------------------------------------------------------------- + +class MiscTestCase : public CppUnit::TestCase +{ +public: + MiscTestCase() { } + +private: + CPPUNIT_TEST_SUITE( MiscTestCase ); + CPPUNIT_TEST( Delete ); + CPPUNIT_TEST_SUITE_END(); + + void Delete(); + + DECLARE_NO_COPY_CLASS(MiscTestCase) +}; + +// register in the unnamed registry so that these tests are run by default +CPPUNIT_TEST_SUITE_REGISTRATION( MiscTestCase ); + +// also include in it's own registry so that these tests can be run alone +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MiscTestCase, "MiscTestCase" ); + +void MiscTestCase::Delete() +{ + // Allocate some arbitrary memory to get a valid pointer: + long *pointer = new long; + CPPUNIT_ASSERT( pointer != NULL ); + + // Check that wxDELETE sets the pointer to NULL: + wxDELETE( pointer ); + CPPUNIT_ASSERT( pointer == NULL ); + + // Allocate some arbitrary array to get a valid pointer: + long *array = new long[ 3 ]; + CPPUNIT_ASSERT( array != NULL ); + + // Check that wxDELETEA sets the pointer to NULL: + wxDELETE( array ); + CPPUNIT_ASSERT( array == NULL ); + + // this results in compilation error, as it should +#if 0 + struct SomeUnknownStruct *p = NULL; + wxDELETE(p); +#endif +} + diff --git a/tests/test.bkl b/tests/test.bkl index 34e49912ab..0d15a28298 100644 --- a/tests/test.bkl +++ b/tests/test.bkl @@ -43,6 +43,7 @@ longlong/longlongtest.cpp mbconv/convautotest.cpp mbconv/mbconvtest.cpp + misc/misctests.cpp net/ipc.cpp regex/regextest.cpp regex/wxregextest.cpp diff --git a/tests/test_test.dsp b/tests/test_test.dsp index f7d86fa686..febddb5fa5 100644 --- a/tests/test_test.dsp +++ b/tests/test_test.dsp @@ -361,6 +361,10 @@ SOURCE=.\streams\memstream.cpp # End Source File # Begin Source File +SOURCE=.\misc\misctests.cpp +# End Source File +# Begin Source File + SOURCE=.\thread\queue.cpp # End Source File # Begin Source File diff --git a/tests/test_vc7_test.vcproj b/tests/test_vc7_test.vcproj index 63fdfa8701..ca8b6b7268 100644 --- a/tests/test_vc7_test.vcproj +++ b/tests/test_vc7_test.vcproj @@ -760,6 +760,8 @@ RelativePath=".\mbconv\mbconvtest.cpp"/> + +