Converting void* GetClientData to integer Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
xskater11x
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat May 20, 2006 2:45 am

Converting void* GetClientData to integer

Post by xskater11x »

I was wondering if there was any simple way to convert from void* to int. I have a map of sockets to players and the only way to find out what player is to find the set socket number. So far i have only been able to get an int* to convert successfully, but the map needs a regular int*. If anyone has any ideas, please share, or i can provide code if you need to see what I am currently doing.
phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 »

First, you should know what hides behind your void*.
If its a real pointer then:
int* ip = (int*)vp;
or
int* ip = static_cast<int*>(vp);

If its not a pointer, and only a single integer, you can do this:
int i = (int)*vp;
xskater11x
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat May 20, 2006 2:45 am

Post by xskater11x »

It's still not returning the correct id from the socket.

Code for display:

Code: Select all

			bool isRemoved = removeSocket(sock);
			if (isRemoved)
			{
				int id= (int)*sock->GetClientData();
				wxString sRemove = "Client succesfuly removed, clientID= ";
				sRemove += wxString::Format("%d", id);
				sRemove+="\n";
				((ServerFrame*)sFrame)->textChat->AppendText(sRemove);
			}
			else
			{
				((ServerFrame*)sFrame)->textChat->AppendText(_("Socket was not removed from system.\n"));
				sock->Destroy();
			}
RemoveSocket:

Code: Select all

bool Network::removeSocket(wxSocketBase *remsocket)
{

	int id = (int)*remsocket->GetClientData();

	clientSockets[id]->Destroy;
	clientSockets[id] = NULL;

	return true;

}
Output:

Code: Select all

OnSocketEvent: wxSOCKET_CONNECTION
New client connection accepted
Client was added, clientID= 0
OnSocketEvent: wxSOCKET_INPUT
Recieving incoming data
Client Identify Recieved.
grim
OnSocketEvent: wxSOCKET_LOST
Deleting socket.
Client succesfuly removed, clientID= -17891602
tan
wxWorld Domination!
wxWorld Domination!
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Post by tan »

Hi,
try to change

Code: Select all

  int id= (int)*sock->GetClientData();
to

Code: Select all

  int id= *((int *)sock->GetClientData());
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
Belgabor
I live to help wx-kind
I live to help wx-kind
Posts: 173
Joined: Mon Sep 25, 2006 1:12 pm

Post by Belgabor »

phlox81 wrote:If its not a pointer, and only a single integer, you can do this:
int i = (int)*vp;
Shouldn't that be
int i = (int)vp;
?

The other would probably convert a pointer to vp to int...

To make sure we should see where and how the client data is assigned =)
phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 »

Belgabor wrote:
phlox81 wrote:If its not a pointer, and only a single integer, you can do this:
int i = (int)*vp;
Shouldn't that be
int i = (int)vp;
?

The other would probably convert a pointer to vp to int...

To make sure we should see where and how the client data is assigned =)
yeah, was a little late yesterday :)
xskater11x
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat May 20, 2006 2:45 am

Post by xskater11x »

Code: Select all

int openSlot = findOpenPlayerSlot();

int* clientData = new int(openSlot);
sock->SetClientData(clientData);
thats how its set, openslot is returned from a function
Shouldn't that be
int i = (int)vp;
?
that just returns cannot convert 'void*' to 'int'
tan
wxWorld Domination!
wxWorld Domination!
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Post by tan »

xskater11x wrote:

Code: Select all

int openSlot = findOpenPlayerSlot();

int* clientData = new int(openSlot);
sock->SetClientData(clientData);
thats how its set, openslot is returned from a function
Shouldn't that be
int i = (int)vp;
?
that just returns cannot convert 'void*' to 'int'
In ths case

Code: Select all

int *ptmp = (int *)sock->GetClientData();
int res = *ptmp;
delete ptmp;
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 »

tan wrote:
In ths case

Code: Select all

int *ptmp = (int *)sock->GetClientData();
int res = *ptmp;
delete ptmp;
don't use delete for pointers that aren't created with new!
tan
wxWorld Domination!
wxWorld Domination!
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Post by tan »

phlox81 wrote:
tan wrote:
In ths case

Code: Select all

int *ptmp = (int *)sock->GetClientData();
int res = *ptmp;
delete ptmp;
don't use delete for pointers that aren't created with new!
But it is:

Code: Select all

int* clientData = new int(openSlot);
sock->SetClientData(clientData);
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
xskater11x
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat May 20, 2006 2:45 am

Post by xskater11x »

Code: Select all

First-chance exception at 0x00539254 in Server.exe: 0xC0000005: Access violation writing location 0x00000045
is what i now get using tan's suggestion

Code: Select all

clientSockets[id]->Destory();
is what brings up the error. Which makes me believe the number its converting to is wrong, because the sockets function perfectly.
tan
wxWorld Domination!
wxWorld Domination!
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Post by tan »

xskater11x wrote:

Code: Select all

First-chance exception at 0x00539254 in Server.exe: 0xC0000005: Access violation writing location 0x00000045
is what i now get using tan's suggestion

Code: Select all

clientSockets[id]->Destory();
is what brings up the error. Which makes me believe the number its converting to is wrong, because the sockets function perfectly.
Post more relevant code, i belive the problem is anywhere else, then converting to int.
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
Belgabor
I live to help wx-kind
I live to help wx-kind
Posts: 173
Joined: Mon Sep 25, 2006 1:12 pm

Post by Belgabor »

xskater11x wrote:

Code: Select all

int openSlot = findOpenPlayerSlot();

int* clientData = new int(openSlot);
sock->SetClientData(clientData);
thats how its set, openslot is returned from a function
The correct statement then is (I think):

Code: Select all

int i = ((int*) vp)*;
If that doesn't work, indeed the problem has to be somewhere else.
Post Reply