Link to home
Start Free TrialLog in
Avatar of mhanefitel
mhanefitelFlag for Türkiye

asked on

Problem with the TPictureList

Hi,

   I created a class(TUrunKartiClass)  with TPictureList. But when i load pictures to TPictureList class and then try to use it in a function i get an access violation error as described in the code.  When i run through the code i get an access violaiton error here : myPicture.Graphic.SaveToStream(TempStream);
   What is wrong with this.
   Thanks in advance.



//THIS IS THE CLASS I HAVE CREATED.

unit UrunKartiClass;

interface

uses  Graphics, Contnrs, Classes;

type
     TPictureList = Class(TObjectList)
         protected
            procedure SetResim(I: Integer; APicture: TPicture);
            function GetResim(I: Integer): TPicture;
         public
            function Add(APicture: TPicture): Integer;
            property Items[I: Integer]: TPicture read GetResim write SetResim;

     End;

   TUrunKartiClass = class
         private
            UrunKodu         : String;
            UrunAdi          : String;
            UrunGrubu        : String;
            Birimi           : String;
            UrunBoyutlari    : String;
            Aciklama         : String;
            EnSonKayitTarihi : String;

         published

         public
                UrunResimleriList :  TPictureList;

                Constructor   Create(
                      pUrunKodu         : String;
                      pUrunAdi          : String;
                      pUrunGrubu        : String;
                      pBirimi           : String;
                      pUrunBoyutlari    : String;
                      pAciklama         : String
                   );Overload;

                Constructor Create(pUrunKodu : String); Overload;

                Function  Kaydet : Boolean;
                Function  UrunResimleriniKaydet( pUrunResimleri : array of TPicture) : Boolean;
                Function  UrunKartiKayitliMi : Boolean;

   end;



implementation

uses DatabaseBaglantisi, MSAccess, General, Sysutils, Windows,  DB;



Procedure TPictureList.SetResim(I : Integer; APicture : TPicture) ;
begin
  inherited Put(I, APicture);
end;

Function TPictureList.GetResim(I : Integer) : TPicture ;
begin
     Result := TPicture(inherited Get(I));
end;

Function TPictureList.Add(APicture : TPicture) : Integer ;
begin
     Result := inherited Add(APicture);
end;


Constructor TUrunKartiClass.Create(
      pUrunKodu         : String;
      pUrunAdi          : String;
      pUrunGrubu        : String;
      pBirimi           : String;
      pUrunBoyutlari    : String;
      pAciklama         : String);
begin
    UrunKodu           := pUrunKodu;
    UrunAdi            := pUrunAdi;
    UrunGrubu          := pUrunGrubu;
    UrunBoyutlari      := pUrunBoyutlari;
    Aciklama           := pAciklama;
end;

Constructor TUrunKartiClass.Create(pUrunKodu : String);
begin
    //BU CONSTRUCTOR ILE DATABASE DEN KAYIT BULUNUP OBJECT E ATANACAK.
end;

Function  TUrunKartiClass.Kaydet : Boolean;
var TempQuery : TMSQuery;
    i : Integer;
    TempStream : TMemoryStream;
    myPicture : TPicture;
