Giter Site home page Giter Site logo

erase() crashes about plf_colony HOT 10 CLOSED

AIIleG avatar AIIleG commented on May 20, 2024
erase() crashes

from plf_colony.

Comments (10)

mattreecebentley avatar mattreecebentley commented on May 20, 2024 1

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.

mattreecebentley avatar mattreecebentley commented on May 20, 2024 1

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.

AIIleG avatar AIIleG commented on May 20, 2024

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.

mattreecebentley avatar mattreecebentley commented on May 20, 2024

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.

AIIleG avatar AIIleG commented on May 20, 2024

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.

mattreecebentley avatar mattreecebentley commented on May 20, 2024

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.

AIIleG avatar AIIleG commented on May 20, 2024

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.

mattreecebentley avatar mattreecebentley commented on May 20, 2024

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.

AIIleG avatar AIIleG commented on May 20, 2024

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.

AIIleG avatar AIIleG commented on May 20, 2024

Thanks again for the info and Merry Christmas to you, too :-)

from plf_colony.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.