-
Hey guys! I'm writing down some code to use on a simple messaging server.
Whenever the server has to send some messages in a row, I get: Access violation reading location 0xFEEEFEF6
I've been getting some errors for trying to modify free heap values too.
this is where I allocate the socket (Sorry if it's hard to read :S)
client_list.push_back(Client("", false, new sf::TcpSocket)); //I'm creating a client object, storing it
listener.accept(*(client_list[client_list.size() - 1].S.Socket)); //on a vector
selector.add(*(client_list[client_list.size() - 1].S.Socket));
And I free memory here.
delete client_list[i].S.Socket;
client_list.erase(client_list.begin() + i , client_list.begin() + i + 1);
Maybe this isn't enough to get a correct evaluation, but I don't really know from where else the bug could be coming from.
Thanks!
-
You shouldn't use manual memory management. (http://bromeon.ch/articles/raii.html)
If you get an Access Violation you're most likely try to access something out of bound or something that doesn't exist anymore. This is a very simple case to debug. Just set some break points, track different variables and step through youd application step by step.
-
I tried to use unique_pt but... I get and error saying that unique_ptr is not a member of std.
Using VS2013 with c++11 flag and #include<memory.h>
-
#include<memory.h>
That should probably be #include<memory>
-
Thanks! that fixed it!
But I still can't get this to work....
std::unique_ptr<sf::TcpSocket> Socket;
Socket = make_unique<sf::TcpSocket>();
this tries to copy a tcpsocket, which cannot be copied. How do I re-write this so that no copies occur?
-
How do I re-write this so that no copies occur?
Try this:
std::unique_ptr<sf::TcpSocket> Socket(new sf::TcpSocket());
-
error C2280: 'std::unique_ptr<sf::TcpSocket,std::default_delete<_Ty>> &std::unique_ptr<_Ty,std::default_delete<_Ty>>::operator =(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' :
attempting to reference a deleted function c:\program files (x86)\microsoft visual studio 12.0\vc\include\xlocmon
Still not working. Thanks for your help anyway :D
-
It's not this line (the construction) that triggers an assignment. Where else are you using the pointer?
-
This pointer is declared inside an object Client.
client_list.push_back(Client("", false));
Every time I pass an object or the full list I do it as a reference. At least I think I do.
I only use that pointer to send and receive messages, like so:
Socket->send(buffer, x + 1);
And as a argument to selector functions:
selector.isReady(*client_list[i].S.Socket))
EDIT: I don't really know what the problem is, but it has something to do with vector push_back() and erase()
I tried to comment those lines and the program compiles without errors
EDIT2: I ended up changing my vector to store unique pointers to Clients instead of the clients themselves. Everything working as intended... for now. Thanks a lot guys, I REALLY appreciate your help.
-
If you write your own class that is not copyable, you must make it movable in order to be stored in most containers.
By the way,
std::unique_ptr<sf::TcpSocket> Socket;
Socket = make_unique<sf::TcpSocket>();
can be written as
auto Socket = std::make_unique<sf::TcpSocket>();
There's never a need to assign a variable one line after it has been declared...