Problem to destroy Delphi DM

1

In the system we use the Delphi DM to do SQL commands, however they are called screen-saver, and sometimes one screen uses the other (or more). But this causes memory overflow, to solve, I'm killing the DM after closing the screen, but have some method ready to check from time to time the unused open and close them?

    
asked by anonymous 24.11.2015 / 18:53

2 answers

1

It's easy to do this:

if (DM <> nil) then // Se é diferente de nulo
begin
  DM := nil;
end;

Or where I mentioned DM := nil; you can pass FreeAndNil(DM);

Examples to avoid Access Violation:

 Abriu Cadastro de Clientes --> DM Aberto.
 -- Abriu Tela que procura Estados --> Essa tela usa DM.
 -- Fechou a Tela que procura Estados --> Não pode fechar o DM
Fechou o Cadastro de Clientes --> Pode fechar o DM

In this way you control the screens that the main DM uses, however, the correct one is the main DM always stay open while the application is running, it creates secondary DMs like DMClientes , DMetcetc ... That way, when you close the Clients screen, you close DMClientes and your application will not depend on anything else in that DM!

    
24.11.2015 / 18:59
0

Based on what you describe and without the code to know what you're doing, it's a bit difficult to help you. But come on.

One approach would be to put the basics of the basics in these DMs. That is, only the DataSets you will actually use. Whatever is accessory you can create at runtime. In this case, the inheritance might help a lot, leaving the repeating part of the code in DMpai.

Another thing that can help is to use create a connection singleton or even create a pool. Depending on the bank you are using, it may be that many transactions / connections are holding too much resource without releasing. With the singleton / connection pool, you can lessen this possibility of error.

Another question is: Arrange the house before exiting DM . Close DataSet's , terminate the active transactions, close the connections (or return them to the pool). Another detail is that if you are using TFDQuery , and creating it at runtime, ensure that it will destroy it ( Try... finally ) using Release instead of Free direct. The Release method makes memory releases that .Free does not. Do not ask me why the two are public because I do not know.

Finally, if your project allows you to work with the same instance of DataModule , you only need to build a factory and a singleton class to give you back the DMs you need.

    
22.12.2017 / 04:10