Solved

StringGrid inside a DLL

Posted on 2004-03-22
11
311 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

747 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now