Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2003-04

Exception w/ Freemem()

9 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Jon Sawyer

W/ the following code whenever I call DeAllocate() I get the following error: An unhandled exception occurred at 0x0806268A :
Invalid pointer operation
0x0806268A
0x08061FB0
0x0805AAF7
0x08065D17
0x08048567

I've been able to track it to the FreeMem() line, but I don't understand why its doing it..

I've got the folowing code:

Function    tQSurface.Allocate(iWidth, iHeight: Word): Boolean;
Begin
  {Check for Mem}
  If MaxAvail > (iWidth*iHeight*SizeOf(tQPixel)) then
  Begin
    {$IfDef QDebug}
      WriteDebug('Allocated(): OK!');
    {$EndIf}
    If Allocated then DeAllocate;
    GetMem(fBitmap, iWidth*iHeight*SizeOf(tQPixel));
    fWidth  := iWidth;
    fHeight := iHeight;
    Allocate := TRUE;
  end
  else
  Begin
    {$IfDef QDebug}
      WriteDebug('Allocate(): Not Enough Memory');
    {$EndIf}
    Allocate := False;
  End;
end;
Procedure   tQSurface.DeAllocate;
Begin
  If fBitmap  nil then
  Begin
    FreeMem(fBitmap, fWidth*fHeight*SizeOf(tQPixel));
  end;
end;

Thanks in advanced for any help/advice..

Pierre Muller
to Jon Sawyer

These kind of problems usually are due to some memory corruption, like writing past the end of an allocated memory block, which can then destroy the chain of free blcoks handled in GetMem/FreeMem functions.

I would bet that simply calling Allocate and Deallocate will not generate your exception...

Try to compile your program with '-ghl' option.
(and also the RTL sources if you have them,
just use 'make OPT=-ghl' in your RTL source directory) This might help you to find out what's is going wrong.

Unless you can give aa full source code and
a detailed suite of operations to do to reproduce the bug (if your program is interactive)
it will be very difficult to help you more....

you wrote 52 lines
W/ the following code whenever I call DeAllocate() I get the following error: An unhandled exception occurred at 0x0806268A : Invalid pointer operation 0x0806268A 0x08061FB0 0x0805AAF7 0x08065D17 0x08048567 I've been able to track it to the FreeMem() line, but I don't understand why its doing it.. I've got the folowing code: Function tQSurface.Allocate(iWidth, iHeight: Word): Boolean; Begin {Check for Mem} If MaxAvail > (iWidth*iHeight*SizeOf(tQPixel)) then Begin {$IfDef QDebug} WriteDebug('Allocated(): OK!'); {$EndIf} If Allocated then DeAllocate; GetMem(fBitmap, iWidth*iHeight*SizeOf(tQPixel)); fWidth := iWidth; fHeight := iHeight; Allocate := TRUE; end else Begin {$IfDef QDebug} WriteDebug('Allocate(): Not Enough Memory'); {$EndIf} Allocate := False; End; end; Procedure tQSurface.DeAllocate; Begin If fBitmap nil then Begin FreeMem(fBitmap, fWidth*fHeight*SizeOf(tQPixel)); end; end; Thanks in advanced for any help/advice.. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Jonas Maebe
to Jon Sawyer: W/ the following code whenever I call DeAllocate() I get the following error: An…
Jon Sawyer wrote
If MaxAvail > (iWidth*iHeight*SizeOf(tQPixel)) then Begin

Note that you should leave this line out. MaxAvail only indicates how much of the memory that's currently allocated to your application is still free. Once that memory is full and you allocate more, the RTL will ask the OS to grow the heap of your program. Once memory is exhausted (though this can happen long after you've used all physical memory in the computer, since the OS will use swapping to give you more), you will get a run time error 204. To catch this and make sure your program doesn't stop then, you have to use exceptions (uses sysutyils; and a try..except block, see the manual for more info).

Jonas

