StringGrid inside a DLL


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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
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, it give an AV as the assigned is owned by the form....
wqclatreAuthor Commented:
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
wqclatreAuthor Commented:
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)

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

wqclatreAuthor Commented:
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;
  if not Initialized then begin

function Deinitialize : Boolean;
  if Initialized then begin
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
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 then freeing the form (that try to free the owned stringgrid too)  it raise the AV as the grid was already destroyed....
wqclatreAuthor Commented:
Should that be the problem when I Set the DLLApplication to the Main Program's Application?
wqclatreAuthor Commented:
And if so. Why does it work if I just switch to a memo instead?
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
THe memo is also created on the fly or in design time?
wqclatreAuthor Commented:

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
      memo:= TMemo.Create(ParentComponent);
      memo.Parent := ParentComponent;
      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
      combo:= TCombobox.Create(ParentComponent);
      combo.Parent := ParentComponent;
      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
      gb:= TGroupBox.Create(ParentComponent);
      gb.Parent := ParentComponent;
      CompList.AddObject(gb, XMLChildNode);
      if XMLChildNode.HasChildNodes then
        ParseXMLChildNodes(XMLChildNode, gb);
    end else
    if XMLChildNode.Attributes['type'] = 'TStringGrid' then
      sg:= TStringGrid.Create(ParentComponent);
      sg.Parent := ParentComponent;
      sg.OnSelectCell:= OnGlobalSelectCell;
      CompList.AddObject(sg, XMLChildNode);
      if XMLChildNode.HasChildNodes then
        ParseXMLChildNodes(XMLChildNode, sg);

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
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
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....

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.