PROBLEM SOLVED ! :D
From: Skybuck Flying (nospam_at_hotmail.com)
Date: 02/21/04
- Next message: Nicholas Sherlock: "Re: Nonononono NEW PROBLEM ;)"
- Previous message: Skybuck Flying: "Finalize is useless on raw data pointers !!!"
- In reply to: Skybuck Flying: "Finalize is useless on raw data pointers !!!"
- Next in thread: Skybuck Flying: "Initializing/Finalizing each field might be possible too :D"
- Reply: Skybuck Flying: "Initializing/Finalizing each field might be possible too :D"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 21 Feb 2004 03:59:09 +0100
Hello boys and girls.
Skybuck is back on the horse again ! :D
The problem is now solved !
It was a very much learning experience for me =D
Thx for all the tips :D
BUTBUTBUT the thing is... The solution is only 50% because I have to do it
manually but it is BETTER THAN NOTHING :D
The double linked list as I mentioned in this previous post will not be able
to successfully destroy the data of it's nodes.
Ofcourse let's be REALISTIC for a second !
BECAUSE I AM BEING TOTALLY UNREALISTIC LOL :D
STRINGS ARE LIKE CLASSESS !!!!!!!!!!!!!! THEY NEED TO BE DESTROYED MANUALLY
IN THIS CASE.
Yesyesyesyesyesyes I hear you thinking BUT BUT BUT BUT WHAT ABOUT REFERENCE
COUNTING ?!
WELL THIS IS A CLEAR CASE WHERE REFERENCING COUNT BITES YOU IN YOUR ASS.
IT'S PRETTY FUCKING USELESS IN THIS CASE... JUST LIKE A CLASS, YOU NEED TO
CALL CLASS.DESTROY
SINCE STRINGS DONT HAVE A DESTRUCTOR HOW CAN YOU GET RID OF THEM ?!
ANSWER: INITIALIZE AND FINALIZE THE COMPLETE STRUCTURE ?!
WHOOOOOAAA DOESN'T SEEMS THAT A LITTLE BIT EXTREME ?!
NOT REALLY... THINK OF FINALIZE AS A DESTRUCTOR FOR RECORDS !!??!! :D
YES SIR !
FINALIZE IS THE DESTRUCTOR FOR RECORDS lol.
Ok....
Now there is only one very very very very very very interesting problem
remaining with my double linked list.
How can I insert strings directly into the raw data pointers.
Rob mentioned something about that... I am not sure what the hell he was
talking about... but this discussion should not take
place in this thread...
This thread should slowly reach an end.... I expect nothing else but
comments on this thread lol.
( hoping that you guys agree with me in this new insight that finalize acts
like a destructor for records :P :D and initialize like a constructor for
records
I may add :D )
See bottom in case you interested in seeing the final solution :D
Bye,
Skybuck :D
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
{
version 0.06
This version represent more exactly how the double linked list works
because it uses raw pointers for the data pointer
Info is actually an external type which can be put into the double linked
list since the list is a generic list. (thx to the raw data pointers etc)
also the list has a datasize : longint variable which keeps track of
the size of the data in the nodes. all data is of the same size.
I'll put that fact into this code as well.
version 0.07
Now let's try to solve the problem by calling initialize and finalize
on a raw pointer.
If that doesn't work then use the tip from Rob that initialize and finalize
needs to know the size, maybe we can specify it.
I doubt that any of these two solutions will work but we'll see
It would be spectacular if it worked :D
let's try one thing at a time... let's try finalize first.
Since the memory leak is uuuggggllyy !
As I suspected... finalize is useless on raw pointers !
version 0.08
This version will show how to solve both problems by manually typecasting
and manually initialize and finalize it.
It's also interesting to note: that when I tried to open the new project
that delphi had access violations and exceptions... probably because
the previous version had bugs ! :D
So two new procedures will be created to represent this fact.
ManuallyInitializeData and ManuallyFinalizeData which represent external
procedures which are ofcourse not part of the list... but of the user of
the
list.
YES IT WORKS:
PROBLEM SOLVED (though manually, not automatic ;))
}
type
Pinfo = ^Tinfo;
Tinfo = record
mAddress : string;
mMembership : boolean;
end;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Edit1: TEdit;
CheckBox1: TCheckBox;
Edit2: TEdit;
CheckBox2: TCheckBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Data : pointer; // raw data pointer which is in node
// which points to the data which can be anything
DataSize : integer; // size of raw data which is the same for all nodes.
procedure ManuallyInitializeData;
procedure ManuallyFinalizeData;
procedure Setup;
procedure CreateData;
procedure DestroyData;
procedure SetData( const Address : string; const Membership : boolean );
procedure GetData( var Address : string; var Membership : boolean );
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ManuallyInitializeData;
begin
// Initialize( PInfo(Data) );
// hint: pointer expression needs no initialize/finalize need ^ operator ?
// dumbass you forgot the deference operator, good thing that delphi
// hints me about it ! :D
Initialize( PInfo(Data)^ );
end;
procedure TForm1.ManuallyFinalizeData;
begin
// Finalize( PInfo(Data) ); // same here.
Finalize( PInfo(Data)^ );
end;
// list like procedures:
// (except set/get data which is of a second generation list etc, which
// uses the original list )
// but this is not important, the code illustrates the problem well enough
;)
// in reality set data and get data is readdata and writedata and is of type
// pointer ofcourse. the next generation list which use the original one
// has indeed set data and get data like this :D so it represent a mix
// of first generation and second generation lists :D
procedure TForm1.Setup;
begin
Data := nil;
DataSize := sizeof(Tinfo);
end;
procedure TForm1.CreateData;
begin
GetMem( Data, DataSize );
// Initialize( Data^ );
end;
procedure TForm1.DestroyData;
begin
// let's try and call finalize on a raw pointer.
// Finalize( Data^ ); // hint: Expression needs no initialize/finalize. ???
// I am guessing delphi problably can't tell the size or delphi thinks
// it doesn't contain dynamic variables ?!
// let's try something else.
// Let's try to use the data size
// Finalize( Data^, DataSize ); // hint: Expression needs no
initialize/finalize. ???
// No it's the same problem, Delphi can't tell that a dynamic variable is
in it !!!
FreeMem( Data, DataSize );
end;
procedure TForm1.SetData( const Address : string; const Membership :
boolean );
var
Info : Pinfo;
i : integer;
begin
Info := PInfo(Data);
Info.mAddress := Address;
Info.mMembership := Membership;
// Data.mAddress := Address;
Info.mAddress := '';
// create a big string of exactly 100 MB
for i:=1 to 100*1024*1024 do
begin
Info.mAddress := Info.mAddress + 'A';
end;
Info.mMembership := Membership;
end;
procedure TForm1.GetData( var Address : string; var Membership : boolean );
var
Info : Pinfo;
begin
Info := PInfo(Data);
Address := Info.mAddress;
Membership := Info.mMembership;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Setup;
CreateData;
ManuallyInitializeData; // ok this should solve problem 1
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
SetData( Edit1.Text, CheckBox1.Checked );
end;
procedure TForm1.Button3Click(Sender: TObject);
var
s : string;
b : boolean;
begin
GetData( s, b );
Edit2.Text := s;
CheckBox2.Checked := b;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
ManuallyFinalizeData; // ok this should solve problem 2 the memory leak
DestroyData;
end;
end.
- Next message: Nicholas Sherlock: "Re: Nonononono NEW PROBLEM ;)"
- Previous message: Skybuck Flying: "Finalize is useless on raw data pointers !!!"
- In reply to: Skybuck Flying: "Finalize is useless on raw data pointers !!!"
- Next in thread: Skybuck Flying: "Initializing/Finalizing each field might be possible too :D"
- Reply: Skybuck Flying: "Initializing/Finalizing each field might be possible too :D"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]