Find references to object in gdb? - gdb

I have the address of an object. Is there any way I can find all references to that address?
I'm working with vala and have a references counting problem. So I'm trying to find where I have a reference to the object that is sticking around.

Not directly, but you can set a break point on the appropriate reference function for you object. Each object has foo_ref and foo_unref that are called to change the reference count. If you set break points on these, you can trace the reference counting.

Related

How can I keep a reference to something that gets moved?

I have something as the following using rapidjson
rapidjson::Value parent;
parent.SetObject();
rapidjson::Value child;
child.SetObject();
parent.AddMember("child", child, document.GetAllocator());
The problem is when I call parent.AddMember(), the library nullifies my child variable because rapidjson uses move semantics.
How can I still keep a reference to the child value when it gets moved?
Ideally, I'd like to keep a reference to the child node so that I can modify it later, without having to go find it in the JSON tree.
Not specific to rapidjson:
When you have a reference to an object child, and the resource owned by child is transferred to another object by moving, the reference to child is still valid, but the referred object is no longer the one that owns the resource.
You cannot make the original reference variable to refer to the other object. But you could use a pointer or a reference wrapper instead, if you change the value of the pointer when the pointed object is moved.
I'm not familiar with rapidjson, but with a brief browsing of documentation, you could at least use parent.FindMember to get a reference to the newly created member, and update the pointer to that.

Cython cast PyObject* to object without increasing reference counter

I understand that when I'm make casting like: <object> p, where p is PyObject* reference counter is increased. Is it possible to cast PyObject* to object without increasing reference counter?
In short: no. Variables typed as Python objects always use reference counters. However depending on what you actually want to do there might be various ways to avoid reference counting. For example if you only need the cast temporarily for accessing a single property you can simply use (<Foo>p).myProperty which does not increase reference counters as far as I've understood (source). This whole thread in the cython-users group also gives some more insights into how to avoid reference counting.

what exactly reference counting in c++ means?,

What exactly is reference counting? In particular, what is it for C++? What are the problems we can face if we don't handle them? Do all languages require reference counting?
What exactly is reference counting? In particular, what is it for C++?
In simple words, Reference counting means counting the references to an object.
Typically, C++ employs the technique of RAII. Wherein, the ability to manage the deallocation of an type object is tied up within the type object itself. It means that the user does not have to explicitly manage the lifetime of the object and its deallocation explicitly, The functionality to do this management is built in the object itself.
This functionality means that the object should exist and remain valid untill there are stakeholders who refer to the object, and this is achieved by reference counting. Everytime the object is shared(copied) the reference count(typically a member inside the class type) is incremented and each time the destructor is called the count is decremented, when the count reaches 0, the object is not being reffered by anyone and it marks the end of its lifetime and hence it is destructed.
What are the problems we can face if we don't handle them?
It would mean no more RAII, and endless and often faulty manual resource management.
In short programming nightmares.
Do all languages require reference counting?
Languages don't require reference counting but employing the technique provides very easy usage and less efforts for users of the language, So most languages prefer to use it to provide these advantages to their users.
Reference counting is a simple but not complete approach for garbage detection.
When the counter reaches zero, you could release that object.
BUT if there are no more used objects which referencing each other cyclic, they will never be released
Consider a references b, b references a, but nothing else reference a or b.
The reference count on a and b will be still 1 (= in use)
Reference-count garbage collection is a powerful technique for managing memory that helps prevent objects from being deleted accidentally or more than once. The technique is not limited to C++ code and, despite its name, is unrelated to the C++ concept of reference variables. Rather, the term means that we maintain a count of all ``owning references'' to an object and delete the object when this count becomes zero.
Reference counting - lets use a metaphor.
You have an ear. You want it back at some point.
You get a group of people pointing at your ear. You count them as soon as they point.
When the number goes to zero - it is just yours and you can do with it as you wish.
I.e. take it out of the equation (free it back to memory).
BTW. Circular stuff is tricky to spot.

C++ Pointer (Pass By Reference) Question

A pointer that is passed-in-by-reference. Why? aren't pointers just references anyways? What's really happening to this parameter?
void someFunc(MyPtr*& Object)
{
}
Simply speaking, it gives you the ability to change the pointer itself: it can be changed to point to another location in the function.
And the change will be reflected outside.
It enable you to:
void someFunc(MyPtr*& Object)
{
//Modify what Object is pointing to
Object=&old_Object;
//You can also allocate memory, depending on your requirements
Object=new MyPtr;
//Modify the variable Object points to
*Object=another_object;
}
Other's will have to vote to verify this cause I'm a bit rusty on my C++ but I believe the idea here is you'd pass in a pointer by reference, that is instead of creating a new space to store the pointer itself you use a reference to the pointer so if you were to modify the pointer not just the value it would be modified after returning from the function, whereas otherwise all you could do is modify the value at position passed in. Hope that makes sense.
The difference to passing just a pointer is that if the pointer is changed (Object = x) then this change will be seen by the calling function. You could achieve the same when you pass MyPtr** Object and dereference the pointer *Object = x;. With the second approach you could pass NULL to the function. This is not possible for references.
You are not quite right. The pointer content is passed by reference but the pointer itself is still passed by value, i.e. reassinging it to some other pointer will not be reflected upon the exit from the method because the pointer will be set to point to the same memory block as before the call. Think of it as a simple int variable. However with &* or ** you can reassign the pointer and that will be visible outside the scope of this method.
Why?
For the same reason that you would pass in anything else by reference.
aren't pointers just references anyways?
Dear god, no. Not even remotely the same thing. Look, you can try to build a mental model of a reference by starting with a pointer, but by the time you've fixed up all the differences, you have a horrible illogical mess.
References are a much simpler and more intuitive concept, and there are only "historical reasons" for trying to understand pointers before them. Modern C++ uses raw pointers only rarely, and treats them as an implementation detail as much as possible.
A reference is another name for an already-existing thing. That's it. When used as a function parameter, they thus allow the called function to refer to the caller's data.
It also means the pointer can be 0 (NULL) which can having meaning to the method. A reference must always be valid and cannot be made 'nothing'

