Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Exception on socket.send() - Solved! Thank you all!  (Read 4804 times)

0 Members and 1 Guest are viewing this topic.

SlySherZ

  • Newbie
  • *
  • Posts: 13
    • View Profile
Exception on socket.send() - Solved! Thank you all!
« on: June 19, 2014, 01:23:35 am »
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!
« Last Edit: June 19, 2014, 04:10:21 pm by SlySherZ »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
AW: Exception on socket.send()
« Reply #1 on: June 19, 2014, 07:44:04 am »
You shouldn't use manual memory management.

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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

SlySherZ

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Exception on socket.send()
« Reply #2 on: June 19, 2014, 01:18:57 pm »
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>

select_this

  • Full Member
  • ***
  • Posts: 130
  • Current mood: just ate a pinecone
    • View Profile
    • darrenferrie.com
Re: Exception on socket.send()
« Reply #3 on: June 19, 2014, 01:21:07 pm »
#include<memory.h>

That should probably be #include<memory>
Follow me on Twitter, why don'tcha? @select_this

SlySherZ

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Exception on socket.send()
« Reply #4 on: June 19, 2014, 01:41:49 pm »
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?

select_this

  • Full Member
  • ***
  • Posts: 130
  • Current mood: just ate a pinecone
    • View Profile
    • darrenferrie.com
Re: Exception on socket.send()
« Reply #5 on: June 19, 2014, 01:48:38 pm »
How do I re-write this so that no copies occur?

Try this:

std::unique_ptr<sf::TcpSocket> Socket(new sf::TcpSocket());
Follow me on Twitter, why don'tcha? @select_this

SlySherZ

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Exception on socket.send()
« Reply #6 on: June 19, 2014, 02:46:55 pm »
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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Exception on socket.send()
« Reply #7 on: June 19, 2014, 02:55:24 pm »
It's not this line (the construction) that triggers an assignment. Where else are you using the pointer?
Laurent Gomila - SFML developer

SlySherZ

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Exception on socket.send()
« Reply #8 on: June 19, 2014, 03:07:14 pm »
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.
« Last Edit: June 19, 2014, 03:51:36 pm by SlySherZ »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Exception on socket.send() - Solved! Thank you all!
« Reply #9 on: June 20, 2014, 09:07:34 am »
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...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything