How to have a data buffer?

Just starting out? Need help? Post your questions and find answers here.
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

How to have a data buffer?

Post by Shannara »

Ref: http://www.purebasic.fr/english/viewtopic.php?t=23930

For networking by sending and rec bytes (not strings). How in the world have you guys figured out how to make a data (byte) buffer when MemorySize() returns old size (due to an OS, thanks Windows! limitation).

For instance, if I wanted to free the client buffer, I would use FreeMemory(*ClientBuffer), but then MemorySize(*ClientBuffer) still have the info. So, when I attach the additional data to the buffer, it is bigger when it shouldnt ..

So basically, my question is for all you network gurus out there ... how did you get around this OS limitation?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

I don't understand the problem. After you have used FreeMemory(Memory) assume that the memory is no more and do not place anything there.

If you need a way to check whether you have freed some memory, set the memory pointer to 0 after every call to FreeMemory():

Code: Select all

Macro FreeMemorySafe(Memory)
  FreeMemory(Memory)
  Memory = 0
EndMacro

Memory = AllocateMemory(49)
Debug MemorySize(Memory)
FreeMemorySafe(Memory)
Debug MemorySize(Memory)
But, if you're relying on MemorySize to check whether the buffer is initialized you are doing dangerous coding. Only rely on the return value of AllocateMemory() and on a call to FreeMemory() for that.

So if you called AllocateMemory() and the return value was 0 then the memory is not allocated. Else the memory is allocated until you call FreeMemory(). No other PB calls can check it, if you can't keep track of it using program flow you must make a variable for it (or set the memory pointer to 0).

Code: Select all

So, when I attach the additional data to the buffer
Don't attach data to a buffer you've freed!
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

I think there's some misunderstanding. I attach data to an existing buffer. For instance, the *ClientBuffer will grow until a complete packet is recieved, then it's parse out, and shrinked down.

The Memory=0 is a great idea though. That'll work to blank out the buffer completely.

I rely on MemorySize() usually to see the size of an existing buffer, the buffer I will use to attach to. Then I do something like *ClientBuffer = ReAllocateMemory(memorysize(*clientbuffer) + memorysize(*newbuffer)).

This will reallocate, then I can take the memorysize(*clientbuffer) + 1 as the address to Movememory or copymemory of the new buffer onto the end of the client buffer :)

I think the Memory = 0 will do the trick :)

Thank you very much!
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Yes, I didn't understand that you was using ReallocateMemory(). But still you need to only use MemorySize() on a memory area that is not freed.
Post Reply