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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access violation assigning LongInt to Integer? 1

Status
Not open for further replies.

djjd47130

Programmer
Nov 1, 2010
480
US
I am doing some lower level work with saving bitmap images to a file using scanline. After reading a LongInt from the file (as the program was already developed this way), I try simply assigning this value to an Integer within an object. This integer is actually a published property of the object, which calls a SetValue procedure, where I assign it (and other stuff). My object is for sure created just 4 lines of code before I try this, and cannot figure it out.

Here's a snippet of my code:
Code:
var
  TID: LongInt; //Tried as integer too
begin
.........
          bmp:= TBitmap.Create;  {color 24 bit xor map holder}
          try
            bmp.height:= IHeight;
            bmp.width:= IWidth;
            bmp.pixelformat:= pf24bit;
            for i:= 0 to ImageCount - 1 do begin
              RowLength:= (IWidth *  24) div 8;
              T:= TJDThumbnail.Create; //CREATING THE OBJECT
                T.ID:= 0;
                BlockRead(f, TID, 12, amt); //READING LONGINT VALUE FROM FILE
                if amt <> 12 then EM:= EM + 'ID - File Format Incorrect'+#10;
                T.ID:= TID; //THIS IS WHERE IT'S ASSIGNED TO THE INTEGER (SEE BELOW)
                for y:= 0 to IHeight - 1 do begin
                  pb:= bmp.ScanLine[y];  {get ^ address of first byte on row}
                  BlockRead(f, pb^, RowLength, amt);
                  if amt <> RowLength then
                    EM:= EM + 'Read incorrect amount of line data ('+
                      IntToStr(amt)+'/'+IntToStr(RowLength)+')' + #10;
                end;
                T.Bitmap.Assign(bmp);
              fThumbs.AddObject(IntToStr(T.ID), T);
            end;
          finally
            bmp.free;
          end;
..........

  TJDThumbnail = class(TPersistent)
  private
    fID: Integer; //ACTUAL VARIABLE OF INTEGER
    fBitmap: TBitmap;
    procedure SetID(Value: Integer);
  public
    constructor Create;
    destructor Destroy; override;
    property Bitmap: TBitmap read fBitmap;
  published
    property ID: Integer read fID write SetID; //PROPERTY OF INTEGER
  end;

..........

procedure TJDThumbnail.SetID(Value: Integer);  
begin
  fID:= Value; //ACTUAL ASSIGNING OF LONGINT TO INTEGER
end;



JD Solutions
 
PS - All was working perfectly until I added the ID to the contents saved in the file. Strange thing is, after manually assigning the object's ID property to 0, I read that variable in debug and it read some ridiculous negative number like -38978964598.


JD Solutions
 
In your constructor method TJDThumbnail.Create, are you actually calling [tt]inherited Create[/tt]?
 
euh, as far as I remember an integer(longint) is only 4 bytes!!!

so your line :

Code:
 BlockRead(f, TID, 12, amt); //READING LONGINT VALUE FROM FILE
will read 4 bytes from your file into your Longint var AND will trash 8 bytes off memory adjacent to the memory location of your TID variable (and thus causing random AV's)

/Daddy

so this should be ok (don't forget to adapt the writing part)
Code:
 BlockRead(f, TID, 4, amt); //READING LONGINT VALUE FROM FILE

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
@Griffyn: It's not an inherited constructor, no override
@whosrdaddy: That worked, no more AV's.. I've just never used a LongInt before, or with files using this method. Does it need to be a LongInt to be saved into a file? because I need to save large values. It's specifically saving thumbnail images to a single file, indexed with a unique ID. the unique ID corresponds with the database's record of an inventory item, where the thumbnail image being stored is the image of that inventory item. Every image is the same size, as specified in the beginning of the file. The size of this ID needs to be able to handle the max size of an int type in MSSQL, as this is where the value comes from.


JD Solutions
 
JD,

in fact LongInt is the same as Integer = 4 bytes signed integer.
Mssql int normally corresponds to Delphi Integer.

Cheers,
Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Ahh gotcha, see it's these little details which I'm fuzzy with, like signed integer... I still have a lot to learn

JD Solutions
 
Thanks, that made it much more clear. I got it working like butter now, and soon will be posting it in the FAQ as 'How do I store thumbnail images in a single file?'. It's a drop-in component with very simple functionality.


JD Solutions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top