|
| Re: EAccess violation when try to remove object for Tlistobject |
 |
Tue, 11 Dec 2007 08:13:19 -050 |
On Tue, 11 Dec 2007 11:47:50 +0200, Stanislav Nedelchev wrote:
> For example I test it with method Extract
> FForms.Extract(TfBuddyForm(FForms[i]));
>
> and it's never crahs but it's not free memory.
It sounds like it has nothing to do with the list, but that something
happens when you Free the form (i.e. some code in the destructor an
error in a component, etc.)
What happens if you do:
TfBuddyForm(FForms[i]).Free;
And ignore the list for the moment?
By the way this group is for discussing object orientated design, if
you choose an appropriate group you will get better answers.
--
Marc Rohloff [TeamB]
|
| Post Reply
|
| Re: EAccess violation when try to remove object for Tlistobject |
 |
Tue, 11 Dec 2007 10:09:53 -030 |
TObjectList has two creation methods. The default (FForms :=
TObjectList.Create;) creates a list of objects, forms in your case, and
destroy them when you do a "Remove".
If you just want to remove the reference to the forms in the list, and
leave the forms in memory, you should create the TObjectList this way:
FForms := TObjectList.Create(False);
The parameter tells the list it doesn't own the forms, but only a
reference to them, if you use "True" as parameter, the list will
destroy
de forms when you call Delete, Remove or Free.
From Delphi 7 help:
"Description
Create creates a TObjectList instance. If no parameter is specified, or
if the constructor is called with AOwnsObjects set to true, the new
TObjectList is initialized with OwnsObjects set to true."
I hope it helps.
Leonardo M. Ramé
http://leonardorame.blogspot.com
Stanislav Nedelchev escribió:
> For example I test it with method Extract
> FForms.Extract(TfBuddyForm(FForms[i]));
>
> and it's never crahs but it's not free memory.
>
> Regards.
>
> Stanislav Nedelchev написа:
>> Hi to everybody I have problem when I try to remove object from
TobjectList.
>> FForms is TobjectList
>> TfBuddyForm is a Tform.
>> I can change caption if the form it's seams that this object exists
but
>> I can delete it. The most strange thng is that sometime it's not
crash.
>> I'm using Jedi VCL in this form . Can anybody give Idea what to look
to
>> fix this problem.
>>
>>
>> Here is the example code.
>>
>> function TMainForm.RemoveForm(BuddyName_: string): TfBuddyForm;
>> var
>> i: integer;
>> l: integer;
>> begin
>> result := nil;
>> i := 0;
>> l:=0;
>> BuddyName_ := lowercase(BuddyName_); //guarantee case insensitive
search
>> while (result = nil) and (i < FForms.Count) do
>> begin
>> if lowercase(TfBuddyForm(FForms[i]).BuddyName) = BuddyName_ then
>> begin
>> result := TfBuddyForm(FForms[i]);
>> Result.Caption := '------'; //Works
>> FForms.Remove(TfBuddyForm(FForms[i])); //crash
>> end
>> else
>> inc(i)
>> end;
>> {
>> for l := lbForms.Items.Count - 1 downto 0 do
>> if lbForms.Items.Strings[l] = BuddyName_ then
>> lbForms.Items.Delete(l);
>> }
|
| Post Reply
|
| EAccess violation when try to remove object for Tlistobject |
 |
Tue, 11 Dec 2007 11:42:51 +020 |
Hi to everybody I have problem when I try to remove object from TobjectList.
FForms is TobjectList
TfBuddyForm is a Tform.
I can change caption if the form it's seams that this object exists but
I can delete it. The most strange thng is that sometime it's not crash.
I'm using Jedi VCL in this form . Can anybody give Idea what to look to
fix this problem.
Here is the example code.
function TMainForm.RemoveForm(BuddyName_: string): TfBuddyForm;
var
i: integer;
l: integer;
begin
result := nil;
i := 0;
l:=0;
BuddyName_ := lowercase(BuddyName_); //guarantee case insensitive search
while (result = nil) and (i < FForms.Count) do
begin
if lowercase(TfBuddyForm(FForms[i]).BuddyName) = BuddyName_ then
begin
result := TfBuddyForm(FForms[i]);
Result.Caption := '------'; //Works
FForms.Remove(TfBuddyForm(FForms[i])); //crash
end
else
inc(i)
end;
{
for l := lbForms.Items.Count - 1 downto 0 do
if lbForms.Items.Strings[l] = BuddyName_ then
lbForms.Items.Delete(l);
}
|
| Post Reply
|
| Re: EAccess violation when try to remove object for Tlistobject |
 |
Tue, 11 Dec 2007 11:47:50 +020 |
For example I test it with method Extract
FForms.Extract(TfBuddyForm(FForms[i]));
and it's never crahs but it's not free memory.
Regards.
Stanislav Nedelchev написа:
> Hi to everybody I have problem when I try to remove object from
TobjectList.
> FForms is TobjectList
> TfBuddyForm is a Tform.
> I can change caption if the form it's seams that this object exists but
> I can delete it. The most strange thng is that sometime it's not crash.
> I'm using Jedi VCL in this form . Can anybody give Idea what to look to
> fix this problem.
>
>
> Here is the example code.
>
> function TMainForm.RemoveForm(BuddyName_: string): TfBuddyForm;
> var
> i: integer;
> l: integer;
> begin
> result := nil;
> i := 0;
> l:=0;
> BuddyName_ := lowercase(BuddyName_); //guarantee case insensitive search
> while (result = nil) and (i < FForms.Count) do
> begin
> if lowercase(TfBuddyForm(FForms[i]).BuddyName) = BuddyName_ then
> begin
> result := TfBuddyForm(FForms[i]);
> Result.Caption := '------'; //Works
> FForms.Remove(TfBuddyForm(FForms[i])); //crash
> end
> else
> inc(i)
> end;
> {
> for l := lbForms.Items.Count - 1 downto 0 do
> if lbForms.Items.Strings[l] = BuddyName_ then
> lbForms.Items.Delete(l);
> }
|
| Post Reply
|
| Re: EAccess violation when try to remove object for Tlistobject |
 |
Tue, 11 Dec 2007 12:04:09 -030 |
That's exactly why I think the problem is the TObjectList. When you
remove an item from the list, and the list was created without
Create(False), the form is Destroyed. If after that, you try to call
Form.Free, an Access Violation is raised because the form wasn't there
anymore.
Leonardo M. Ram
http://leonardorame.blogspot.com
Marc Rohloff [TeamB] escribi:
> On Tue, 11 Dec 2007 11:47:50 +0200, Stanislav Nedelchev wrote:
>
>> For example I test it with method Extract
>> FForms.Extract(TfBuddyForm(FForms[i]));
>>
>> and it's never crahs but it's not free memory.
>
> It sounds like it has nothing to do with the list, but that something
> happens when you Free the form (i.e. some code in the destructor an
> error in a component, etc.)
>
> What happens if you do:
> TfBuddyForm(FForms[i]).Free;
> And ignore the list for the moment?
>
> By the way this group is for discussing object orientated design, if
> you choose an appropriate group you will get better answers.
|
| Post Reply
|
|
|
|
|
|
|
|
|
|