[Done] The specified '*MemoryID' is not valid

Windows specific forum
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

[Done] The specified '*MemoryID' is not valid

Post by IdeasVacuum »

PB5.44LTS/PB5.50 x86
If I try to Fill and/or Free an allocated memory block:
[ERROR] The specified '*MemoryID' is not valid.

Code: Select all

;encrypt/decrypt a 512 char string with AES
EnableExplicit
#VecChars = 8  ;16 bytes
#KeyChars = 16 ;32 bytes
#KeyBits = 256
#StrChars = 512
Define    sStr.s = "udaqF_RYk@VHe{jVtv?{b#jNeHEzhFKAqjnPG=W$Pr~=Y=NKj=c_@[@yf@)AF]_p}MRDbUcg<u>Qn#W#d>AcpJfNmb@pBp~VDzn]P@[e<hK?]{=_CHygyjen>h+TxFsGyDCcEMdTXHvgsq)]kE_ush$ffzYPDLcA?)~bHWhH{yczjqPcQ~~E(vn=w}D%rqa#@sAJkHFW>P{hu[>J(fe%>jEag%kSKNzu)QQGZYXDV<MUnCks<h_U>Dcsh#HM<gPuXDWqw%)@LfE+Eh?UgEvL{~wy<zgC=kQ[nWjdcBr[cMmhk$cFNT%yU]jAEtwGw$)kpCF(W[]ySLyR[MaD(aCatLhqT%xavNfan%%XWaN%F#Qn~@vBf=FDc=v#ghXB]YBZ[SLHLNtpp+z[an]q@#(wf+tA(<YsfV+~FUfP[adkeM>>~pt#}FC(~CM<vDrmZhJJ>vH<NXS_eG#=stHRSnEys<YE<KGs~FT[YPNJF)byzbM}DsgBGASMNAF@jhV?)Vqx"
Define    sVec.s = "JEVR{meE"
Define    sKey.s = ")U=nG~cWZSYLYuuq"
Define   iBuff.i = StringByteLength(sStr, #PB_Unicode) + SizeOf(character)
Define      *Key = AllocateMemory(StringByteLength(sKey, #PB_Unicode)  + SizeOf(character))
Define      *Vec = AllocateMemory(StringByteLength(sVec, #PB_Unicode) + SizeOf(character))
Define *CyphdStr = AllocateMemory(iBuff)
Define *DeCypStr = AllocateMemory(iBuff)

Debug " "
Debug "     MemorySize(*Key)-->" + Str(MemorySize(*Key)) + "<--"
Debug "     MemorySize(*Vec)-->" + Str(MemorySize(*Vec)) + "<--"
Debug "MemorySize(*CyphdStr)-->" + Str(MemorySize(*CyphdStr)) + "<--"
Debug "MemorySize(*DeCypStr)-->" + Str(MemorySize(*DeCypStr)) + "<--"
Debug " "

              PokeS(*DeCypStr, sStr, #StrChars, #PB_Unicode)
              PokeS(*Key, sKey, #KeyChars, #PB_Unicode)
              PokeS(*Vec, sVec, #VecChars, #PB_Unicode)

              If AESEncoder(*DeCypStr, *CyphdStr, iBuff, *Key, #KeyBits, *Vec, #PB_Cipher_CBC)

                       Debug Len(PeekS(*CyphdStr, #StrChars, #PB_Unicode))
                       Debug PeekS(*CyphdStr, #StrChars, #PB_Unicode)

                        AESDecoder(*CyphdStr, *DeCypStr, iBuff, *Key, #KeyBits, *Vec, #PB_Cipher_CBC)

                       Debug Len(PeekS(*DeCypStr, #StrChars, #PB_Unicode))
                       Debug PeekS(*DeCypStr, #StrChars, #PB_Unicode)
                       Debug sStr

                       ;If(*CyphdStr) : FillMemory(*CyphdStr, MemorySize(*CyphdStr)) : EndIf
                       ;If(*DeCypStr) : FillMemory(*DeCypStr, MemorySize(*DeCypStr)) : EndIf
                       ;If(*Key)      : FillMemory(*Key, MemorySize(*Key)) : EndIf
                       ;If(*Vec)      : FillMemory(*Vec, MemorySize(*Vec)) : EndIf
                       ;If(*CyphdStr) : FreeMemory(*CyphdStr) : EndIf
                       ;If(*DeCypStr) : FreeMemory(*DeCypStr) : EndIf
                       ;If(*Key)      : FreeMemory(*Key) : EndIf
                       ;If(*Vec)      : FreeMemory(*Vec) : EndIf
              Else
                       MessageRequester("Attention", "AES Encode Failed")
              EndIf
End
What is wrong with the memory allocation?
Also, perhaps related, why, after displaying the encrypted string, does the Debug Output Window change font??
Last edited by IdeasVacuum on Fri Jan 06, 2017 12:06 am, edited 2 times in total.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: The specified '*MemoryID' is not valid

Post by Keya »

IdeasVacuum wrote:

Code: Select all

Define      *Key = AllocateMemory(StringByteLength(sKey, #PB_Unicode))
Define      *Vec = AllocateMemory(StringByteLength(sVec, #PB_Unicode))
add +2 to each of those to accomodate for the null. btw if you enable Purifier you get a little bit more info about it, ive found it can be good for finding funky memory hiccups where im usually overwriting something im not supposed to :)
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: The specified '*MemoryID' is not valid

Post by IdeasVacuum »

Grrr - it was right there in front of me :oops: Thanks Keya
- what threw me off track was the fact that everything else worked.

The Debug Output Window font change is still a mystery........
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: The specified '*MemoryID' is not valid

Post by Keya »

IdeasVacuum wrote:Grrr - it was right there in front of me :oops: Thanks Keya
just one of those annoying ones where the answer will only come to you after sleep or caffeinated beverages, so the forum is always the healthier and faster option :D

the debug window font thing is weird, i wonder if some of those UTF8 chars act as control codes or something

i just checked and in my XP the Debug window is a RichEdit20W, which goes someway to helping explain it!
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: The specified '*MemoryID' is not valid

Post by STARGÅTE »

It is also possible to use: #PB_String_NoZero

Code: Select all

PokeS(*Key, sKey, Len(sKey), #PB_Unicode|#PB_String_NoZero)
With this constant no additional null-character is written, and AllocateMemory(StringByteLength(sKey, #PB_Unicode)) ist ok.
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
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: The specified '*MemoryID' is not valid

Post by IdeasVacuum »

It is also possible to use: #PB_String_NoZero
I noticed that STARGATE, good to know.

Keya, you are right about the Debug Window - surprising that it is an EditorGadget. I think it need only be a TextGadget or better still a ListView/ListIcon, such that individual lines could be easily selected to copy.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply