Quote:
ok, let's see, the weird 405 number of iteration was the previous number I got, and I probably forgot to change it every where. So yeah, it should be 148 too.
Case in point. Use std::vector.
Quote:
My strange capitalization of the OBJECT_BASE is probably over-exposure to windows constants and class names :p.
Windows, being a C API, doesn't use "class" to mean the same thing as what you get with the C++ "class" keyword. In any case, name things consistently.
Quote:
I'm interested by this stack processing of IDs, but how could I achieve this? because every data structures (lists, queues, stacks) are based upon arrays, isn't it?
Logically yes, and the SC++L stack class is a container adaptor for vector, which itself is a dynamic array. That doesn't mean that everything needs to physically use pure arrays; I've mentioned numerous times now, you should use std::vector.
Now then, consider a simple implementation:
class UniqueIDGenerator{ public: UniqueIDGenerator() : mNextID(0) { } size_t AcquireID() { // If we have an ID in the reusable stack, reuse it. if(!mReusable.empty()) { size_t result = mReusable.top(); mReusable.pop(); return (result); } // Otherwise, place the next available ID in the // set of used IDs and return the new ID. mUsedIDs.insert(mNextID); return (mNextID++); } void ReclaimID(size_t id) { std::set< size_t >::iterator it = mUsedIDs.find(id); if(it != mUsedIDs.end()) { // Store the ID on the reusable IDs stack for // potential reuse later on when a new ID is // requested. mReusable.push(*it); mUsedIDs.erase(it); } } private: size_t mNextID; std::set< size_t > mUsedIDs; std::stack< size_t > mReusable;};
This (very simple) implementation allows your (hypothetical) bullet management interface to acquire and reclaim unique IDs (unique per UniqueIDGenerator instance, obviously) for use with the bullets it manages. It allows for ID reuse by storing reclaimed IDs on a stack. When generating new IDs, if the stack is non-empty, the top value is popped from the stack and returned.
Note that this implementation isn't particularly exception safe, doesn't have great error handling, and allows the issues with reusable IDs to become very apparent (since IDs are reused quickly). I still maintain reusable IDs are generally a bad idea.
Quote:
When I'm destroying my objects, I use a "collision detection" function, which is called just after every steps function of the objects, I'm not sure it's what you're talking about, but I think this 'should' be ok, otherwise, I don't see how another class could handle the deletion of the objects without wasting memory...
Are you referring to calling the destructor manually? The reason this is bad is that it can leave your object in an unusable state (its lifetime has ended), but the object is still accessible (its storage is still around). In general, you should not call the destructor manually unless you really know what you are doing. You do not.
Manual destructor invocation is usually seen when storage has been decoupled from lifetime, as in when using placement new. You aren't using placement new, and you definately don't need to manually invoke the destructor.
Note especially that manually invoking it will cause "double destruction" and potentially undefined behavior, because eventually the destructor will be automatically invoked for you (if the original object was on the stack, it will be destroyed when it goes out of scope; if the original object was on the heap, it will be destroyed when delete is called; in both cases the destructor would be called
a total of two times if you also manually invoked it, which is bad).
If a pointer is just aliasing another object, it does not need to be cleaned up and will not leak memory (provided the original object is cleaned up). Presumably in your case, the actual BulletObjs are created via new someplace, and bulletInstances just points at them. You clean up those BulletObjs by calling delete, not by manually invoking the destructor.
A bullet management interface should handle this for you. That interface should be able to:
- When requested, create a new bullet object.
- When requested, destroy a given bullet object.
- Be able to process and update all bullets' simulation state.
- Manage the lifetime of the actual bullet objects.
(Note the lack of anything related to rendering bullets, since its generally more appropriate to keep rendering and simulation isolated).
Quote:
Oh! I forgot, could I possibly use the new and delete operators in my constructor and destructor to manage their memory with their lifetime?
Manage the memory of what? The BulletObj itself? No. Once you are in the constructor (or destructor), your memory is already allocated (or is about to be deallocated).
I think you might need to re-read your C++ book's chapter on pointers and object lifetimes. You might also want to check out the relevant sections in
The C++ FAQ Lite.