Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Explain Something To Me 1

Status
Not open for further replies.

Glenn9999

Programmer
Jun 19, 2004
2,312
US
I notice it's seeming easy to cause errors, nightmares, and problems in Delphi. The latest example:

Code:
mem_limit := in_limit * 1024 * 1024;

items_blocked := mem_limit div in_lrecl;
writeln('Items blocked: ', items_blocked);

memsize := items_blocked * in_lrecl;
writeln('Mem size: ', memsize);

All vars are declared as longint (and were DWord). The operators in the calculations all have been provided in input.

The first line takes down FPC with an RTE 217, so I don't get to the other lines (Delphi is fine with the first line).

But Delphi executes and does nothing with the last 4 lines, seemingly (the two writes do not appear in CONSOLE MODE).

Can anyone enlighten me?
 
maybe you have an overflow error??

use a try except block

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Nope. The code works perfectly in an non-OOP environment (I changed it away from OOP). So I'm thinking it's something about how the object with this code was set up.

Line #1 is one procedure method, Lines 2-5 are another procedure method. All the other variables in the methods are operating on properties within the object.

So I know what was causing the errors now. Question is how I would fix this if I were to get the idea to try OOP coding again.
 
Can you post the smallest OOP code example you can come up with that still has this issue? Obviously there's nothing intrinsically wrong with what you've posted - the issue lies elsewhere.
 
Sure. I'm hesitant to post too much, because of IP issues, but I can post the snippet with the 4 lines. Basically I was trying to do blocking calculations for a file given a record length and and then come back with the relevant stats to read the file in and process it in a memory block allocated by getmem (that part will be not in this example, but I never got that far when I was trying to code it as an object using OOP).

Anyway, I am trying to learn how to code in OOP (design objects, code them, etc, etc), but what I noticed seemed very weird...the RTE 217 is a "segmentation fault" for FWIW.

The object with methods is such:
Code:
vfalloc = class(TObject)
   memsize: longint;
   mem_limit: longint;
   items_blocked: longint;

   procedure setmemlimit(in_limit: longint);
   procedure getmemsize(in_lrecl: longint);
 end;

procedure vfalloc.setmemlimit(in_limit: longint);
   { sets upper limit we will allocate in MB - placed here so
     the upper limit of memory can be set through configuration
     by the end user }
    begin
      mem_limit := in_limit * 1024 * 1024;
    end;


  procedure vfalloc.getmemsize(in_lrecl: longint);
    { given an input lrecl gets an appropriately blocked
      section of memory under mem_limit size.  Sets memsize }
    begin
      items_blocked := mem_limit div in_lrecl;
      writeln('Items blocked: ', items_blocked);

      memsize := items_blocked * in_lrecl;
      writeln('Mem size: ', memsize);
    end;

Using said object, like I said there's more, but I'm just showing the relevant sections of both object and mainline code that are causing me troubles. The errors occur in the process of what you see.

Code:
var
  memalloc: vfalloc;

with memalloc do
   begin
     setmemlimit(64);
     getmemsize(memory_size);
   end;
 
and to be sure, where you say

Code:
var
  memalloc: vfalloc;

with memalloc do
  begin
    setmemlimit(64);
    getmemsize(memory_size);
  end;

you do actually instantiate memalloc don't you?
Code:
var
  memalloc: vfalloc;
begin
  memalloc := vfalloc.Create;
  with memalloc do
    begin
      ....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top