help! access violation pointer

Pemrograman Delphi

help! access violation pointer

Postby mambamaestro » 25 Feb 2012, 16:52

saya punya kode sebagai berikut:

Code: Select all
  1. type

  2.   TJam = array of Integer;

  3.   THariJam = array[1..7] of TJam;

  4.  

  5.   TRuang = record

  6.     nm_ruang: string;

  7.     avl: THariJam; //availability

  8.   end;

  9.  

  10.   PRuang = ^TRuang;

  11.  

  12. ........

  13.  

  14. var

  15.   PR: PRuang;

  16.  ............  

  17. begin

  18. .........

  19.     GetMem(PR, sizeof(TRuang));    

  20.     SetLength(PR^.avl[1], Length(PR^.avl[1]) + 1); //sukses

  21.     SetLength(PR^.avl[2], Length(PR^.avl[2]) + 1); //error, access violation kenapa ya?

  22. .....

  23. end;


kenapa ya, pada
Code: Select all
  1. SetLength(PR^.avl[1], Length(PR^.avl[1]) + 1);


bisa sukses, tapi pada index 2 dalam kode
Code: Select all
  1. SetLength(PR^.avl[2], Length(PR^.avl[2]) + 1);


ternyata access violation. kenapa ya? padahal avl bertipe THariJam. Dimana THariJam adalah static array, yang jelas ada indexnya. kenapa error nya?
Mohon pencerahan.
mambamaestro
Member
 
Posts: 156
Joined: 06 May 2010, 14:14
Location: Surabaya

Re: help! access violation pointer

Postby herux » 25 Feb 2012, 17:19

Coba masukin variable PR^.avl[2] + 1 = ?
herux
Member
 
Posts: 132
Joined: 06 May 2010, 10:59

Re: help! access violation pointer

Postby D.E » 25 Feb 2012, 18:12

Ini aneh, kenapa berkehendak mendapatkan length dari un-initialized variable?
Tentu AVL dong...

SetLength(

PR^.avl[1], // Variable yang hendak di set length

Length(PR^.avl[1]) // Mau set length berapa banyak? Koq bacanya dari variable itu sendiri yg notabane belum di inisialisasi

+ 1)

Coba gini dulu:

Code: Select all
  1. SetLength(PR^.avl[1], 20);

  2. SetLength(PR^.avl[2], 20);


Ada AVL gak?
User avatar
D.E
Senior Member
 
Posts: 638
Joined: 04 May 2010, 18:12

Re: help! access violation pointer

Postby leledumbo » 25 Feb 2012, 19:06

Yang pertama berhasil kebetulan, n itu entah tunjuk ke pointer mana. Alesannya udah dijelasin om D.E.
E tapi menurut dokumentasi Length() emang mestinya ngasih 0 ke dynamic array yang belom diinisialisasi, bentar ta' tanyaken di milis.
leledumbo
Senior Member
 
Posts: 262
Joined: 24 May 2010, 15:58

Re: help! access violation pointer

Postby D.E » 25 Feb 2012, 19:28

Solusinya setelah GetMem isi memory ptr tersebut harus di reset:

Code: Select all
  1. GetMem(PR, sizeof(TRuang));

  2. FillChar(PR^, sizeof(TRuang), 0);

User avatar
D.E
Senior Member
 
Posts: 638
Joined: 04 May 2010, 18:12

Re: help! access violation pointer

Postby leledumbo » 25 Feb 2012, 20:03

Syuda dibales, ternyata solusinya simpel: kembalilah ke jalan Pascal yang benar.
Alias: pake New instead of GetMem dan Dispose instead of FreeMem.
Karena ternyata New = GetMem + Initialize, Dispose = Finalize + FreeMem.
leledumbo
Senior Member
 
Posts: 262
Joined: 24 May 2010, 15:58

Re: help! access violation pointer

Postby D.E » 25 Feb 2012, 20:18

He baru ingat saya ama New & Dispose.
Sip top markotop gut marsogut :)
User avatar
D.E
Senior Member
 
Posts: 638
Joined: 04 May 2010, 18:12

Re: help! access violation pointer

Postby mambamaestro » 25 Feb 2012, 20:29

baik solusi mas lele or mas dumbo dah kucoba. thanks all. tapi solusi mas lele lebih ringkes keknya. trims. btw, jika misalkan variabel bertipe pointer ke record, dimana record tadi mempunyai field bertipe pointer juga, apakah dengan men-dispose variabel tersebut, otomatis field recordnya juga ikut ter-dispose? trims.
mambamaestro
Member
 
Posts: 156
Joined: 06 May 2010, 14:14
Location: Surabaya

Re: help! access violation pointer

Postby mambamaestro » 25 Feb 2012, 20:30

sori, mas lele dan mas D.E
mambamaestro
Member
 
Posts: 156
Joined: 06 May 2010, 14:14
Location: Surabaya

Re: help! access violation pointer

Postby D.E » 25 Feb 2012, 20:36

mambamaestro wrote:baik solusi mas lele or mas dumbo dah kucoba. thanks all. tapi solusi mas lele lebih ringkes keknya. trims. btw, jika misalkan variabel bertipe pointer ke record, dimana record tadi mempunyai field bertipe pointer juga, apakah dengan men-dispose variabel tersebut, otomatis field recordnya juga ikut ter-dispose? trims.

Sepertinya TIDAK, jika sub pointer dialokasikan secara explisit maka harus di dispose secara explisit juga.
User avatar
D.E
Senior Member
 
Posts: 638
Joined: 04 May 2010, 18:12

Next

Who is online

Users browsing this forum: No registered users and 1 guest

cron