test object not retaining the value after being passed by the constructor in google test
<p>I am using google test/mock framework for unit testing. I call the constructor of the base class I am testing in my SetUp function. I set certain private members of the class using the object generated in SetUp to modify my test's behavior. When my test function invokes the base function that I am testing, the address of the private member variables change and therefore the tests seg fault. I need to figure out the cause of such behavior since it doesn't happen with another similar file that I am testing.</p>

<pre><code>//Class to test
//base code

//header file
class To_Test
{

friend My_test_class;

private:
TestStruct* sptr;  //pointer to a structure, set by some random function elsewhere

public:
To_Test();
~To_Test();
boolean Function_1();

}

//cpp file

To_Test::To_Test()
{
sptr = NULL;
}

boolean To_Test::Function_1()
{
boolean variable;
variable = sptr-&gt;bool;
if (variable)
            {
            do something
            return TRUE;
            }
return FALSE;
}

//Test framework

//test class header file
#include "To_Test.h"

class My_test_class : public :: testing :: Test
{
public:

To_Test *ToTestObj;
virtual void SetUp();

void Test_Function_1();

}

//gtest.cpp file
My_test_class::SetUp()
{
ToTestObj = New To_test;
}

My_test_class::Test_Function_1()
{
ToTestObject-&gt;sptr = (RandomStruct*) malloc (sizeof(RandomStruct));
sptr-&gt;bool = TRUE;
ASSERT_TRUE(TRUE = ToTestObject-&gt;Function_1());
}
</code></pre>

<p>The address of ToTestObject in SetUp, in Test_Function_1 and Function_1 is the same.
However, the address of sptr in SetUp and Test_Function_1 is different from that of Function_1. Therefore, when I step through Function_1 when test is executing, sptr has no memory as it points to NULL and the execution fails when it tries to access the memory at sptr->bool.</p>

<p>I am not sure what is causing this issue. Any help is really appreciated!</p>
