Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"[ERROR] Memory Corrupt" with DSOCK
#1
This code works fine in the IDE, and when compiled it still runs, but displays "[ERROR] Memory Corrupt" at the very end.

This is basically just a copy of the gethttp.bas example, except I'm using a string for the receive buffer. If I make the buffer smaller (<=780 bytes), I do not get the error. But I want a big buffer. Tongue

Anybody know what's causing this or how to fix it?

Code:
DEFINT A-Z
'$DYNAMIC
'$INCLUDE: 'DSOCK.BI'
'$LIB: 'DSOCKQB.LIB'
CLS

DIM WSADat AS WSAData
DIM saServer AS sockaddrIn

ReqVer = MAKEWORD(1, 1)
rc = WSAStartup(ReqVer, WSADat)
IF rc <> 0 THEN
  PRINT "Error:"; rc
  rc = WSACleanup
  END
END IF

IF WSADat.wVersion <> ReqVer THEN
  PRINT "Winsock version not supported"
  rc = WSACleanup
  END
END IF

lpServerName$ = "www.qbasicnews.com"
lpHostEntry& = gethostbyname(lpServerName$)
IF lpHostEntry& = NULL THEN
  PRINT "Can't find " + lpServerName$
  rc = WSACleanup
  END
END IF

SocketHandle& = socket(AF.INET, SOCK.STREAM, IPPROTO.TCP)
IF SocketHandle& = INVALID.SOCKET THEN
  PRINT "Error:"; WSAGetLastError
  rc = WSACleanup
  END
END IF

lpServEnt& = getservbyname("http", "tcp")
IF lpServEnt& = NULL THEN
  saServer.sinPort = htons(80)
ELSE
  saServer.sinPort = servent.sPort(lpServEnt&)
END IF

saServer.sinFamily = AF.INET
saServer.sinAddr.sAddr = hostent.hAddrList(lpHostEntry&)

rc = connect(SocketHandle&, saServer, LEN(saServer))
IF (rc = SOCKET.ERROR) THEN
  PRINT "Error:"; WSAGetLastError
  rc = closesocket(SocketHandle&)
  rc = WSACleanup
  END
END IF

PRINT "Sending"

SendBuffer$ = "GET / HTTP/1.1" + CHR$(13) + CHR$(10)
SendBuffer$ = SendBuffer$ + "Host: " + lpServerName$ + CHR$(13) + CHR$(10)
SendBuffer$ = SendBuffer$ + "Connection: close" + CHR$(13) + CHR$(10)  'Keep-Alive?
SendBuffer$ = SendBuffer$ + "User-Agent: Your Mother" + CHR$(13) + CHR$(10)
SendBuffer$ = SendBuffer$ + CHR$(13) + CHR$(10)

lpSendBuffer& = MAKELONG(SADD(SendBuffer$), VARSEG(SendBuffer$))
rc = send(SocketHandle&, lpSendBuffer&, LEN(SendBuffer$), 0)
IF rc = SOCKET.ERROR THEN
  PRINT "Error:"; WSAGetLastError
  rc = closesocket(SocketHandle&)
  rc = WSACleanup
  END
END IF

PRINT "Receiving:"

BufferSize = 4096
ReceiveBuffer$ = SPACE$(BufferSize)
lpRecvBuffer& = MAKELONG(SADD(ReceiveBuffer$), VARSEG(ReceiveBuffer$))

OPEN "RECEIVE.TXT" FOR OUTPUT AS #1

DO
  rc = recv(SocketHandle&, lpRecvBuffer&, BufferSize, 0)

  IF rc = SOCKET.ERROR THEN
    PRINT "Error:"; WSAGetLastError
    rc = closesocket(SocketHandle&)
    rc = WSACleanup
    END
  END IF

  IF rc = 0 THEN EXIT DO

  PRINT LEFT$(ReceiveBuffer$, rc);
  PRINT #1, LEFT$(ReceiveBuffer$, rc);
LOOP

CLOSE #1

rc = shutdown(SocketHandle&, 2)
rc = closesocket(SocketHandle&)

rc = WSACleanup
END
Reply
#2
I also have some problems like that with some of my engines. Works fine inside the IDE, works using AFlib but would crash using Rellib. QB lib bug?
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#3
I dunno what causes it, but I'm pretty sure the "[ERROR] Memory Corrupt" message is generated by DSOCK, maybe Blitz knows?
Reply
#4
Blitz: We need you here. :*)
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#5
I'm guessing that qb is shuffling around memory, that's why you never should use dynamic structures buffers with libs, becuase at some time qb can change its adress while the lib doesn't know and that will cause that some unknown memory area is overwritten.
oship me and i will give you lots of guurrls and beeea
Reply
#6
Hrmm...I think you're right. I switched to a fixed-length string and now it works fine. Thanks for your help.
Reply
#7
np
oship me and i will give you lots of guurrls and beeea
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)