std::vector crashes on every action

Weird error with std::vector

Page 1 of 1

5 Replies - 3275 Views - Last Post: 17 August 2010 - 12:37 PM Rate Topic: -----

#1 Guest_t0day*


Reputation:

std::vector crashes on every action

Posted 16 August 2010 - 12:47 PM

Hi there.
Currently I'm using a std::vector for storing a pointer to my CPlayer class.
I declare this vector by
  	std::vector<class CPlayer*> playerList;



in my CServer.h

In CServer.cpp I include
#include "CServer.h"
#include "CPlayer.h"
#include <vector> // Yea useless :P/>



Once a player connects I need to access playerList. But if I do that I end up with an access violation. The confusing thing for me is that it happens on every action I try to do with the vector.
Copy -> Crash
		std::vector<class CPlayer*> pl = this->playerList;


Do .size() -> Crash
this->playerList.size();



Does anybody got a helpful idea for me how to fix that?

Thanks t0day

Is This A Good Question/Topic? 0

Replies To: std::vector crashes on every action

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,233
  • Joined: 14-September 07

Re: std::vector crashes on every action

Posted 16 August 2010 - 01:00 PM

1.

std::vector<class CPlayer*> playerList;



is syntactically incorrect.

std::vector<CPlayer*> playerList;



2. Why are you copying a vector of pointers into another? If you aren't solid on pointers it would be better to brush up on them before diving into some idiosyncrasies involving them and their storage in STL containers.
Was This Post Helpful? 0
  • +
  • -

#3 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: std::vector crashes on every action

Posted 16 August 2010 - 01:16 PM

why even use a vector? why not just access it through playerlist directly? ... I guess im confused on what your trying to accomplish here

Are you thinking along the lines of this?
std::vector<CPlayer *> players;
if(new player connected)
     players.push_back(&theNewPlayer);


This post has been edited by ImaSexy: 16 August 2010 - 01:19 PM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_t0day*


Reputation:

Re: std::vector crashes on every action

Posted 16 August 2010 - 02:01 PM

Quote

1 std::vector<class CPlayer*> playerList;


is syntactically incorrect.


1 std::vector<CPlayer*> playerList;


CPlayer isn't included at CServer.h. And it works with other functions very well :P (Althrough it might be bad coding style)

Quote

2. Why are you copying a vector of pointers into another? If you aren't solid on pointers it would be better to brush up on them before diving into some idiosyncrasies involving them and their storage in STL containers.

Well I used it for some testing purposes. The real crash occures when I used .size()
std::vector<CPlayer *>newvector = this->playerList;
newvector.size();


but it also crashes at copying the vector.

Quote

why even use a vector? why not just access it through playerlist directly? ... I guess im confused on what your trying to accomplish here

Are you thinking along the lines of this?
view source
print?
1 std::vector<CPlayer *> players;
2 if(new player connected)
3

I don't really get what you want to tell me sorry :(
I'm using the vector for storing pointes to class instances. If you (the player) connect it creates a new CPlayer and adds it to the playerList.
Was This Post Helpful? 0

#5 GWatt   User is offline

  • member icon

Reputation: 307
  • View blog
  • Posts: 3,105
  • Joined: 01-December 05

Re: std::vector crashes on every action

Posted 16 August 2010 - 03:31 PM

What you've posted is entirely out of context and not wrong by itself. You should post more of what your source. The function(s) where this happens would be good.
Was This Post Helpful? 0
  • +
  • -

#6 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: std::vector crashes on every action

Posted 17 August 2010 - 12:37 PM

Any reason why you're storing pointers instead of storing the objects directly? You may find that it would make your life much simpler by declaring std::vector<CPlayer> instead - alot of the problems you're seeing should disappear.

If you really need to store pointers (Nothing you have said in your post suggests that you do), then what you're doing comes with all kinds of caveats attached - making copies of arrays or containers which store pointers can silently cause alot of issues if you aren't careful with ensuring deep copies, and ensuring that you don't end up with 'dangling' pointers. A common approach is to wrap a vector<T*> within its own class which manages the vector (resolves all your 'deep copy' and cleanup issues), using an object composition kind of approach; the idiom is commonly known as RAII (Resource Acquisition Is Initialisation), its worth taking time to look this up on google.

This post has been edited by Bench: 17 August 2010 - 12:41 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1