PDA

View Full Version : Object References nilled



pstudio
01-07-2011, 06:34 PM
Hi again,
Seems hacking together a game engine in a week or two isn't so problem free as I could have hoped for.
So my problem is that I have some object references that are automatically nilled by Delphi.

Imagine we have class B: (Simplified code of course)


class B = class
private
FRef: A; // A is some class
public
procedure DoSomething;
end;

procedure DoSomething;
begin
FRef.SomeProcedure;
end;


So an instance of B will have a reference to an instance of A. In my code I know FRef has been set when an instance of B is created (checked it with the debugger). And there is nowhere else FRef is assigned a new value. However suddenly FRef is nil when DoSomething is called in my program. I know for sure that the instance of A FRef was pointing to still exist, because it is that instance which is calling DoSomething.

Does anyone know or have an idea of why my reference is suddenly nil?

simvector
01-07-2011, 07:55 PM
Do you have any exception handlers in your code? I had a situation like this once and long story short.. in my case... up the chain an exception was occurring and nilling my object references.

Another thing to watch for is unit init/finial handlers. They can fire in the wrong order than what you expect.

User137
01-07-2011, 10:40 PM
Could be a memory leak from anywhere. My guess is a typo somewhere or misthought logics. Only TComponent descendants free subobjects and that's only when they are destroyed themselves.

code_glitch
01-07-2011, 10:51 PM
If you are OCD about freeing memory, you can add an active flag to everything and then run a thread that registers all objects and sees if they are active. if not, they are auto-freed and deleted from the register. I tried it and it works - your only challenge is to have everything register itself and not cause a memory access violation by freeing used memory etc... That is, if you want a totally OCD approach to user137's suggestion to a problem. I did it once, and TBH didn't like it a whole lot.

pstudio
02-07-2011, 12:39 AM
Thanks for the suggestions. I've decided to put this on hold. It was meant to be used for the PGDmC but there's too many issues to bother using it now.
There's definitely some mem leaks in it somewhere and probably also stuff being freed that wasn't meant to be freed. I'll have to look the code through and do some restructuring after the compo.

simvector
02-07-2011, 02:00 AM
or... if your using later versions of Delphi set ReportMemoryLeaksOnShutdown := True; to see what's going on with memory and/or use the full version of FastMM4 and find out where it's occurring in your code. I found a link related to Themes if enabled in a DLL which I found was in the RTL itself. A workaround was to call free in the dll detach event. My point, until I started working on the compiler/IDE projects, never took advantage of these tools. Once I did I discovered that man... some leaks has been in my code base for years. Whoa.

paul_nicholls
04-07-2011, 02:10 AM
Hi again,
Seems hacking together a game engine in a week or two isn't so problem free as I could have hoped for.
So my problem is that I have some object references that are automatically nilled by Delphi.

Imagine we have class B: (Simplified code of course)


class B = class
private
FRef: A; // A is some class
public
procedure DoSomething;
end;

procedure DoSomething;
begin
FRef.SomeProcedure;
end;


So an instance of B will have a reference to an instance of A. In my code I know FRef has been set when an instance of B is created (checked it with the debugger). And there is nowhere else FRef is assigned a new value. However suddenly FRef is nil when DoSomething is called in my program. I know for sure that the instance of A FRef was pointing to still exist, because it is that instance which is calling DoSomething.

Does anyone know or have an idea of why my reference is suddenly nil?

I guess that the procedure DoSomething should be of class B?


procedure B.DoSomething;
begin
FRef.SomeProcedure;
end;

cheers,
Paul