I'm changing some code to deal with a change in the signature of some functions which are being called by it. The original code looks something like (this is only illustrative):
int main()
{
TestClassA instance;
instance.MethodA();
instance.MethodB();
// do other stuff with instance
}
class TestClassA
{
void MethodA ()
{
UList* aList = NULL;
getListOfStuff(aList);
// do some stuff
fList = aList;
}
void MethodB ()
{
//do something with fList
}
UList* fList;
}
struct Ulist
{
int value;
std::vector<Ulist> components;
}
getListOfStuff(UList* aList)
{
aList = new UList();
PopulateOriginal(aList);
}
The signature of getListOfStuff has now changed (the change is beyond my control) to:
getListOfStuff(UList& aList)
{
aList=PopulateNewVersion();
}
Seems I wasn't clear about what I was asking, so to clarify...what I am asking is how to call the new version of the method getListOfStuff and safely store the returned data from it (the aList parameter) in a raw pointer field fList, which can then be accessed from MethodB later on.
Following on from #GuillaumeGris answer below, would the following work? (The bit I'm unsure about is whether the assignment within the new getListOfStuff affects whether fList is still pointing to valid data after MethodA has exited.)
void MethodA ()
{
UList* aList = new UList();
getListOfStuff(*aList);
// do some stuff
fList = aList;
}
You don't need an explicit heap allocation (new).
class TestClassA
{
void MethodA ()
{
// Clear components
fList.components.clear();
getListOfStuff(fList);
}
UList fList;
}
If fList is optional, I would recommend the use of a std::unique_ptr
class TestClassA
{
void MethodA ()
{
if (!fList)
fList = std::make_unique<Ulist>();
else
{
// Clear components
fList->components.clear();
}
getListOfStuff(*fList);
}
std::unique_ptr<UList> fList;
}
Related
Is there a way to match the address of a given parameter in an EXPECT_CALL?
I have code like the following:
EXPECT_CALL(mock1, GetTheData()).WillOnce(Return(theData));
EXPECT_CALL(mock2, SetTheData(_)); // How to check the parameter is the same object as the one returned by GetTheData
// the following was tried but does not work
EXPECT_CALL(mock2, SetTheData(_)).WillOnce([&theData](auto param){ EXPECT_EQ(&theData, param) })
But because the SetTheData function takes its argument by value, the address is different. So I would need to find a way to get the object, before it was passed to the SetTheData function.
I tried some stuff with matches, but that did not seem to work either.
Is this possible at all? If so, how? And if not, why not?
EDIT:
As requested here is a more complete example to give more context.
struct TheData
{
// some stl containers
std::unordered_map<int, std::array<std::byte, 16>> mapToArrays;
std::unordered_map<int, long long> mapToInts;
}
class IDataFetcher
{
public:
virtual TheData GetTheData() = 0;
}
class IDataReceiver
{
public:
virtual void SetTheData(TheData theData) = 0;
}
class DataFetcherMock : public IDataFetcher
{
public:
MOCK_METHOD(TheData, GetTheData, (), (override));
}
class DataReceiverMock
{
public:
MOCK_METHOD(void, SetTheData, (TheData), (override));
}
class Sut
{
public:
Sut(std::unique_ptr<IDataFetcher> fetcher, std::unique_ptr<IDataReceiver> receiver)
void DoTheThing()
{
mReceiver->SetTheData(mFetcher->GetTheData());
}
private:
std::unique_ptr<IDataFetcher> mFetcher;
std::unique_ptr<IDataReceiver> mReceiver;
}
TEST(TestFoo, TestGroupFoo)
{
auto fetcherMock = std::make_unique<DataFetcherMock>();
auto receiverMock = std::make_unique<DataReceiverMock>();
EXPECT_CALL(*fetcherMock, GetTheData()).WillOnce(Return(theData));
EXPECT_CALL(*receiverMock, SetTheData(_)); // Here I want to check the objects are the same
Sut sut(std::move(fetcherMock), std::move(receiverMock));
sut.DoTheThing();
}
Apologies ahead of time, as I am not even sure how to phrase the question, but I was working on a homework assignment (to which the question is unrelated except that I ran into the problem working on the assignment) and came across certain problem (this is just an extraction and generalization, I left out destructors, etc...):
class idType {
int _id;
public:
int getID() { return _id; }
idType(int in = -1) : _id(-1) {}
};
class Foo {
static int _count;
idType* _id; //int wrapper just for examples sake
public:
Foo() { _id = new idType(_count); ++_count; }
idType* getID() { if(_id) return _id; return NULL; } //ERROR WHEN CALLED FROM BELOW
};
class Bar {
Foo* _foo;
public:
Bar(Foo* foo = NULL) : _foo(foo) {}
Foo* getFoo() { if(_foo) return _foo; return NULL; }
};
int foo::_count = 0;
int main() {
Bar BAR;
if(BAR.getFoo()->getID() && BAR.getFoo()); //RUN TIME ACCESS VIOLATION ERROR
return 0;
};
As I mentioned, this is not the code I am working with, but I believe it more clearly identifies what is happening. BAR passes the getFoo() check, so _foo isn't(?) NULL but getFoo()->getID() faults at if(_id).
Is the static variable somehow preventing any NULL instance of a pointer to that class type from existing?
The reason I asked that at first was because when I commented out the static variable lines in my original program, the program worked fine. HOWEVER, after trying this code (which more or less emulates what I am doing) removing the static variable lines makes no difference, it still faults the same.
This may be simple, but I am at a loss as to what is wrong. Thank you much for any help.
Check pointers before using them.
int main()
{
Bar BAR;
Foo *pFoo = BAR.getFoo();
if (pFoo && pFoo->getID())
{
// do something
}
return 0;
};
You are not creating a Foo instance anywhere, and not passing a Foo* to the Bar constructor, so Bar::_foo gets initialize to NULL, which is what Bar::getFoo() returns. And then your if statement crashes due to your backwards use of short-circuit logic (you are trying to access Foo::getID() before first validating that Bar::getFoo() returns a non-NULL Foo* pointer).
You have to pass pointer to Foo object to BAR
int main() {
Bar BAR(new Foo);
//Reverse the condition, check for pointer validity first
if(BAR.getFoo() && BAR.getFoo()->getID());
return 0;
};
Provide the destructor to cleanup _foo after usage
~Bar() { if(_foo) delete _foo; _foo=NULL; }
In the program I'm currently working on, a number of the objects include state variables which persist with the object. e.g. an object representing a point in 3d model might include a variable to control whether that point has been selected for editing. Quite regularly, one or more of these state variables will be temporarily modified by a piece of code, e.g.
void MyFunc();
{
mytype temp = statevar;
statevar = newvalue;
DoSomething();
statevar = temp;
}
This has got problems, as if DoSomething() throws an exception, statevar is not restored correctly. My planned workaround is to create a new template class that restores the value in its dtor. Something like
template<class TYPE> class PushState
{
PushState(TYPE Var) { Temp = Var; }
Pop() { Var = Temp; }
~PushState() { Pop(); }
TYPE Temp;
}
void MyFunc();
{
PushState<mytype> Push(statevar);
DoSomething();
}
Is there a better way to do this, or a well accepted method of pushing variables to the stack?
Another option is to use an ad hoc RAII class.
For example:
void MyFunc();
{
class Finally {
mytype temp;
public:
Finally( ) { temp = statevar; }
~Finally() {
statevar = temp;
}
} finally;
statevar = newvalue;
DoSomething();
}
Whether the function returns normally or throws an exception, the destructor is called automatically when finally goes out of scope.
You can use Boost.ScopeExit. This will instantiate an object whose destructor will execute the scope exit code. It just hides all the boilerplate code for you.
#include <boost/scope_exit.hpp>
void MyFunc() {
mytype temp = statevar;
statevar = newvalue;
BOOST_SCOPE_EXIT(&statevar) {
statevar = temp;
} BOOST_SCOPE_EXIT_END
DoSomething();
}
Here's my problem,
Class MClass {
public:
void Add(OtherClass* objects) {
_objects = objects;
}
private:
OtherClass* _objects;
}
//otherfile.cpp
void Setup() {
MClass myObj;
OtherClass obj[NUMBER_OF_OBJECTS];
//obj initialization here
//...
myObj.Add(obj);
}
It will cause a RT error because the *obj diminishes after the end of the function body.
But, how can make this one valid?
I like to initialized first an object before assigning it to other class.
EDIT
I don't want to use storage classes or something here, I just want a raw array since it is very expensive for me to use. Its functionality will not lessen my problem here.
So how do I do that in a raw-array style?
Class MClass {
public:
void Add(std::vector<OtherClass> objects) {
_objects = std::move(objects);
}
private:
std::vector<OtherClass> _objects;
}
//otherfile.cpp
void Setup() {
MClass myObj;
std::vector<OtherClass> obj(NUMBER_OF_OBJECTS);
myObj.Add(std::move(obj));
}
In your example, you store a pointer to a local array. If the method ends, the array goes out of scope and doesn't exist anymore.
This is the reason, your pointer is not valid anymore. If you want to solve this, learn about the scope of variables in C++.
It is not completely clear what you are trying to do, but you could store a collection of objects instead of a pointer:
class MClass
{
public:
void Add(const std::vector<OtherClass>& objects) {
objects_ = objects;
}
void Add(std::vector<OtherClass>&& objects) {
objects_ = std::move(objects);
}
private:
std::vector<OtherClass> objects_;
};
then
void Setup()
{
MClass myObj;
std::vector<OtherClass> obj(NUMBER_OF_OBJECTS);
//obj initialization here
//...
myObj.Add(std::move(obj)); // move obj's contents onto myObs's objects.
}
Stop using raw arrays, and use either std::vector or std::array. Then you don't have to worry about it anymore.
If you really want to do it manually, you have to copy is manually as well. Using e.g. std::vector and std::move is more effective, but here you go:
Class MClass {
public:
MClass()
: _objects(nullptr), _count(0)
{}
MClass(const MClass& other)
: _objects(nullptr), _count(0)
{
Add(other._objects, other._count);
}
~MClass()
{
if (_objects != nullptr)
delete [] _objects;
}
void Add(const OtherClass* objects, const size_t count)
{
if (_objects != nullptr)
delete [] _objects;
_objects = new [count];
for (size_t i = 0; i < count; i++)
_objects[i] = objects[i];
_count = count;
}
MClass& operator=(const MClass& other)
{
Add(other._objects, other._count);
}
private:
OtherClass* _objects;
size_t _count;
};
// ...
myObj.Add(obj, NUMBER_OF_OBJECTS);
As you can see, it's a lot of more code, which makes it harder to follow and debug, and also larger possibility of errors. And not as "effective" as I said above.
I need to pass a pointer to a class so some code I don't control. This code automatically free()s the pointer when it is done, but I need the class later. I hoped I could just make a 'wrapper' class that would keep the class from being deallocated without actually preventing the code from accessing it, but virtual calls don't work.
template <class T>
class PointerWrapper:public T
{
public:
T* p;
PointerWrapper(T *ptr)
{
p=ptr;
}
~PointerWrapper(void)
{
}
T* operator->() const
{
return p;
}
T& operator*() const
{
return *p;
}
};
void codeIDontControl(Example *ex)
{
ex->virtualfunction();
delete ex;
}
void myCode()
{
Example *ex=new Example();
codeIDontControl(ex);
do something with ex //doesn't work because ex has been freed
codeIDontControl(new PointerWrapper<Example>(ex));
do something with ex //ex hasn't been freed, but the changes made to it via
// Example::virtualfunction() in codeIDontControl() aren't there anymore
}
Basically, ex->virtualfunction() calls the virtual function in PointerWrapper itself instead of the virtual function in PointerWrapper->p. It seems that it's ignoring the -> operator?
Now, I don't need to use a PointerWrapper-esque class if there's a different way to do this, but it was all I could think of...
I can't modify Example either, but I can subclass it
You should provide Forwarder class - which redirects virtual calls to stored pointer. Freeing of forwarder class will not cause releasing of pointee. This approach does NOT need to do copy (which can be expensive/may be not implemented/or even not make sense):
struct Forwarder : Example
{
Example *impl;
Forwarder(Example *i) : impl(i) {}
void virtualfunction()
{
impl->virtualfunction();
}
};
Full code:
live demo:
#include <iostream>
#include <ostream>
using namespace std;
struct Example
{
virtual void virtualfunction()=0;
virtual ~Example() {}
};
struct Implmenetation : Example
{
bool alive;
Implmenetation() : alive(true) {}
void virtualfunction()
{
cout << "Implmenetation::virtualfunction alive=" << alive << endl;
}
~Implmenetation()
{
alive=false;
cout << "Implmenetation::~Implmenetation" << endl;
}
};
struct Forwarder : Example
{
Example *impl;
Forwarder(Example *i) : impl(i) {}
void virtualfunction()
{
impl->virtualfunction();
}
};
void codeIDontControl(Example *ex)
{
ex->virtualfunction();
delete ex;
}
void myCode()
{
Implmenetation impl;
codeIDontControl(new Forwarder(&impl));
//do something with ex //doesn't work because ex has been freed
impl.virtualfunction();
}
int main()
{
myCode();
}
Output is:
Implmenetation::virtualfunction alive=1
Implmenetation::virtualfunction alive=1
Implmenetation::~Implmenetation
It's bad design, really. Only the allocator should be allowed to free memory. Functions like this are dangerous, as they leave with with dangling pointers.
This is just off the top of my head, maybe you could try something like this? It's not a safe idea, but if someone implemented it I would be interested to know what happens.
class Foo
{
Foo(Foo* copy) : m_copy(copy) {}
~Foo() { if(m_copy) { *m_copy = *this; } } // Use copy constructor to create copy on destuction.
Foo* m_copy;
}
Foo copy(NULL);
Foo* original = new Foo(©);
MethodThatDeletes(original);
// Original should be destroyed, and made a copy in the process.
original = NULL;
// Copy should be a copy of the original at it's last know state.
copy;
You are providing a Example* to codeIDontControl. The overloaded operator-> on PointerWrapper is an for the PointerWrapper type not the Example* type or even the PointerWrapper* type (i.e. for a value or reference of that type not a pointer to that type).
Since the function you need to call isn't controlled by you, you will need to provide a complete wrapper of the type it expects as a wrapper over the instance you wish to control the lifetime of.