begin
    //DEFAULT OLARAK KAYDETMEDI OLARAK VARSAYALIM.
    Result := False;
    if ConnectToDB then
    begin
        try
           Mouse_Bekleme;
           DBConnection.StartTransaction;
           //INSERTS UPDATES DELETES COME HERE
           try
             TempQuery := TMSQuery.Create(TempQuery);
             TempQuery.Connection := DBConnection;
             TempQuery.SQL.Clear;
             TempQuery.Params.Clear;
             TempQuery.SQL.Text := 'Insert into Tab_Urun_Kartlari Values(' +
                                   ':ProjeGrubu, :ProjeKodu, :UrunKodu, :UrunAdi, :UrunGrubu, :Birimi, ' +
                                   ':UrunBoyutlari, :Aciklama, :EnSonKayitTarihi ' +
                                   ')';
             TempQuery.ParamByName('ProjeGrubu').AsInteger      := 1;
             TempQuery.ParamByName('ProjeKodu').AsInteger       := 1;
             TempQuery.ParamByName('UrunKodu').AsString         := Trim(UrunKodu);
             TempQuery.ParamByName('UrunAdi').AsString          := Trim(UrunAdi);
             TempQuery.ParamByName('UrunGrubu').AsString        := Trim(UrunGrubu);
             TempQuery.ParamByName('Birimi').AsString           := Trim(Birimi);
             TempQuery.ParamByName('UrunBoyutlari').AsString    := Trim(UrunBoyutlari);
             TempQuery.ParamByName('Aciklama').AsString         := Trim(Aciklama);
             TempQuery.ParamByName('EnSonKayitTarihi').AsString := EnSonKayitTarihi;
             TempQuery.Execute;

             MesajGoster(IntToStr(UrunResimleriList.Count), mb_ok);
             for i := 0 to  UrunResimleriList.Count - 1 do
             begin
                 TempQuery.SQL.Clear;
                 TempQuery.Params.Clear;
                 TempQuery.SQL.Text := 'Insert into Tab_Urun_Resimleri Values(' +
                                       ':ProjeGrubu, :ProjeKodu, :UrunKodu, :ResimID, :UrunResmi ' +
                                       ')';
                 TempQuery.ParamByName('ProjeGrubu').AsInteger      := 1;
                 TempQuery.ParamByName('ProjeKodu').AsInteger       := 1;
                 TempQuery.ParamByName('UrunKodu').AsString         := Trim(UrunKodu);
                 TempQuery.ParamByName('ResimID').AsInteger         := i + 1;
                 try
                   //SIMDI ÜRÜN RESIMLERINI KAYDEDELIM.
                   TempStream := TMemoryStream.create;
                   TempStream.Position := 0;
                   myPicture := TPicture.Create;
                   myPicture := UrunResimleriList.GetResim(i);
                   MesajGoster(IntToStr(i), mb_ok);
                   //*************I GET ACCESS VIOLATION ERROR HERE**********
                   myPicture.Graphic.SaveToStream(TempStream);
                   TempQuery.ParamByName('UrunResmi').LoadFromStream(TempStream,ftBlob);
                   TempQuery.Execute;
                 finally
                   TempStream.Free;
                   FreeAndNil(myPicture);
                 end;
             end;
           finally
             TempQuery.Free;
           end;
           DBConnection.Commit;
           Result := True;
           Mouse_Normal;
           KaydedildiMesajiGoster;
        Except
         on E : Exception do
         begin
           DBConnection.Rollback;
           Mouse_Normal;
           KaydedilemediMesajiGoster;
           MesajGoster2('HATA OLUSTU. LÜTFEN HATAYI NOT ALIP BILGI ISLEME BILDIRINIZ. HATA AÇIKLAMASI = '+E.Message, mb_ok);
           Result := False;
         end;
        end;
        DisconnectFromDB;
    end else Result := False;
end;


Function  TUrunKartiClass.UrunResimleriniKaydet( pUrunResimleri : array of TPicture) : Boolean;
begin



end;


Function  TUrunKartiClass.UrunKartiKayitliMi() : Boolean;
begin

end;

end.





// I USE ABOVE CLASS HERE

procedure TfrmUrunTanimlama.btnKaydetClick(Sender: TObject);
var
    TempStream: TMemoryStream;
    i : Integer;
    TempUrunKarti : TUrunKartiClass;
    myPicture : TPicture;
begin
     TempUrunKarti := TUrunKartiClass.Create(
                                              Trim(txtUrunKodu.Text),
                                              Trim(txtUrunAdi.Text),
                                              Trim(txtUrunGrubu.Text),
                                              Trim(comboBirimi.Text),
                                              Trim(txtUrunBoyutlari.Text),
                                              Trim(memoAciklama.Lines.Text)
                                            );

     TempUrunKarti.UrunResimleriList := TPictureList.Create(True);

     for i := 0 to  cxGrid1View1.DataController.RecordCount - 1 do
     begin
         try
           TempStream := TMemoryStream.Create;
           TempStream.Position := 0;
           myPicture  := TPicture.Create;
           cxImage.LoadPicture(myPicture, cxImage.GetGraphicClassByName('TJPEGImage'), cxGrid1View1.DataController.Values[i,cxGrid1View1UrunResimleri.Index]);
           myPicture.Graphic.SaveToStream(TempStream);
           TempUrunKarti.UrunResimleriList.Add(myPicture);
         Finally
           TempStream.Free;
//           objPicture.Free;
           FreeAndNil(myPicture);
         end;
     end;
     MesajGoster(Inttostr(TempUrunKarti.UrunResimleriList.Count), mb_ok);

     TempUrunKarti.Kaydet;


     TempUrunKarti.Free;
end;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I didn't finished my comment after correcting the code.
Your biggest errors are that you are creating objects before assigning the variables to others instance in  lists. Therefore you are leaking memory. And worse, you destroy those instance while they are still stored in the list. When you try to access to the objects in the list again after that, the memory zone they are pointing to have been released which cause the access violations.
Avatar of mhanefitel

ASKER

Thanks. I made corrections as you said.