Jon Sawyer
to Jonas Maebe
Jonas Maebe wrote 14 lines
On vrijdag, apr 11, 2003, at 07:32 Europe/Brussels, Jon Sawyer wrote: > If MaxAvail > (iWidth*iHeight*SizeOf(tQPixel)) then > Begin Note that you should leave this line out. MaxAvail only indicates how much of the memory that's currently allocated to your application is still free. Once that memory is full and you allocate more, the RTL will ask the OS to grow the heap of your program. Once memory is exhausted (though this can happen long after you've used all physical memory in the computer, since the OS will use swapping to give you more), you will get a run time error 204. To catch this and make sure your program doesn't stop then, you have to use exceptions (uses sysutyils; and a try..except block, see the manual for more info).

Thank you. I will look into that and try it out.

Show quoted text 17 lines
These kind of problems usually are due to some memory corruption, like writing past the end of an allocated memory block, which can then destroy the chain of free blcoks handled in GetMem/FreeMem functions. I would bet that simply calling Allocate and Deallocate will not generate your exception... Try to compile your program with '-ghl' option. (and also the RTL sources if you have them, just use 'make OPT=-ghl' in your RTL source directory) This might help you to find out what's is going wrong. Unless you can give aa full source code and a detailed suite of operations to do to reproduce the bug (if your program is interactive) it will be very difficult to help you more....

I is absolutly possible for me to give a full bug report but it is most likely a bug in my own code. I'll have to check my GL routines to make sure that they are not writing past the end of the allocated chunk of memory.

I'm using Pointer Math to write to the memory: ie
pQPixel(fBitmap)[(fWidth*iY)+(iX-1)] := MyPixel; {ect..} ^-- Is there anything wrong with this algorithim?

Thats the only algorithim I have implemented at the moment that writes the memory surface.

Jon Sawyer
to Jon Sawyer

My most sincere apologizes. The problem was entirely with my own code. I am sorry for wasting your time.

The problem was the -1 I was applying to X.
pQPixel(fBitmap)[(fWidth*iY)+(iX-1)] := MyPixel;
now reads
pQPixel(fBitmap)[(fWidth*iY)+iX] := MyPixel;

As an excuse I can only give that I have just recently been working on a console object. Where (1,1) = (0,0) . I had gotten into the "habit" of applying -1 onto my coordinates.

Although I am still experiencing problems, they are not with anything other than my own code. Thank you very much for help. And I am very sorry for bothering you.

Vit Kavan
to Jon Sawyer

Hi everyone,

this is what I get during compile (porting some Delphi code):

Error: call by var parameters have to match exactly: Got "Array Of TVertex3D" expected "Array Of TVertex3D"

this is the function declaration:

procedure TranslateArr(xc,yc,zc:single;var Src,Dst:array of
TVertex3D;count:int);

this is how I call it:

TranslateArr(Center.x,Center.y,Center.z,Vertices,Vertices2,VertexCount);
any ideas? bug? unsupported feature?

cu,
VTech

______________________________________________________________________ Reklama:
Od dubna si můžete volat levněji. Pořiďte si službu Moje Jednička a volejte svému nejbližšímu jen za 1,50 Kč za minutu. www.oskarmobil.cz

Jonas Maebe
to Vit Kavan
Vit Kavan wrote 13 lines
Error: call by var parameters have to match exactly: Got "Array Of TVertex3D" expected "Array Of TVertex3D" this is the function declaration: procedure TranslateArr(xc,yc,zc:single;var Src,Dst:array of TVertex3D;count:int); this is how I call it: TranslateArr(Center.x,Center.y,Center.z,Vertices,Vertices2,VertexCount) ; any ideas? bug? unsupported feature?

What is the declaration of Vertices and Vertices2? I suspect it's a problem with confusion of open and dynamic arrays in the compiler though, maybe we should add those keywords when constructing the name of a type to write out.

Jonas

Unknown author
to Jonas Maebe

JM> What is the declaration of Vertices and Vertices2?

  TVertex3D = record
   x,y,z : float;   //float = single
  end;
  Vertices : array of TVertex3D;
  Vertices2 : array of TVertex3D;
Peter Vreman
to Unknown author
you wrote 8 lines
JM> What is the declaration of Vertices and Vertices2? TVertex3D = record x,y,z : float; //float = single end; Vertices : array of TVertex3D; Vertices2 : array of TVertex3D;

Can you provided a compilable example that we can use for the test suite.

Peter