Page 1 of 1

Error in 4.50: Procedure stack has been corrupted

Posted: Tue Apr 27, 2010 2:41 pm
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

Re: Error in 4.50: Procedure stack has been corrupted

Posted: Tue Apr 27, 2010 2:55 pm
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.

Re: Error in 4.50: Procedure stack has been corrupted

Posted: Tue Apr 27, 2010 3:02 pm
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. ;)

Re: Error in 4.50: Procedure stack has been corrupted

Posted: Tue Apr 27, 2010 8:32 pm
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. :)

Re: Error in 4.50: Procedure stack has been corrupted

Posted: Tue Apr 27, 2010 9:04 pm
by netmaestro
In the meantime, did you try my version here: http://www.purebasic.fr/english/viewtop ... 12&t=39485

Re: Error in 4.50: Procedure stack has been corrupted

Posted: Wed Apr 28, 2010 2:21 pm
by Mr Coder
I didn't try yours, NM, but srod's works fine now with his bug fix.