Dubbuging C++ with VS. Track down varrible change - c++

I have a huge program (VS project) which contains global variable. It modifies from hundreds places. Actually it standard STL container (std::vector) and question is how can I track down this particular variable modifications?
In my work I use VS debugger (Prof 2017 15.9.5)

There are a few solutions which come to my mind:
Create another class which will encapsulte this vector.
Then allow users to use this vector by adding getter returning
reference to it.
Then you can put a breakpoint in getter.
Put a memory breakpoint on one of vector's members but it is plantform specific solution which may not work in all scenarios.
Replace std::vector with your custom type with interface identical to std::vector's which would use original std::vector in its internals.
With this solution you can monitor accesses more precisely.
P.S. remove this global ASAP.

Related

Storing dynamically created objects

Lets say I have a class Obj, and instances of them are created at run time, can I store them without new like this?
Obj my_obj;
std::vector<Obj> my_vec;
my_vec.push_back(my_obj);
Or do I have to use new?
std::vector<Obj*> my_vec;
my_vec.push_back(new Obj);
And what if I have pointers inside of my class Obj? Also, what if use the second option, do I have to clean up everything?
The question is related to heap allocated objects vs. stack objects.
There are already a few good resources on that in Stackoverflow:
Stack Memory vs Heap Memory
Stack vs Heap C++
C++ stack vs heap allocation
Why should I use a pointer rather than the object itself?
(you may want to look at all the above, each has its own angle).
But, your question is specifically about a container holding objects or pointers to objects. For this there are a few other resources:
Should I store entire objects, or pointers to objects in containers?
Is it better to save object pointer in STL container rather than object itself?
Let me summarize shortly:
Usually working with values and not pointers should be your preferred option. They are much easier to maintain and less bug-prone. Before C++11 performance considerations could be a reason for considering working with container of pointers, but since move semantics came in C++11, if you design your code properly, working with containers of values should not be costly.
Another reason for working with pointers might be polymorphism. You want your container to hold and manage different types which share the same base class. Though it seems that a container of pointers should be the solution in such a case, which is used in many examples for polymorphism in C++, this still should not be your preferred choice. The first alternative for raw pointers should be holding unique_ptr which is a smart pointer type introduced in the C++ standard library in C++11.
The other option is to model the data that you want to manage withing a class that would hide and manage your polymorphism without exposing it to the container or to the user. Let's take an example: suppose you want to manage a container of Command objects, where Command is a base class for many different actual commands. Instead of having a container of Command* or unique_ptr<Command>, rename Command to be CommandBase, then hold (wrap, hide) unique_ptr<CommandBase> inside class Command and hold in your container objects of type Command.
Another alternative would be to avoid inheritance altogether. So for different callables (our Command example) you can just use std::function.
You can also use std::variant to manage the different types (e.g. using Shape = std::variant<Circle, Triangle>;).
Hiding the fact that you are using polymorphism, or avoiding polymorphism altogether, can become quite useful in your API, with the benefits of being able to preserve proper value semantics.
More on value semantics and its benefits can be found in CppCon 2022 talk by Klaus Iglberger: Back to Basics: Cpp Value Semantics and in C++Now 2022 talk by Dave Abrahams: A Future of Value Semantics and Generic Programming, Part 1 and Part 2. The original talk on the subject, by Sean Parent: Value Semantics and Concepts-based Polymorphism, from C++Now 2012.
You can also read about value semantics in the following links:
C++ classes as value types a Microsoft blog post
Reference and Value Semantics in ISO-CPP FAQ
Value Semantics and Polymorphism a blog post by Dean Berris

Effective use of stack vs. heap memory allocation in C++

