ReallocateMemory

Just starting out? Need help? Post your questions and find answers here.
GenRabbit
Enthusiast
Enthusiast
Posts: 118
Joined: Wed Dec 31, 2014 5:41 pm

ReallocateMemory

Post by GenRabbit »

In the manual for ReallocateMemory it says;

Code: Select all

*MemoryID The address of the memory area to resize. This value must be the result of a call to AllocateMemory() or ReAllocateMemory(). 

If this parameter is #Null, the command acts like AllocateMemory() and allocates a new memory area of the given size. 
In my code it has this;

Code: Select all

			*DataBuffer = #Null
			For x = 1 To TotalSkillsCounted
				y = x - 1
				TextLength = Len(weaponskillsEx(y)\Language[LanguageCount])
				*NewBuffer = ReAllocateMemory(*DataBuffer, MemorySize(*DataBuffer) + SizeOf(unicode) + TextLength)
				If *NewBuffer
					PokeU(*NewBuffer + WriteOffset,TextLength)
					WriteOffset + #UnicodeLength
					PokeS(*NewBuffer + WriteOffset, weaponskillsEx(y)\Language[LanguageCount], TextLength, #PB_Unicode | #PB_String_NoZero)
					WriteOffset + TextLength
					*DataBuffer = *NewBuffer
				Else
					FreeMemory(*Databuffer)
					ProcedureReturn #AllocateMemoryError
				EndIf
			Next x

And I get this error message
[ERROR] The specified '*MemoryID' is null.

Why is this not legal? *Datapointer is set to 0. (#NULL). Since its 0, it should just allocate memory, rather than reallocate
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: ReallocateMemory

Post by infratec »

The address needs to be #Null :!:

If you use a variable *DataBuffer this variable has an address <> #Null.

And ...
please provide always a working code :wink:

Use

Code: Select all

*DataBuffer = AllocateMemory(1)
instead.
Last edited by infratec on Thu Nov 15, 2018 9:54 am, edited 1 time in total.
GenRabbit
Enthusiast
Enthusiast
Posts: 118
Joined: Wed Dec 31, 2014 5:41 pm

Re: ReallocateMemory

Post by GenRabbit »

I was afraid of that, but hoped I was wrong. That reallocate would allocate rather than reallocate when provided address is 0.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: ReallocateMemory

Post by infratec »

See my 'simple' solution above.
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: ReallocateMemory

Post by STARGÅTE »

The error comes from MemorySize(*DataBuffer), because *DataBuffer = #Null!

Code: Select all

*DataBuffer = #Null
ReAllocateMemory(*DataBuffer, 100)
is fine.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
GenRabbit
Enthusiast
Enthusiast
Posts: 118
Joined: Wed Dec 31, 2014 5:41 pm

Re: ReallocateMemory

Post by GenRabbit »

Doh.. I forgot about that one.
Post Reply