Bug 114843 - Test runner crashes on exit when executing a CppUnit::TestCase
Summary: Test runner crashes on exit when executing a CppUnit::TestCase
Status: RESOLVED NOTABUG
Alias: None
Product: cppunit
Classification: Unclassified
Component: General (show other bugs)
Version:
(earliest affected)
1.13
Hardware: x86 (IA32) Linux (All)
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-01-05 07:31 UTC by Sander
Modified: 2020-12-03 11:30 UTC (History)
2 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sander 2018-01-05 07:31:17 UTC
Hello,

I am using libcppunit-dev version 1.13.2-2.1 on Ubuntu 16.04 Linux, 32 bit. When running a CppUnit::TestCase, the application which runs the test crashes on exit. Creating a very simple test runner:

#include <cstdlib>
#include <iostream>

#include <cppunit/ui/text/TextTestRunner.h>
#include <cppunit/TestResult.h>
#include <cppunit/TextOutputter.h>
#include <cppunit/TestResultCollector.h>

#include "SampleTestCase.h"

int main(int, char**)
{  
  SampleTestCase testCase("My test");
  
  CppUnit::TestResultCollector collector;
  CppUnit::TextOutputter outputter(&collector, std::cout);
  CppUnit::TestResult testResult;
  CppUnit::TextTestRunner runner;
  
  std::cout << testCase.getName() << std::endl;
    
  runner.addTest(&testCase);    
  return (runner.run() ? EXIT_SUCCESS : EXIT_FAILURE);
}

where SampleTestCase is a very simple class which always succeededs:

#include <cppunit/TestCase.h>

class SampleTestCase : public CppUnit::TestCase
{
public:
  explicit SampleTestCase(const std::string& testName);
  SampleTestCase(const SampleTestCase& src) = delete;
  virtual ~SampleTestCase();
  
  void runTest();

};

When running, a crash occurs eventually on destructing this test case:

My test
.


OK (1 tests)


=================================================================
==4128==ERROR: AddressSanitizer: new-delete-type-mismatch on 0xbfd58200 in thread T0:
  object passed to delete has wrong type:
  size of the allocated type:   0 bytes;
  size of the deallocated type: 32 bytes.
    #0 0xb72158e4 in operator delete(void*, unsigned int) (/usr/lib/i386-linux-gnu/libasan.so.2+0x988e4)
    #1 0x8049ef4 in SampleTestCase::~SampleTestCase() /home/sander/NetBeansProjects/Pilot/SampleTestCase.cc:38
    #2 0xb7167b99 in CppUnit::TestSuite::deleteContents() (/usr/lib/i386-linux-gnu/libcppunit-1.13.so.0+0x25b99)
    #3 0xb7167be7 in CppUnit::TestSuite::~TestSuite() (/usr/lib/i386-linux-gnu/libcppunit-1.13.so.0+0x25be7)
    #4 0xb7167824 in CppUnit::TestRunner::WrappingSuite::~WrappingSuite() (/usr/lib/i386-linux-gnu/libcppunit-1.13.so.0+0x25824)
    #5 0xb71672ac in CppUnit::TestRunner::~TestRunner() (/usr/lib/i386-linux-gnu/libcppunit-1.13.so.0+0x252ac)
    #6 0xb7169593 in CppUnit::TextTestRunner::~TextTestRunner() (/usr/lib/i386-linux-gnu/libcppunit-1.13.so.0+0x27593)
    #7 0x804b32f in main /home/sander/NetBeansProjects/Pilot/main.cpp:19
    #8 0xb6962636 in __libc_start_main (/lib/i386-linux-gnu/libc.so.6+0x18636)
    #9 0x8049a30  (/home/sander/NetBeansProjects/Pilot/dist/Debug/GNU-Linux/pilot+0x8049a30)

AddressSanitizer can not describe address in more detail (wild memory access suspected).
SUMMARY: AddressSanitizer: new-delete-type-mismatch ??:0 operator delete(void*, unsigned int)
==4128==HINT: if you don't care about these warnings you may set ASAN_OPTIONS=new_delete_type_mismatch=0
==4128==ABORTING
Comment 1 Sander 2018-01-20 14:17:08 UTC
Hello,

I discovered from documentation that the testrunner owns the test case and will always call delete(). The problem can be circumventended by allocating a TestCase with new();
Comment 2 Xisco Faulí 2018-01-21 17:26:15 UTC
I guess we can close this as RESOLVED NOTABUG then. Thanks
Comment 3 Markus Mohrhard 2018-01-21 19:59:44 UTC
As mentioned this not a CppUnit bug. The test runner is managing the life cycle of the test cases and therefore you need to make sure that you are not deleting the test case yourself. In case of stack allocated objects you can't avoid deleting the object yourself so you can not use them.
Comment 4 Finch. A 2020-12-03 11:30:35 UTC
I found this info very helpful. I have earlier asked a question regarding this. Now its very clear as to what to do.

https://www.thatchfinder.com/directory/19676/sunnyvale-concrete-pros/