PlanetSquires Forums

Support Forums => Other Software and Code => Topic started by: Christian Weilguny on September 14, 2012, 07:14:51 AM

Title: HeapFree dosn't work
Post by: Christian Weilguny on September 14, 2012, 07:14:51 AM
Hi,

I have an App, designed with PB 9 and FF 3.10.

For FF I created a custom control consisting of 4 standard-controls.
One of these standard-controls is a treeview.
If I send a custom message via SendMessage to the control, this treeview will be filled with data from a database (MS-SQL).
For each item in the treeview I make a HeapAlloc with GetProcessHeap to store a string. The pointer to it will be stored as lParam with the item. This is correctly accessible, I can read the stored string without any errors.
By processing the WM_DESTROY - Message of the custom control I walk through the items of the treeview an TRY to free the memory with HeapFree. No one of these calls succeed.
The call to DestroyWindow for the Dialog containing the custom control doesn't return, the App hangs and I have to kill the App with the Systemclose-Button.
If I don't try to free the Memory with HeapFree, the Dialog closes correctly, but by closing the App I get two errors of writing to and reading from a null-pointer.
Has anyone an idea, what's the reason of this behaviour?

Christian
Title: Re: HeapFree dosn't work
Post by: Christian Weilguny on September 20, 2012, 11:19:37 AM
In the meanwhile I've a solution for the problem with HeapFree.
I use GlobalAlloc and GlobalFree instead, and there are no errors.

Only the two erros by closing the app remained.

This two errors begin with 'The instructions at 0x7.....' one time a 'written'-error and the second time a 'read'-error
I am sure, I have released alle allocated memory.
And there's another fact: if I open the same Dialog with the custom control a second time, or, if I open two Dialogs with the custom control, or one Dialog with two of this custom control, there is no error while closing the app. The error comes only by opening a Dialog with one custom control once and never opened in this program-session a second dialog with the custom control.

Perhaps someone has an idea why ... ?

Christian
Title: Re: HeapFree dosn't work
Post by: Christian Weilguny on September 20, 2012, 11:22:30 AM
In addition to my last posting:

The errors occur with Win2000 and WinXP, NOT with Win 7!

Christian
Title: Re: HeapFree dosn't work
Post by: Christian Weilguny on September 20, 2012, 12:25:07 PM
Now I have located where the error initial comes from.

From somewhere, may be from this forum, I got this routine:


Function Ptr2Obj (ByVal pObj As Dword) As IUnknown
   Local pUnk As IUnknown
   Local pv As Dword Ptr
   If pObj = %Null Then Exit Function
   pv = VarPtr(pUnk)
   @pv = pObj
   If IsObject(pUnk) Then
      pUnk.AddRef
      Function = pUnk
   End If
End Function


I give an object-pointer to the control (the data-connection) and transform this pointer with this routine to an object back.
If I comment this routine, there is no error at the end of the app.
What adaption is necessary to avoid this error? I have no idea.

Christian
Title: Re: HeapFree dosn't work
Post by: José Roca on September 20, 2012, 12:52:31 PM
You give an object-pointer to the control and then what you do with the original object reference? By simply storing a pointer in a control you don't keep the object alive.
Title: Re: HeapFree dosn't work
Post by: Christian Weilguny on September 21, 2012, 10:28:06 AM
Hi Jose,

in the meanwhile I solved this problem. As always, I created the error by myself.

I was afraid, the original object in the calling process will be destroyed, if I set the locally generated object-variable to nothing. So I didn't.
This made the error.
Now, I have in the %WM_Destroy of the Custom Control a line 'oData = Nothing' and there is no error.
Neverthless the original object in the calling process will be untouched.

We have a proverb in German, literally translated: 'fear eat soul up'. I've not tried, so I've searched.

Without solution is the question, why the HeapFree makes troubles, even if the GlobalFree works fine.
Perhaps I should say, I had the functions generating and freeing the memory in the FF-Functions Library.
May be, that caused the error.
Now I've generated an object that automatically frees the memory by destroying the object.

Christian