Crash on Test Application Shutdown

311 views
Skip to first unread message

sbpont

unread,
Jun 1, 2010, 11:26:32 AM6/1/10
to Google C++ Testing Framework
I would like to embed tests (using TEST and TEST_F) into various DLLs
and then have a single test application which will load any DLL and
run its embedded tests. The DLLs are not known to the test
application at build time. I am experiencing a crash when the test
application exits. I am using gtest 1.5.0, Windows XP, and Visual
Studio 2008.

Here is some stripped-down code for the test application:

int main(int argc, char* argv[])
{
HMODULE hDLL = ::LoadLibrary("MyDLL.dll");

::testing::InitGoogleTest(&argc, argv);
int nResult = RUN_ALL_TESTS();
::FreeLibrary(hDLL);

return nResult;
}

The test application works correctly in loading the DLL and executing
the tests. However, if I free the library before exiting the main()
function, I get a crash down in the bowels of the C runtime library.

gtest is being built as a DLL. The gtest DLL, the DLL with the test
code, and the test application are all being compiled using the Multi-
threaded DLL runtime libraries (/MD or /MDd).

I was wondering if the gtest framework was trying to access the test
objects when it was being unloaded at application shutdown. If I
unload the test DLL before this happens, then obviously, there will be
trouble. If I do not unload the test DLL, then the application shuts
down correctly.

Vlad Losev

unread,
Jun 1, 2010, 1:01:21 PM6/1/10
to Google C++ Testing Framework
gtest doesn't support this use case. The tests themselves some objects during their initialization, and those objects are cleaned up by gtest during the program shutdown. If the code that performs the clean-up is already unloaded from memory by that time, the program will crash. Unless you want to modify Google Test's code to suppress that clean-up, your only option is to keep those DLLs loaded until the program terminates. But there is another catch: we do not support invoking RUN_ALL_TESTS() more than once per program invocation. Programs that do it may work now but may break with a future release of Google Test.

With all that in mind, I suggest building your tests into .exe files instead of .dlls and executing them in a subprocess via a call to system() or CreateProcess().

- Vlad

andreyb...@gmail.com

unread,
Mar 15, 2018, 12:08:44 PM3/15/18
to Google C++ Testing Framework
Hi Vlad, Others,

Sorry for resurrecting a necro-thread here.

The project I'm working on faced a similar issue (we unload DLLs (containing individual tests) manually, and Google Test used to crash at the application's exit).

Our solution is to add the following to gtest.h:

inline void CLEAN_TESTS() {
  ::testing::UnitTest::GetInstance()->Clean();
}

and the following to gtest.cc:

void UnitTest::Clean() {
  delete impl_;
  impl_ = new internal::UnitTestImpl(this);
}

and then call CLEAN_TESTS() before unloading any DLLs.

An unfortunate consequence is that we have to fork and modify GoogleTest's sources -- something we don't want to do.

This was many moons ago (we used gtest1.6, compiled with VS2010); fast forward to today (we use gtest1.8, compiled with VS2013). Now I don't see this problem anymore! -- even after I removed our solution.

Does it mean this use case is supported now?

Yours,
Andrey

вторник, 1 июня 2010 г., 20:01:21 UTC+3 пользователь Vlad Losev написал:

Andrey Bokhanko

unread,
Mar 16, 2018, 8:15:40 AM3/16/18
to Google C++ Testing Framework
An update: the issue is still reproducible -- GoogleTest still uses a static variable to track all tests and cleans them upon an application's exit. I just got lucky when running our app from a command line (but not from VS debugger).

This makes me wonder if our solution (present, in its entirety, in my previous message) would be acceptable for upstreaming? It enables this use case.

Yours,
Andrey


--

---
You received this message because you are subscribed to a topic in the Google Groups "Google C++ Testing Framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/googletestframework/A9UXUrbKMD8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to googletestframework+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages