Solved

StringGrid inside a DLL

Posted on 2004-03-22
11
320 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

777 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