Yeah, I know I'm a slow coder. I have the internet to blame (code a line, browse for 10 minutes...) Anyway, can anyone see what's wrong in the below code?
// deletes a component from a map of sets.void delComponent( ComponentType* component ) { // THIS PART IS A MAP ( string here ) // THIS PART IS A SET // _components[ component->getClassName() ].erase( *component );}// components are sorted in an ordered set; to change the// key you have to take it out and put it back in. This // function does that.ComponentType* refileComponent( ComponentType* component, std::string newClassName ) { // create a new component object ComponentType* newComponent = addComponent( newClassName ); // fill it with data from the old one *newComponent = *component; // omg assignment operation? // delete the old one delComponent( component ); // return the new one return newComponent;}
Cookies for the first person who can find the bug. Oh, and my code doesn't really have comments like that. Well, except for the "omg assignment operation?" because I wasn't 100% sure how that would be handled when I wrote it. And I also removed the fix, to add suspense.
Anyway, find that bug, and under what circumstances it will crash the system!!
EDIT: Oh, and a precondition of both functions is that the ComponentType* passed is non-NULL. Dur dur :D
That string param should be a const std::string& to avoid copy overhead per call. You'd probably have a much easier time minimizing horrible memory leak problems if you refrained from using pointers all over the place and instead used references for storing these "components." Also, I sure hope that whatever a "component" is that you either have an overloaded assignment operator, or a shallow copy makes sense (which, given the proliferation of pointer types in the code you've posted, is not exactly easy to guarantee).
As it is, there's so many potential things in that code that might explode, I'm not sure which one you're handing out cookies for [wink]