Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.pascal.misc > Newsgroup comp.lang.pascal.delphi.misc

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 06-24-2012, 05:25 PM
Stark
Guest
 
Posts: n/a
Default Invalid pointer operation

I get the error message on this line of the FormClose:
BudgetsList.Free;

This is an TObjectList which is so declared (together with the TMonthly
class whose instances then are added to the list):
var
BudgetsList: TObjectList;
oMonthly: TMonthly;

It is used as follows::

BudgetsList:= TObjectList.Create(true);
do while ....
oMonthly:= TMonthly.Create(...);
BudgetsList.Add(oMonthly);
end;

As soon as I close the form I get the Invalid pointer operation error; I
insist in closing the form and after two or three attempts it closes.Any
idea ?

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 06-24-2012, 06:01 PM
Jamie
Guest
 
Posts: n/a
Default Re: Invalid pointer operation

Stark wrote:
> I get the error message on this line of the FormClose:
> BudgetsList.Free;
>
> This is an TObjectList which is so declared (together with the TMonthly
> class whose instances then are added to the list):
> var
> BudgetsList: TObjectList;
> oMonthly: TMonthly;
>
> It is used as follows::
>
> BudgetsList:= TObjectList.Create(true);
> do while ....
> oMonthly:= TMonthly.Create(...);
> BudgetsList.Add(oMonthly);
> end;
>
> As soon as I close the form I get the Invalid pointer operation error; I
> insist in closing the form and after two or three attempts it closes.Any
> idea ?

If you free any of the objects in the list during your application run
time, you want to remove the entry from the list. Otherwise, when you
terminate your program, an attempt is main to call the TObject.Free
members of each item in the list and if you have not removed it from the
list, the pointer value of the old object is still there, even though it
no longer is valid.

Last time I looked, the TobjectList class cycles through the list of
non nil pointers and calls the "Tobject.Free" for each
entry. This is done when you destroy the TObjectList instant..

Jamie

Reply With Quote
  #3 (permalink)  
Old 06-24-2012, 09:54 PM
Stark
Guest
 
Posts: n/a
Default Re: Invalid pointer operation

> If you free any of the objects in the list during your application run
> time, you want to remove the entry from the list. Otherwise, when you
> terminate your program, an attempt is main to call the TObject.Free
> members of each item in the list and if you have not removed it from the
> list, the pointer value of the old object is still there, even though it
> no longer is valid.
>
> Last time I looked, the TobjectList class cycles through the list of non
> nil pointers and calls the "Tobject.Free" for each
> entry. This is done when you destroy the TObjectList instant..
>
> Jamie
>

There are no free at runtime.. The list count is correct before the free,
then raises the error. After the second attempt to close the form, the count
goes to 3 and then to 0.

Reply With Quote
  #4 (permalink)  
Old 06-25-2012, 04:40 PM
JJ
Guest
 
Posts: n/a
Default Re: Invalid pointer operation

"Stark" <franco.jommi@tin.it> wrote:

> There are no free at runtime.. The list count is correct before the
> free, then raises the error. After the second attempt to close the form,
> the count goes to 3 and then to 0.


It could be that at the time before it was freed, the memory structure is
already broken. Try the same test using TStringList for the list, and a
string as the month.
Reply With Quote
  #5 (permalink)  
Old 06-25-2012, 09:55 PM
Stark
Guest
 
Posts: n/a
Default Re: Invalid pointer operation

Problem found!
I was doing the following:
var
TempList: TStringList;
.......................
TempList:= TMensiliB ( BudgetsList[i] ).GetMomths );
......................
TempList.Free;

This last statement was cancelling the pointer from the list and that's why
I was getting Invalid pointer operation in the ObjectList Free.


Reply With Quote
  #6 (permalink)  
Old 06-26-2012, 06:04 AM
alanglloyd@aol.com
Guest
 
Posts: n/a
Default Re: Invalid pointer operation

On Jun 25, 10:55*pm, "Stark" <franco.jo...@tin.it> wrote:
> Problem found!
> I was doing the following:
> var
> * *TempList: TStringList;
> * *.......................
> * *TempList:= TMensiliB ( BudgetsList[i] ).GetMomths );
> * *......................
> * *TempList.Free;
>
> This last statement was cancelling the pointer from the list and that's why
> I was getting Invalid pointer operation in the ObjectList Free.


You have to decide/control where objects are Free'd, and make your own
rules eg

a) I'll specifically free everything

or

b) My TObjectList will free everything (defaults to true on using
Create)

If the former you must set TObjectList.OwnsObjects to false (or use
Create(false); If the latter, never Free an object in the list, or
always set the OL item to nil when you do Free one.

Alternatively you could add a Notify method to your TObjectList
descendant to do your Free'ing & Nil'ing in the object list (I think
the parent TObjectList.Free only Free's the item).

Alan Lloyd

Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 06:45 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.