Recent

Author Topic: Dissapearing object  (Read 4673 times)

iru

  • Sr. Member
  • ****
  • Posts: 321
Dissapearing object
« on: January 02, 2011, 12:36:18 pm »
Gentlefolk,

Environment: Win XP, SP3, Laz 0.29, 28602.

I have a small class as follows:

unit UnitMultiReg;

type
  TRegMulti = class(TObject)
    RegArray : array of Integer; // [0..cMaxRegs-1] of Integer;
    RegSize  : Integer;           // Local size of the array
    Function SetRegArraySize(InSize : Integer) : Integer;
    Function ClearReg(InReg : Integer) : Integer;
   end;

var
  UR : TRegMulti;

In another module I create the instance UR with

     UnitMultiReg.UR := TRegMulti.Create; 

All compile clean.
At run time the Function SetRegArraySize(InSize : Integer) : Integer; is called,
the array and RegSize are set to the value of InSize.

In debug all looks OK.

Later in the program the program fails in Function ClearReg.
In Debug the rray shows as RegArray = array(nil) and RegSize = 0.

Nowhere do I destroy or free UR.

Somehow I am losing UR and/or it's contents or missing something conceptual.

Any suggestions, ideas, whatever?

Thanks, Ian

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Dissapearing object
« Reply #1 on: January 02, 2011, 01:10:34 pm »
Have you checked that all loops use proper range (0 to RegSize-1)?

Also, are you by any chance referring in code to TRegMulti.ClearReg() instead of UR.ClearReg() ?

iru

  • Sr. Member
  • ****
  • Posts: 321
Re: Dissapearing object
« Reply #2 on: January 03, 2011, 11:07:27 pm »
I have checked all what you have suggested.

When I look at the UR structure in debug I see one addressfor UR when the array size is modified and another address when I execute the ClearAllReg method.

Looks like somehow I am looking at another instance of UR.

I created a small test project with similar structures (forms/frames/etc) and the UR (same address) is accessible to the SetRegArraySize method and ClearAllReg methods. The clear the reg code works OK.

Odd, will chase it further in the original project.....

Ian

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Dissapearing object
« Reply #3 on: January 04, 2011, 12:34:05 am »
Can you show some more code, maybe contains of ClearReg() ? Do you use pointers with the class?

iru

  • Sr. Member
  • ****
  • Posts: 321
Re: Dissapearing object
« Reply #4 on: January 04, 2011, 11:59:09 pm »
Gentlefolk,

The problem is solved, I am the problem.

For debug purposes I added a constructor to the class and then put a breakpoint in the constructor.

To my surprise I came through there twice. The first time in the form create code of the main form and then in the form activate code of the same form. There buried down in the activate code was another  create on UR.

I must have put it there along time ago for some reason and things worked correctly until I changed the array in UR from a static size to a dynamic. Then my problems started.

Thanks for the interest and assistance.

Ian.

 

TinyPortal © 2005-2018