I am developing a large, complex model (mostly simple math, primarily algebra, but lots of calculations). I did my initial bid assuming I'd have to do everything once, but now the scope has expanded such that I need to run the entire model multiple times (on the same set of underlying assumptions but with a different dataset).
Based on my initial needs, I created a bunch of classes, then created dynamic instances of those classes in my main function, passing them by reference to each function as I went. That way, at the end of the main function, I can do all the necessary reporting / output once all of the functions have run. My question is about how to now modify my main function to allow for multiple iterations. A few sample bits of code below, followed by my question(s):
// Sample class declaration (in main)
vector<PropLevelFinancials> PLF;
// Sample function call (functions in other header files)
ProcessPropFinancials(vector<PropLevelFinancials>& PLF);
// Reporting at the end of main (functions in other header files)
OutputSummaryReport(vector<PropLevelFinancials>& PLF, other objects);
// What I need to do next
// Clear/Delete PLF and other objects, iterate through model again
I am quite happy with the speed and result of my current program, so don't need a whole lot of input on that front (although always welcome suggestions).
How should I implement the ability to cycle through multiple datasets (I obviously know how to do the loop, my question is about memory management)? Speed is critical. I want to essentially delete the existing instance of the class object I have created (PLF), and then run everything again on a new instance of the object(s). Is this the type of situation where I should use "new" and "delete" to manage the iterations? Would that change my function calls as outlined above? If I wanted to avoid using new and delete (stay on the stack), what are my options?
No. Do not, ever, use new and delete without a highly exceptional cause.
std::vector<T> offers a clear() member you can use. I suggest you implement a similar one for your own classes if that is what you need. Or you can simply do PLF = std::vector<T>();, which would work a bit better for your own UDTs without modification, assuming that you wrote them according to the most basic C++ guidelines.

C++ Avoiding Global Variables and Singletons

I have recently been trying to remove the use of singletons and global variables from my project but I am having a hard time doing so. I have somewhat devised a better alternative to the singletons and global variables but I am not sure how to handle the data once my application is terminated.
My application needs access to a couple of things for most of the components to work properly; Some components need to access static std::vector<Foo*> foos;and others need to access static std::vector<Bob*> bobs; and some needs to access both. What I have done is created "Managers" for these vectors, a FooManager which gives access to the protected static vector to a class inheriting it and a BobManager which does the same thing for the other vector. By doing this limit the scope of these two objects. My problem is at process termination how and where do I deallocate the pointers in each vector? Multiple classes are now "Managers" of these objects. From the derived class? but what if I deallocate something while another class needs the original data?
Basically my question is how do I avoid deleting the pointers when I shouldn't be? unique_ptr? shared_ptr? Also any other implementation of this is welcome.
If you have the choice in your design, Idan's last paragraph is the way to go (included again):
If you still insist on avoiding the Singleton pattern, what I would
recommend is to make those vectors non-static again, create a single
instance of each manager once at your main function or any other
root-ish function, and pass them to any other object that needs them.
Yes, that's alot of work - but it lets you control when those
vectors(and the objects they point to) are created and destroyed. Or -
you could just use singletons.
Your Question:
Basically my question is how do I avoid deleting the pointers when I
shouldn't be? unique_ptr? shared_ptr? Also any other implementation of
this is welcome.
Reference Counting is one way to solve your problem. It keeps track of the number of things that are interested in a set of data. A quick way (with your current implementation) is to include a variable in the manager classes that keep track of how many instances there are. In the destructor decrement the counter. If the counter is 0 delete your vectors.
If I understood correctly, you have those two vectors that need to be accessed globally, and you used to have singletons to handle each. Now you want to remove those singletons, and instead make those vectors static members and have many instances of of the Manager classes?
Don't. Just... don't.
Global variables are a problem. There is a misconception that singletons are a kind of global variables and therefore are also a problem. They are not - singletons are the solution to the global variables problem. Removing the solution does not mean removing the problem - it just means you have a problem without a solution.
If you still insist on avoiding the Singleton pattern, what I would recommend is to make those vectors non-static again, create a single instance of each manager once at your main function or any other root-ish function, and pass them to any other object that needs them. Yes, that's alot of work - but it lets you control when those vectors(and the objects they point to) are created and destroyed. Or - you could just use singletons.

How to make a global array with variable number of elements?

Is it posible to declare a global array of a struct, and add elements dynamically to it?
Thanks.
If you want to dynamically add elements to something, you might consider using a list. You could create a global list, and dynamically add elements to it as needed. If you really need array type functionality, a vector might be more your speed. In this case, the STL is likely to provide what you need.
It's also good to note that globals aren't always a good idea. If you're using globals a lot, you may want to consider refactoring your code so they won't be necessary. Many people consider global variables to be a code smell.
Avoid using non-PODs as globals. However, you can do this:
std::vector<YourStruct>& global_list()
{
static std::vector<YourStruct> v;
return v;
}
This at least avoids global initialization order problems by enforcing a policy where access is initialization. Otherwise you'll very easily wander into undefined behavior land.
As for what variable-sized container to use, it's hard to tell without more contextual information. Do you need to be able to quickly search for elements in the list, for example? Will you be removing elements from the middle of the list frequently? Do you need random-access, or is sequential iteration fine? Etc. etc.
See std::vector.
Any time you're tempted to use an array, you'd probably do better to use a vector, list, or one of the many other STL containers.
No, not directly. But you may use a STL or self-made vector.
You can use a STL container. Alternatively you can declare of your type and allocate/deallocate memory by yourself. But you should not use the 2nd way.

