wangweitl81 / googletest

Automatically exported from code.google.com/p/googletest
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Memory Crash with std::vector<std::string> on Mac OSX 10.6 #264

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I am trying to test functions from a class that use std::vector<std::string> as 
a return type. It crashes when I test in 
GoogleTest, but the code works fine if I use a separate program to run it.

I'm not sure if the issue is related to GCC 4.2 vs. GCC 4.0, since the issue in 
my test project disappears if I switch to 
GCC 4.0, however in my actual project I'm still seeing these memory issues with 
GCC 4.0

What steps will reproduce the problem?
1. Create a std::vector<std::string> or std::vector<int> in a class function
2. Test the function in a google Test with ASSERT_TRUE(true) at the end of the 
test
3. Run in 10.6 with GCC 4.2

What is the expected output? 

The tests should run without any memory cleanup. The objects that are created 
are on the stack. GoogleTest 
appears to be trying to clean them up.

What do you see instead?

With 10.6 and GCC 4.0 I don't get any errors, but with 4.2 I get the following 
on i386.

GNU gdb 6.3.50-20050815 (Apple version gdb-1346) (Fri Sep 18 20:40:51 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys003
Loading program into debugger…
Program loaded.
run
[Switching to process 34521]
Running…
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from TestVector
[ RUN      ] TestVector.MakeVector
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
[       OK ] TestVector.MakeVector (0 ms)
[ RUN      ] TestVector.ReturnVector
hello
goodbye
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
[       OK ] TestVector.ReturnVector (0 ms)
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
[----------] 2 tests from TestVector (1 ms total)

[----------] Global test environment tear-down
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
TestGoogleTest(34521) malloc: *** error for object 0xa0818db0: pointer being 
freed was not allocated
*** set a breakpoint in malloc_error_break to debug
[==========] 2 tests from 1 test case ran. (1 ms total)
[  PASSED  ] 2 tests.

What version of Google Test are you using? On what operating system?

Building from source R. 395 on Mac OS X 10.6.2 using the Debug gtest framework.

I create a project with building for 10.6 with GCC 4.2 and it crashes 
withoutput:

Please provide any additional information below, such as a code snippet.

I've attached an Xcode test project that uses the gtest framework to test a 
simple class. If no ASSERT's are made it 
works fine, but when the ASSERTS are used to test the code it will cause a 
malloc error.

//  MyVector.h
#include <vector>
#include <string>

namespace abc {

    class MyVector {
    public:

            // Create a stack vector<string> and return a copy
        std::vector<std::string> getVector() const;

            // Create a stack vector<string>
        void makeVector();
    };
}

// MyVector.cpp
#include "MyVector.h"

namespace abc {

    std::vector<std::string> MyVector::getVector() const {
        std::vector<std::string> strVector;
        strVector.push_back("hello");
        strVector.push_back("goodbye");
        return strVector;
    }

    void MyVector::makeVector() {
        std::vector<std::string> strVector;
        strVector.push_back("hi");
        strVector.push_back("bye");
    }
}

// TestVector.cpp
#include <gtest/gtest.h>
#include "MyVector.h"

namespace abc {

        // Test the creation of a vector on the stack
    TEST(TestVector, MakeVector) {
        MyVector myVector;

        myVector.makeVector();

        ASSERT_TRUE(true);  // Crash
    }

        // Test the creation of a vector on the stack, return a copy
    TEST(TestVector, ReturnVector) {

        MyVector myVector;
        std::vector<std::string> v = myVector.getVector();

        std::vector<std::string>::iterator iter;
        for(iter = v.begin(); iter != v.end(); ++iter) {
            std::cout << *iter << std::endl;
        }

        //ASSERT_EQ(true, true);    // Crash 
        ASSERT_TRUE(true);  // Crash
    }

}

Original issue reported on code.google.com by PaulSolt on 18 Mar 2010 at 7:05

Attachments:

GoogleCodeExporter commented 9 years ago
This seems issue 189.  It doesn't seem to be caused by gtest.

Original comment by w...@google.com on 25 Mar 2010 at 6:29