Sign in to follow this  
grekster

Problem with std::list

Recommended Posts

following on from my template problem yesterday I have encountered a problem using std::list which I didnt have yesterday :S I create a class and try to add it to the list like so: cFunctorBase1<arg_type>* Function = new cFunctor1<T,arg_type>(pClass,func); this->ConnectedSlots.push_back(Function); The list is defined as follows: std::list<cFunctorBase1<arg_type>*> ConnectedSlots; arg_type is a template parameter for the class. the error is gives me when it gets to the push_back line is: Unhandled exception at 0x0040403c in JABRECLO.exe: 0xC0000005: Access violation reading location 0xabababb3. What ive noticed is that the list size is extremly big (when viewed in debug) although it should be empty. (and even if i call clear() on it in the constructor it is still not empty:S) If you need any more info (i know what ive posted is a bit vague) just tell me what you would like me to post as im not sure where the problem could lie and want to stay short of posting the whole codebase up lol. If anyone has any suggestions im all ears:) Cheers

Share this post


Link to post
Share on other sites
You might want to check the value of this. My guess is that it is 0xabababab, and that ConnectedSlots is offset 8 bytes from that, which would add up to 0xabababb3. You probably have a few variables (perhaps a couple of 32-bit ints) declared in the class, followed by the list.

If this is this case, then the instance of your overall object is invalid, not the list itself. Maybe you never actually new the instance? It sounds like an uninitialized pointer to me. The reason this problem can be sneaky is because even with an invalid pointer to a class, you can still successfully call (non-virtual) functions using that pointer. The program crashes only when you actually try to access a member variable, since it is then that it needs to access the this pointer, and dereference it (or a pointer offset from it).

Share this post


Link to post
Share on other sites
Quote:
Original post by Agony
You might want to check the value of this. My guess is that it is 0xabababab, and that ConnectedSlots is offset 8 bytes from that, which would add up to 0xabababb3. You probably have a few variables (perhaps a couple of 32-bit ints) declared in the class, followed by the list.

If this is this case, then the instance of your overall object is invalid, not the list itself. Maybe you never actually new the instance? It sounds like an uninitialized pointer to me. The reason this problem can be sneaky is because even with an invalid pointer to a class, you can still successfully call (non-virtual) functions using that pointer. The program crashes only when you actually try to access a member variable, since it is then that it needs to access the this pointer, and dereference it (or a pointer offset from it).


Holy crap that's some good advice...writes down on hand for future problems...

Rating++

Share this post


Link to post
Share on other sites
God I feel so stupid. I had previously designed my events system to have ID 0 as an ALL_EVENTS Handler and so had been returning an ID value 1 higher then the position in the list. When I changed the design I forgot about that and so i had an Array bounds error *slaps forehead* oh well nevermind.

Thanks for that Agony. Very informative ill have to remember that:)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this