A further explanation of the title question is in order, let me explain my scenario.
I have a list container of pointers to several objects on the heap. Whenever a new object is created a pointer to it is added to the list and whenever an object is deleted its pointer is removed. It is safe to say that all pointers on this list are always valid.
Many of the objects on the list contain pointers to other objects on the same list.
Before I dereference any of those pointers I would like to use a CheckAgainstList(ptr*)
function to make sure that the one object is pointing to another object on the same list and is therefore not pointing to an object that has since been deleted.
Get your tinfoil hats on now, Is this possible?
0x00988e50
.0x00988e50
. CheckAgainstList(ptr*)
returns true when we check the pointer because object C is on the list and is in the same memory address B used to occupy.Now we have a bug because A thinks it has a pointer to B, but B is gone and C has taken its place so to speak.
Is this potential bug even possible?
Heap memory allocation isn’t as safe as Stack memory allocation was because the data stored in this space is accessible or visible to all threads. If a programmer does not handle this memory well, a memory leak can happen in the program.
The heap is where the dynamic memory of the system is located. Dynamic memory and the heap can in many cases be considered optional in small embedded systems. Dynamic memory makes memory sharing possible between different pieces of a program.
It is called heap because it is a pile of memory space available to programmers to allocated and de-allocate. Every time when we made an object it always creates in Heap-space and the referencing information to these objects are always stored in Stack-memory.
The stack and the heap are fundamental to an embedded system. Setting up the stack and the heap properly is essential to system stability and reliability. Incorrectly used, they may cause your system to wreak havoc in the strangest ways. Stack and heap memory must be allocated statically by the programmer.
Not only is it possible, it's very likely. A good memory allocator will try to reuse memory as often as possible to reduce fragmentation and bloat.
The problem you're trying to solve might be amenable to a weak_ptr, which can be checked for validity before it's used.
Yes that bug is entirely possible.
Basically what you are doing is pretty dangerous and will lead to bugs pretty quickly. You may be best off using a reference counted smart ptr of some sort. C++11 includes std::shared_ptr which means you can use in place of your normal pointer. This way the memory won't get freed until everything has finished with it and would alleviate issues like you describe.
Your only other option would be to scan through all other objects to see if they reference the deleted 'B' and do something like "null" out their pointers to the, now deleted, pointer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With