-
Hey I've been working on IPC using iceoryx its a real beauty. The problem I'm facing here is I've created a vector of structure(having double & uint64_t). I'm doing Now I've iterated the vector and tried assigning vector values to zero but I get the following error. why is it accessing invalid memory? I just want to clear the vector on the next iteration. How do I erase/clear all the values of this vector? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 19 replies
-
@piyushmz From your description and error message I assume you have a piece of code like this: struct Structure {
uint64_t a;
uint64_t b;
};
iox::cxx::vector<Structure, 10> myVector;
// fill the vector with 10 elements of Structure here
myVector.clear();
for(uint64_t i = 0; i < 10; ++i) {
myVector.at(i).a = 0;
myVector.at().b = 0;
} If this is the case then some remarks. If you want to zero the memory when an element is destroyed you have to explicitly implement a destructor for your structure since the default destructor of POD types (plain old data types like int, float, char, bool) leaves the memory unchanged since there is nothing to cleanup. struct Structure {
~Structure() {
a = 0;
b = 0;
}
uint64_t a;
uint64_t b;
}; This would set your memory on destruction to zero. So the call
would call the destructor of the elements and zero them. But now the as-if rule maybe kicks in https://en.cppreference.com/w/cpp/language/as_if . In short, the compiler is allowed to optimize statements away when they cannot be observed by the program. This means the zeroing is maybe optimized away and you are back to square one. Now I gave you the full overview of the problem - sorry it is maybe more then you asked for. @piyushmz maybe we could solve this pickle also on a different front. Could you please send me the code snippet how you publish the vector? And why do you want to zero the vector in the first place? |
Beta Was this translation helpful? Give feedback.
-
I've the same code as the complexdata topic_data.hpp will be imported in objecdetector.hpp ---------------------------topic_data.hpp----------------------------------
--------------------------objectdetector.hpp-------------------------------
Here's the link for output asciinema output . In this you can see when object is not detected by the camera the values remain static in the vector I need them to be zero. I tried p.clear() but it's not possible. |
Beta Was this translation helpful? Give feedback.
-
ohhh... I got it very clear. I did one mistake that the m_detectedBoxList vector was not generating any values when the camera object wasn't detected hence the values stayed static over there. Now I handled it with an if else which works as desired. |
Beta Was this translation helpful? Give feedback.
-
Hey @elfenpiff @elBoberido sorry to bother you again. So below is my subscriber and the function onProcess() is called iteratively. This subscriber is included in If I declare subscriber globally I get following which is a bad idea I suppose.
I've included the
Tried a lot of things on this just can't get where I'm going wrong? |
Beta Was this translation helpful? Give feedback.
ohhh... I got it very clear. I did one mistake that the m_detectedBoxList vector was not generating any values when the camera object wasn't detected hence the values stayed static over there. Now I handled it with an if else which works as desired.
Thanks very much for your help @elBoberido @elfenpiff