Solved

StringGrid inside a DLL

Posted on 2004-03-22
11
323 Views
Last Modified: 2010-04-05
Hello.

I have one problem.

I try to use a stringgrid from inside a dll.

What I do is that I create it dynamicly in the main program.
Then in the dll I add some values into it.

But When I free the dll I get an Access Violation.

When I load the DLL I set the dll's application to the Programs Application. And I change it back before I unload it.

What can be the problem?
0
Comment
Question by:wqclatre
  • 6
  • 4
11 Comments
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 10655689
maybe a reference to the StringList that is owned by the mainprogram...if you use a TStrings.assign(stringlistfromtheform) in the dll and then try to free it on dll.free, it give an AV as the assigned is owned by the form....
0
 
LVL 2

Author Comment

by:wqclatre
ID: 10655782
String List? Where in the string grid do I have this String List? Can you give me an example?

The StringGrid is owned by the Main program and is created on a form in the main program.
I just add values into StringGrid.Cells[ix,jx]:= 'SomeText';
from inside the dll.

When I do the same with a memmo instead I have no problem
0
 
LVL 2

Author Comment

by:wqclatre
ID: 10655793
SO what I do in the main program is something like this

frmTest := TFrmTest.Create(Application);
Sg:= TStringGrid.Create(frmTest);
SG.Parent:= frmTest;

Here I Load the dll and then add some values in the StringGrid.
Then I Unload the dll and free the frmTest. (And get the AccessViolation when I try to free the frmTest)


0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 2

Author Comment

by:wqclatre
ID: 10655804
and about the dll:

First I do an Initialize. then I Work with the stringgrid and then I  deinitialize and unload it.
function Initialize(MyApp : TApplication) : Boolean;
begin
  if not Initialized then begin
    DLLApplication:=Application;
  end;
  Application:=QuoteApp;
  Initialized:=True;
  Result:=True;
end;

function Deinitialize : Boolean;
begin
  if Initialized then begin
    Application:=DLLApplication;
  end;
  Initialized:=False;
  Result:=True;
end;
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 10655867
OK Sorry for the typo...btw i was meaining this: how do you get access to the STringGrid? if you get a reference to it from the dll then freeing the dll frees also the referenced grid....so then freeing the form (that try to free the owned stringgrid too)  it raise the AV as the grid was already destroyed....
0
 
LVL 2

Author Comment

by:wqclatre
ID: 10655882
Should that be the problem when I Set the DLLApplication to the Main Program's Application?
0
 
LVL 2

Author Comment

by:wqclatre
ID: 10655887
And if so. Why does it work if I just switch to a memo instead?
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 10655912
THe memo is also created on the fly or in design time?
0
 
LVL 2

Author Comment

by:wqclatre
ID: 10655957
Yes.

What I do is that I create the forms from an XML file.

So the code looks something like:

    if XMLChildNode.Attributes['type'] = 'TMemo' then
    begin
      memo:= TMemo.Create(ParentComponent);
      memo.Parent := ParentComponent;
      LoadXmlMemo(XMLChildNode,memo);
      memo.OnChange:= OnGlobalChange;
      memo.OnClick:= OnGlobalClick;
      memo.OnDblClick:= OnGlobalDblClick;
      memo.OnEnter:= OnGlobalEnter;
      memo.OnExit:= OnGlobalExit;
      memo.OnKeyDown:= OnGlobalKeyDown;
      memo.OnKeyPress:= OnGlobalKeyPress;
      memo.OnKeyUp:= OnGlobalKeyUp;
      CompList.AddObject(memo, XMLChildNode);
    end else
    if XMLChildNode.Attributes['type'] = 'TComboBox' then
    begin
      combo:= TCombobox.Create(ParentComponent);
      combo.Parent := ParentComponent;
      LoadXmlCombo(XMLChildNode,combo);
      combo.OnChange:= OnGlobalChange;
      combo.OnClick:= OnGlobalClick;
      combo.OnDblClick:= OnGlobalDblClick;
      combo.OnDropDown:= OnGlobalDropDown;
      combo.OnEnter:= OnGlobalEnter;
      combo.OnExit:= OnGlobalExit;
      combo.OnKeyDown:= OnGlobalKeyDown;
      combo.OnKeyPress:= OnGlobalKeyPress;
      combo.OnKeyUp:= OnGlobalKeyUp;
      CompList.AddObject(combo, XMLChildNode);
    end else
    if XMLChildNode.Attributes['type'] = 'TGroupBox' then
    begin
      gb:= TGroupBox.Create(ParentComponent);
      gb.Parent := ParentComponent;
      LoadXmlGB(XMLChildNode,gb);
      CompList.AddObject(gb, XMLChildNode);
      if XMLChildNode.HasChildNodes then
        ParseXMLChildNodes(XMLChildNode, gb);
    end else
    if XMLChildNode.Attributes['type'] = 'TStringGrid' then
    begin
      sg:= TStringGrid.Create(ParentComponent);
      sg.Parent := ParentComponent;
      sg.OnSelectCell:= OnGlobalSelectCell;
      LoadXmlSG(XMLChildNode,sg);
      CompList.AddObject(sg, XMLChildNode);
      if XMLChildNode.HasChildNodes then
        ParseXMLChildNodes(XMLChildNode, sg);
    end;


The strange thing is that If i free the StringGrid inside the dll I have no problem.
(But it's created in the MainProgram and I just fill in values in the dll. then try to free it in the main program again) I do the same with the other components abowe but only string grid gives me the problem
0
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 125 total points
ID: 10656045
That depends on some TSTringGrid pointers that are assigned to objects cells and allocated during the creation...
In the STringGrid destroy it frees the referenced object, but these pointers don't become nil anymore...
So destroying the STrigGrid from the Dll doesn't deallocate the pointers that are still resolved by the app, but the referenced object were destroyed so trying to destroy them raises the AV....
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question