Comments (10)
Unfortunately impossible to diagnose without some dummy code which replicates the issue.
I could spend hours mocking up tests but without the exact match, hard to know if I'd hit the issue.
Any idea if it's a memory leak issue?
[EDIT: only thing I can see that you might be doing wrong there is that if your code erases the last element in the colony, it's going to iterate past .end() and cause a segmentation fault. To fix:
for (it = nclients.begin(); it != nclients.end(); ) {
if (it->lastcheck < cutoff)
it = nclients.erase(it);
else ++it;
}
from plf_colony.
Ah, I see. BTW using indexes in that way will always be slower than using an iterator,
because it creates two operations (begin() +i, ++i) instead of one (++it).
Merry Christmas!
from plf_colony.
Thanks for the quick reply. I tested the loop issue you mentioned:
struct nclient {
string ip;
unsigned port;
time_t lastcheck;
};
int main() {
plf::colony<struct nclient> nclients;
plf::colony<struct nclient>::iterator it;
struct nclient dummy;
dummy.ip = "1.2.3.4";
dummy.port = 53;
dummy.lastcheck = 381846;
nclients.insert(dummy);
dummy.ip = "1.2.5.4";
dummy.port = 5333;
dummy.lastcheck = 3846;
nclients.insert(dummy);
printf("count: %u\n", nclients.size());
for (it = nclients.begin(); it != nclients.end(); ++it) {
printf("%u\n", it->port);
it = nclients.erase(it);
}
printf("count: %u\n", nclients.size());
nclients.clear();
printf("count: %u\n", nclients.size());
exit(0);
}
Result:
count: 2
53
count: 1
count: 0
And no crash. After all, that kind of loop is the same as you have in the examples on your website. I guess I have to add more debug logging and investigate further. Thanks either way so far!
from plf_colony.
That's not the loop I wrote.
For what you've got there, not only will your loop skip the element after the one you've erased during the loop, it will crash in the manner I've described if you end up erasing the back element.
Closing until you've re-written your code with a working loop (I don't have any examples like yours in my code) and can prove that it's not the result of going past end().
from plf_colony.
The loop you wrote there: https://plflib.org/colony.htm#functions
for (plf::colony<int>::iterator it = i_colony.begin(); it != i_colony.end(); ++it)
{
it = i_colony.erase(it);
}
The purpose of my test was to see whether this causes a crash and it did not. Since that's a quick and simple test I did that first before trying the different loop you suggested in this very thread here previously.
from plf_colony.
Yeah, that loop explicitly states that it's trying to erase half the elements, in a use-case where I know that the number of elements is even, therefore it will reach the end condition.
from plf_colony.
Yes okay, my test above with 3 elements does crash. Might not hurt to make that more clear on the website.
Either way, taking the safe route you suggested above seems to prevent the crashes. Thanks!
from plf_colony.
For any std:: container, erase returns the element after the element you're erasing, so the situation will be the same for any standard container. It just might take longer to reach a crash state for a vector because non-back erasure is so much slower. Cheers-
from plf_colony.
Thanks for the insight. That might be why it didn't crash for me with a vector because I didn't catch the return. I did it like this:
vs = nclients.size();
for (i = 0; i < vs; ++i) {
nclients.erase(nclients.begin() + i);
}
Running this with 3 elements doesn't crash unlike the colony (or other container) version with an iterator.
from plf_colony.
Thanks again for the info and Merry Christmas to you, too :-)
from plf_colony.
Related Issues (20)
- Range-v3 fails to create a view out of colony of noncopyable objects HOT 10
- Get const_iterator from const_pointer HOT 3
- build failure: reintepret_cast drops qualifiers HOT 1
- reserve() calls constructor of T (C++17) HOT 7
- misaligned objects on 32 bit windows HOT 33
- get_iterator_from_pointer expects a non-const pointer HOT 7
- move-insert does not move in some cases HOT 2
- [Feature request] Consider adding push/emplace_back() proxies. HOT 1
- Request for tag/release HOT 6
- Erase is faster than Clear HOT 7
- Reverse iterator operator-> different than forward iterator HOT 1
- Typo in plf_colony.h(280)
- advice or issue, or change request to revert.... HOT 3
- PLF_CPP20_SUPPORT shouldn't be defined on Android NDK r24 HOT 4
- Adding functionality discussion HOT 4
- Update conan link in homepage HOT 8
- Support for disabled exceptions HOT 8
- Please disable `PLF_CPP20_SUPPORT` for apple-clang 14 HOT 5
- Visit function for blocks as data alternative HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from plf_colony.