Page 1 of 1

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

Posted: Thu Jan 05, 2017 1:51 am
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??

Re: The specified '*MemoryID' is not valid

Posted: Thu Jan 05, 2017 1:58 am
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 :)

Re: The specified '*MemoryID' is not valid

Posted: Thu Jan 05, 2017 2:28 am
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........

Re: The specified '*MemoryID' is not valid

Posted: Thu Jan 05, 2017 2:32 am
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!

Re: The specified '*MemoryID' is not valid

Posted: Thu Jan 05, 2017 8:41 am
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.

Re: The specified '*MemoryID' is not valid

Posted: Thu Jan 05, 2017 2:40 pm
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.