Boost shared_ptr use_count function

My application problem is the following -
I have a large structure foo. Because these are large and for memory management reasons, we do not wish to delete them when processing on the data is complete.
We are storing them in std::vector<boost::shared_ptr<foo>>.
My question is related to knowing when all processing is complete. First decision is that we do not want any of the other application code to mark a complete flag in the structure because there are multiple execution paths in the program and we cannot predict which one is the last.
So in our implementation, once processing is complete, we delete all copies of boost::shared_ptr<foo>> except for the one in the vector. This will drop the reference counter in the shared_ptr to 1. Is it practical to use shared_ptr.use_count() to see if it is equal to 1 to know when all other parts of my app are done with the data.
One additional reason I'm asking the question is that the boost documentation on the shared pointer shared_ptr recommends not using "use_count" for production code.
Edit -
What I did not say is that when we need a new foo, we will scan the vector of foo pointers looking for a foo that is not currently in use and use that foo for the next round of processing. This is why I was thinking that having the reference counter of 1 would be a safe way to ensure that this particular foo object is no longer in use.
My immediate reaction (and I'll admit, it's no more than that) is that it sounds like you're trying to get the effect of a pool allocator of some sort. You might be better off overloading operator new and operator delete to get the effect you want a bit more directly. With something like that, you can probably just use a shared_ptr like normal, and the other work you want delayed, will be handled in operator delete for that class.
That leaves a more basic question: what are you really trying to accomplish with this? From a memory management viewpoint, one common wish is to allocate memory for a large number of objects at once, and after the entire block is empty, release the whole block at once. If you're trying to do something on that order, it's almost certainly easier to accomplish by overloading new and delete than by playing games with shared_ptr's use_count.
Edit: based on your comment, overloading new and delete for class sounds like the right thing to do. If anything, integration into your existing code will probably be easier; in fact, you can often do it completely transparently.
The general idea for the allocator is pretty much the same as you've outlined in your edited question: have a structure (bitmaps and linked lists are both common) to keep track of your free objects. When new needs to allocate an object, it can scan the bit vector or look at the head of the linked list of free objects, and return its address.
This is one case that linked lists can work out quite well -- you (usually) don't have to worry about memory usage, because you store your links right in the free object, and you (virtually) never have to walk the list, because when you need to allocate an object, you just grab the first item on the list.
This sort of thing is particularly common with small objects, so you might want to look at the Modern C++ Design chapter on its small object allocator (and an article or two since then by Andrei Alexandrescu about his newer ideas of how to do that sort of thing). There's also the Boost::pool allocator, which is generally at least somewhat similar.
If you want to know whether or not the use count is 1, use the unique() member function.
I would say your application should have some method that eliminates all references to the Foo from other parts of the app, and that method should be used instead of checking use_count(). Besides, if use_count() is greater than 1, what would your program do? You shouldn't be relying on shared_ptr's features to eliminate all references, your application architecture should be able to eliminate references. As a final check before removing it from the vector, you could assert(unique()) to verify it really is being released.
I think you can use shared_ptr's custom deleter functionality to call a particular function when the last copy has been released. That way, you're not using use_count at all.
You would need to hold something other than a copy of the shared_ptr in your vector so that the shared_ptr is only tracking the outstanding processing.
Boost has several examples of custom deleters in the shared_ptr docs.
I would suggest that instead of trying to use the shared_ptr's use_count to keep track, it might be better to implement your own usage counter. this way you will have full control over this rather than using the shared_ptr's one which, as you rightly suggest, is not recommended. You can also pre-set your own counter to allow for the number of threads you know will need to act on the data, rather than relying on them all being initialised at the beginning to get their copies of the structure.