1

I'm trying to create a generic method to get all the references to an object from an object.

For example:

  TTest2 = class(TObject);
  TTest = class(TObject)
    Test2: TTest2;
  end;

I want to create a method like:

var
  Local: TTest;
  LinkedObjects: TList;
begin
  Local := TTest.Create;
  LinkedObjects := Local.GetChildren;
  //blah
end;

I'd like to create a method that says to me that on offset X, there is a reference for an object. The objective is to be able to list any object in any kind of field, so, published field's (that are listed on object header - vmtFieldTable) won't solve, Rtti (As it's not default for every classes) won't solve too. It's probably not possible without some help from compiler (providing some information), but if you have some idea, please let me know.

I'm researching the possibility to develop a GC for Delphi. Everything on a GC is very mature, the technology is not a problem. But how to have access for some information is what make things complicated. At this point, I'm thinking a way to deal with the Mark step.


Some thoughts

Overload the assign operator of TObject ? It's not possible just on NextGen compilers. Full answer.

Go through all the allocated memory and search for valid pointers on its space ? Slow, but is it possible ? initialize and finalize all the object's memory clear, and then go through the memory looking for pointer with a valid object header ? or can I create a parity bit on objects to make it easier to identify?


Update: I found an interesting link! Talking about the same problem we discussed here. I'll try do it.

I'm putting some information together here.

Thanks you,

Community
  • 1
  • 1
Rodrigo Farias Rezino
  • 2,687
  • 3
  • 33
  • 60
  • 2
    Easy. If you aren't prepared to use rtti then this is impossible. – David Heffernan Apr 01 '16 at 13:36
  • David, if there is some way to generate RTTI for all classes that my project is using, including third components, yes, it would be a solution. – Rodrigo Farias Rezino Apr 01 '16 at 13:50
  • As I said, if you cannot use rtti then you have to accept defeat – David Heffernan Apr 01 '16 at 14:15
  • 1
    Not _really_ impossible without RTTI. But a lot of extra work. You could set up a system whereby objects you assign/unassign are added/removed from a list of "children" (I'm not certain 'child' is the right term though). ..... However, that said; the real question is ***Why?*** I have a sneaky suspicion your underlying problem is a little different and you're likely looking for advice on how to do something that you shouldn't need to do in the first place: _if you took a fundamentally different approach to your **real** problem_. – Disillusioned Apr 01 '16 at 14:17
  • @Craig Possible if you change the rules and force all types to partake in this cooperative system you propose. As stated though, impossible. – David Heffernan Apr 01 '16 at 14:52
  • Craig, by now it's just research. But I'm looking for the possibility to create a GC for Delphi. As we (probably) know he can "use" Bohem in Delphi. I'll look what approach did it use to make the Mark on objects. Somehow it lists every link. – Rodrigo Farias Rezino Apr 01 '16 at 15:03
  • @Craig: Yes, impossible without RTTI, because the poster says in a comment *for all classes that my project is using, including third party components*. You can't force those third-party components to participate in your registration system without modifying the source. – Ken White Apr 01 '16 at 15:20
  • Hi Johan, I know about interfaces (ARC). But I'd like an option to hold all the application, not only those objects implemented as interface. – Rodrigo Farias Rezino Apr 01 '16 at 21:01
  • @Johan that's not GC, that's ARC – David Heffernan Apr 02 '16 at 16:55
  • @RodrigoFariasRezino you can learn from Apple experience. They also tried to use GC in non-managed natively compiled language (Objective C). They struggled for years, but in the end it gave them so many limitations and errors, that they abandoned GC and "innovated" by reintroducing ARC. Do you think you would be able to do better than Apple did for years ? – Arioch 'The Apr 04 '16 at 10:10
  • Arioch, can you talk more about these limitations and errors ? As far as I know, it was just a matter of performance. GC is not an experimental technology, Java have been using it since it was born, and it's very good. The bad point about GC, is when you are working with mobile. ARC gives a better response on mobile scenario because of the architecture of the mobile phones. Games, sure, handle the memory on your own, is the best bet. And, unfortunately, we don't have access to the dcc, so ARC ins't an option to develop by myself. – Rodrigo Farias Rezino Apr 04 '16 at 12:24

0 Answers0