What is a good way to think about C++ references?

I've been programming C, mainly in an embedded environment, for years now and have a perfectly good mental model of pointers - I don't have to explicitly think about how to use them, am 100% comfortable with pointer arithmetic, arrays of pointers, pointers-to-pointers etc.
I've written very little C++ and really don't have a good way of thinking about references. I've been advised in the past to "think of them as pointers that can't be NULL" but this question shows that that is far from the full story.
So for more experienced C++ programmers - how do you think of references? Do you think of them as a special sort of pointer, or as their own thing entirely? What's a good way for a C programmer to get their head round the concept?
I've get used to think about references as an alias for main object.
EDIT(Due to request in comments):
I used to think about reference as kind of aliasing is because it behaves in the exact same way as the original variable without any need to make an extra manipulation in order to affect the variable referenced.
For me, when I see a pointer in code (as a local variable in a function or a member on a class), I have to think about
Is the pointer null, or is it valid
Who created the object it points to (is it me?, have I done it yet?)
Who is responsible for deleting the
object
Does it always point to the same
object
I don't have to think about any of that stuff if it's a reference, it's somebody else's problem (i.e. think of a reference as an SEP Field for a pointer)
P.S. Yes, it's probably still my problem, just not right now
I'm not all too fond of the "ever-valid" view, as references can become invalid, e.g.
int* p = new int(100);
int& ref = *p;
delete p; // oops - ref now references garbage
So, I think of references as non-rebindable (that is, you can't change the target of a reference once it's initialized) pointers with syntactic sugar to help me get rid of the "->" pointer syntax.
In general you just don't think about references. You use references in every function unless you have a specific need for calling by value or pointer magic.
References are essentially pointers that always point to the same thing. A reference doesn't need to be dereferenced, and can instead be accessed as a normal variable. That's pretty much all that there is to it. You use pointers when you need to do pointer arithmetic or change what the pointer points to, and references for just about everything else.
References are pointer-consts with different syntax. ie. the reference
T&
is pretty much
T * const
as in, the pointer cannot be changed. The content of both is identical - a memory address of a T - and neither can be changed.
Then apart from that pretty much the only difference is the syntax: . for references and -> and * for pointer.
That's it really - references ARE pointers, just with different syntax (and they're const).
How about "pointers that can't be NULL and can't be changed after initialisation". Also, they have no size by themselves (because they have no identity of themselves).
I think of the reference as being the object it refers to. You access the object using . symantecs (as opposed to ->), re-enforcing this idea for me.
I think your mental model of pointers, and then a list of all the edge cases you've encountered, is the best way.
Those who don't get pointers are going to fare far worse.
Incidentally, they can be NULL or any other non-accessible memory location (it just takes effort):
char* test = "aha";
char& ok = *test;
test = NULL;
char& bad = *test;
One way to think about them is as importing another name for an object from a possibly different scope.
For instance : Obj o; Obj& r = o;
There is really little difference between semantics of o and r.
The major one seems that the compiler watches the scope of o for calling the destructor.
I think of it as a pointer container.
If you use linux, you can think of references as hard links and pointers as symbolic links (symlinks).
Hard link is just another name for a file. The file gets "deleted" when all hard links to this file are removed.
Same about references. Just substitue "hard link" with "reference" and "file" with "value" (or probably "memory location"?).
A variable gets destroyed when all references are gone out of scope.
You can't create a hard link to a nonexistent file. Similary, it's not possible to create a reference to nothing.
However you can create a symlink to a nonexistent file. Much like an uninitialized pointer. Actually uninitialized pointers do point to some random locations (correct me if I'm wrong). But what I mean is that you are not supposed to use them :)
From a syntactic POV, a reference is an alias for an existing object. From a semantic POV, a reference behaves like a pointer with a few problems (invalidation, ownership etc.) removed and an object-like syntax added. From a practical POV, prefer references unless you have the need to say "no object". (Resource ownership isn't a reason to prefer pointers, as this should be done using smart pointers.)
Update: Here's one additional difference between references and pointers which I forgot about: A temporary object (an rvalue) bound to a const reference will have its life-time extended to the life of the reference:
const std::string& result = function_returning_a_string();
Here, the temporary returned by the function is bound to result and will not cease to exist at the end of the expression, but will exist until result dies. This is nice, because in the absence of rvalue references and overloading based on them (as in C++11), this allows you to get rid of one unnecessary copy in the above example.
This is a rule introduced especially for const references and there's no way to achieve this with pointers.