Error in 4.50: Procedure stack has been corrupted

Just starting out? Need help? Post your questions and find answers here.
Mr Coder
User
User
Posts: 54
Joined: Tue Apr 13, 2010 8:02 am

Error in 4.50: Procedure stack has been corrupted

Post by Mr Coder »

What does "Procedure stack has been corrupted" in PureBasic 4.50 mean? I get it for the following procedure that I found in this forum. The procedure works fine in 4.41 in my program.

Code: Select all

Procedure.l SaveIcon(hIcon,file$)

  Protected iconinfo.ICONINFO,hbmMask,hbmColor
  Protected cbitmap.BITMAP,cwidth,cheight,cbitsperpixel,colorcount,colorplanes
  Protected mbitmap.BITMAP,mwidth,mheight,ficon,xhotspot,yhotspot
  Protected file,imagebytecount,hdc,oldbitmap,mem,bytesinrow,temp
  Protected bitmapinfo.BITMAPINFO,bitmapinfoheader.BITMAPINFOHEADER

  If Not(GetIconInfo_(hIcon,iconinfo)) : ProcedureReturn 0 : EndIf

  ficon=2-iconinfo\fIcon : hbmMask=iconinfo\hbmMask : GetObject_(hbmMask,SizeOf(BITMAP),mbitmap)
  mwidth=mbitmap\bmWidth : mheight=mbitmap\bmHeight

  hbmColor=iconinfo\hbmColor
  If hbmColor
    GetObject_(hbmColor,SizeOf(BITMAP),cbitmap) : cwidth=cbitmap\bmWidth : cheight=cbitmap\bmHeight
    cbitsperpixel=cbitmap\bmBitsPixel : If cbitsperpixel=0 : cbitsperpixel=1 : EndIf
    If cbitsperpixel < 8 : colorcount=Pow(2,cbitsperpixel) : EndIf
    colorplanes=cbitmap\bmplanes
  Else
    cwidth=mwidth : cheight=mheight/2 : cbitsperpixel=1 : colorcount=2 : colorplanes=1 : mheight=cheight
  EndIf
  file=CreateFile(#PB_Any,file$)
  If file
    WriteWord(file,0)
    WriteWord(file,ficon)
    WriteWord(file,1)
    WriteByte(file,cwidth)
    WriteByte(file,cheight)
    WriteByte(file,colorcount)
    WriteByte(file,0)
    WriteWord(file,colorplanes)
    WriteWord(file,cbitsperpixel)
    WriteLong(file,0)
    WriteLong(file,Loc(file)+4)
    imagebytecount=SizeOf(BITMAPINFOHEADER)
    WriteLong(file,imagebytecount)
    WriteLong(file,cwidth)
    WriteLong(file,cheight+mheight)
    WriteWord(file,colorplanes)
    WriteWord(file,cbitsperpixel)
    WriteLong(file,0)
    WriteLong(file,0)
    WriteLong(file,0)
    WriteLong(file,0)
    WriteLong(file,0)
    WriteLong(file,0)
    hdc=CreateCompatibleDC_(0)
    If hbmColor=0
      WriteLong(file,#Black)
      WriteLong(file,#White)
      imagebytecount+SizeOf(rgbquad)*2
    ElseIf cbitsperpixel<=8
      temp=Pow(2,cbitsperpixel)
      bytesinrow=SizeOf(rgbquad)*temp
      mem=AllocateMemory(bytesinrow)
      GetDIBColorTable_(hdc,0,temp,mem)
      WriteData(file,mem,bytesinrow)
      FreeMemory(mem)
      imagebytecount+bytesinrow
    EndIf
    bytesinrow=(cwidth*cbitsperpixel+31)/32*4
    bytesinrow * cheight
    mem=AllocateMemory(bytesinrow)
    bitmapinfo\bmiHeader\biSize=SizeOf(BITMAPINFOHEADER)
    bitmapinfo\bmiHeader\biWidth=cwidth
    bitmapinfo\bmiHeader\biPlanes=colorplanes
    bitmapinfo\bmiHeader\biBitCount=cbitsperpixel
    If hbmColor
      bitmapinfo\bmiHeader\biHeight=cheight
      GetDIBits_(hdc,hbmColor,0,cheight,mem,bitmapinfo,#DIB_RGB_COLORS)
    Else
      bitmapinfo\bmiHeader\biHeight=2*cheight
      GetDIBits_(hdc,hbmMask,0,cheight,mem,bitmapinfo,#DIB_RGB_COLORS)
    EndIf
    WriteData(file,mem,bytesinrow)
    FreeMemory(mem)
    imagebytecount+bytesinrow
    bytesinrow=(mwidth+31)/32*4
    bytesinrow*mheight
    mem=AllocateMemory(bytesinrow)
    bitmapinfo\bmiHeader\biWidth=mwidth
    bitmapinfo\bmiHeader\biPlanes=1
    bitmapinfo\bmiHeader\biBitCount=1
    If hbmColor
      bitmapinfo\bmiHeader\biHeight=mheight
      GetDIBits_(hdc,hbmMask,0,mheight,mem,bitmapinfo,#DIB_RGB_COLORS) ; ***** ERROR HERE *****
    Else
      bitmapinfo\bmiHeader\biHeight=2*mheight
      GetDIBits_(hdc,hbmMask,mheight,mheight,mem,bitmapinfo,#DIB_RGB_COLORS)
    EndIf
    WriteData(file,mem,bytesinrow)
    FreeMemory(mem)
    imagebytecount+bytesinrow
    DeleteDC_(hdc)
    FileSeek(file,14)
    WriteLong(file,imagebytecount)
    CloseFile(file)
  EndIf
  DeleteObject_(hbmMask)
  DeleteObject_(hbmColor)
  ProcedureReturn file
EndProcedure
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Error in 4.50: Procedure stack has been corrupted

Post by freak »

It a purifier message. It means you are writing past the end of a local variable, possibly overwriting other local variables or the procedure's return address. Its usually an API call with a too small buffer for the output.

If it worked in 4.41 then you were lucky enough to only overwrite something unimportant, but you should still fix it.
quidquid Latine dictum sit altum videtur
Mr Coder
User
User
Posts: 54
Joined: Tue Apr 13, 2010 8:02 am

Re: Error in 4.50: Procedure stack has been corrupted

Post by Mr Coder »

Thanks Freak. The original code was from srod here (http://www.purebasic.fr/english/viewtop ... 12&t=23387) so I'll let him fix it. ;)
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Error in 4.50: Procedure stack has been corrupted

Post by srod »

Yes, there has been a long standing bug in this code which I haven't as yet gotten around to fixing. I'm pretty sure this will be the root of the problem.

I'll fix it presently. :)
I may look like a mule, but I'm not a complete ass.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Error in 4.50: Procedure stack has been corrupted

Post by netmaestro »

In the meantime, did you try my version here: http://www.purebasic.fr/english/viewtop ... 12&t=39485
BERESHEIT
Mr Coder
User
User
Posts: 54
Joined: Tue Apr 13, 2010 8:02 am

Re: Error in 4.50: Procedure stack has been corrupted

Post by Mr Coder »

I didn't try yours, NM, but srod's works fine now with his bug fix.
Post Reply