Link to home
Start Free TrialLog in
Avatar of davesn
davesn

asked on

globalalloc globallock globalfree????

hi i am writing some code that makes use of these functions... now here's the screwy part... sometimes the last global free call at the end causes an error, sometimes it doesn't(see below).I'm using this code to read the record line select portion of the mixer api using two sound cards.

after the code is a copy of the details of the error, if it helps

        thanks, dave


------------------
Private Function GetListValue()

 Dim hMem As Long, ret As Long
 Dim BoolValue As MIXERCONTROLDETAILS_BOOLEAN
 
                m_currentDstControlDetails.cbStruct = Len(m_currentDstControlDetails)
                m_currentDstControlDetails.dwControlID = m_DstControl(m_currentDstControlID).dwControlID
                m_currentDstControlDetails.Item = m_DstControl(m_currentDstControlID).cMultipleItems
                hMem = GlobalAlloc(&H40, m_currentDstControlDetails.cbDetails)
                m_currentDstControlDetails.paDetails = GlobalLock(hMem)
                m_currentDstControlDetails.cbDetails = Len(m_currentDstControlDetails.paDetails)
            ret = mixerGetControlDetails(m_mxID, m_currentDstControlDetails, MIXER_GETCONTROLDETAILSF_VALUE)
            If ret <> MMSYSERR_NOERROR Then
                Err.Raise ret, _
                   "CMixer:InitializeDstControls", _
                   "Could not retrieve control info for " & GetMixerName(m_mxID) & vbCrLf & "Error:" & mmsysGetErrorString(ret)
            GlobalFree (hMem)
            Exit Function
            End If
 
            CopyStructFromPtr BoolValue, m_currentDstControlDetails.paDetails + (m_currentDstControlDetails.cbDetails * m_currentSrcLineID), Len(BoolValue)
            m_currentMxListItem = BoolValue.fValue
        Debug.Print m_currentSrcLineID, m_currentMxListItem
        GlobalFree (hMem) '<-----causing error listed below.
    GetListValue = m_currentSrcControlID
End Function
---------------
all function declares for the .bas file are from winapi.txt on vb5
---------------
'VB5 caused an invalid page fault in
'module KERNEL32.DLL at 015f:bff7a128.
'Registers:
'EAX=00000000 CS=015f EIP=bff7a128 EFLGS=00010202
'EBX=00648754 SS=0167 ESP=007fed68 EBP=007fed9c
'ECX=00000000 DS=0167 ESI=00648744 FS=3adf
'EDX=00000000 ES=0167 EDI=00000010 GS=0000
'Bytes at CS:EIP:
'89 51 08 8b 53 08 8b 43 04 8d 8b 0b 10 00 00 c1
'Stack dump:
'007fed9c 00648744 005d0000 00017570 bff7b30e 005d0000 00648744 00000010 00000200 007fee08 0000ffff 00017570 00648744 007fede4 bff7b953 005d0000
   
Avatar of pinshah
pinshah

If ret <> MMSYSERR_NOERROR Then
                Err.Raise ret, _
                   "CMixer:InitializeDstControls", _
                   "Could not retrieve control info for " & GetMixerName(m_mxID) & vbCrLf & "Error:" & mmsysGetErrorString(ret)
            GlobalFree (hMem)
            Exit Function
            End If

According to this code the globalfree(hMem) is never executed because you are raising error prior to this.


I dnt know exactly why it is happeneing
but is the error occuring when U call the function more than once ?

I mean

Call GetListValue()
...... some code
'is the error occurs here
Call GetListValue()




looks like if the ret value was received and you were unable to retrieve the control info then you cleared that memory already.  However, you still attempt to clear the memory again after the If statement. Just create another variable Freed As Boolean and set it to true and false as needed so you know when you have and havent already freed the memory specified by hMem
Azra

He has return in the if statement
Exit Function

Any way he has used Err.Raise so it will go to the ErrorHandler if it exist

youre right..i didnt see it...however i dont see anywhere where he unlocks the memory
Avatar of davesn

ASKER

yes.. all the errors are handled by a routine

specificly what happens is the record line has several different connections (ie record settings or voice command settings so this code functions like a record line selector to set the active wave in line.

what is happening is if i attempt to set the record line i get the error. if values for the voice commands are set it works fine.

incidentally, if the last globalfree call is commented, then the code seems to work...but that leaves a memory handle un-freed so to speak..
How are you able to free this memory when I dont see anywhere where you unlocked it??
Avatar of davesn

ASKER

oh yeah.. this only happens on one soundcard. the other soundcard works fine with this code.both cards seem to work fine with other programs in tandem(like cakewalk etc..)
Are to freeing the hmem pointer in your error handler  routine. if not the memory is not freed at all.

As i said you before no you have raised the error before the GlobalFree(hmem) in the if statement. Err.Raise will transfer the execution to your error handler routine and therefore the GlobalFree(hmem) is never executed in the if statement.

let me show u the flow

If ret <> MMSYSERR_NOERROR Then
 Err.Raise 'Execution is now to the error handler routine somewhere

GlobalFree ... never executed

End if

if there is a error the memory is not free and now if u try to call the function again i think you are getting the problem.

Also is there is any way to check whether the pointer hmem is a valid memory location.

Avatar of davesn

ASKER

           CopyStructFromPtr BoolValue, m_currentDstControlDetails.paDetails + (m_currentDstControlDetails.cbDetails * m_currentSrcLineID), Len(BoolValue)
            m_currentMxListItem = BoolValue.fValue
        Debug.Print m_currentSrcLineID, m_currentMxListItem
        GlobalFree (hMem) '<-----causing error listed below.
    GetListValue = m_currentSrcControlID
End Function


note the copystructfromptr function call..this uses the rtlmovememory function call...the other global free is part of the error handler

this is the code that the last globalfree 'cleans up after'
-----------------
hMem = GlobalAlloc(&H40, m_currentDstControlDetails.cbDetails)
                m_currentDstControlDetails.paDetails = GlobalLock(hMem)
-----------------

Avatar of davesn

ASKER

           CopyStructFromPtr BoolValue, m_currentDstControlDetails.paDetails + (m_currentDstControlDetails.cbDetails * m_currentSrcLineID), Len(BoolValue)
            m_currentMxListItem = BoolValue.fValue
        Debug.Print m_currentSrcLineID, m_currentMxListItem
        GlobalFree (hMem) '<-----causing error listed below.
    GetListValue = m_currentSrcControlID
End Function


note the copystructfromptr function call..this uses the rtlmovememory function call...the other global free is part of the error handler

this is the code that the last globalfree 'cleans up after'
-----------------
hMem = GlobalAlloc(&H40, m_currentDstControlDetails.cbDetails)
                m_currentDstControlDetails.paDetails = GlobalLock(hMem)
-----------------

i'm not familiar with rtlmovememory but if it is same memory that you locked then this will cause error
Avatar of davesn

ASKER

i just tried putting the first globalfree before the err.raise statement.. didn't make a difference because the error handler is written to resume at the next instruction(ie exit function)
Can you explain more clearly?
Do u have Visual Studio Sp3
Avatar of davesn

ASKER

and if there is no error the global free statement is still cleaning up after the gobalalloc/lock functions...
Do u have Visual Studio Sp3
Do u have Visual Studio Sp3
Am i completely off the mark here?
A moveable memory block cannot be moved or discarded while it is locked.
Calling GlobalFree to a locked block will not raise an error.  But it also will not free the memory.  You must call GlobalUnlock(hMem) to unlock the memory before it can be freed.
GlobalFree frees up the resources associated with a memory block, including of course the memory itself. This function should be used to deallocate any memory blocks allocated by GlobalAlloc after you are finished using it. The function returns 0 if successful, or the value passed as hMem if an error occured.

Avatar of davesn

ASKER

all of the variables are set up in other subs in the module so if a mixer has more than one selectable record line the code is reused for each line...i'm developing a mixer class. almost all of the functions are done just this last thing... the code is based on a standard getmixercontroldetails call to select across an array (because all record lines are selectable for use as a wave in source via a mixer list construct.
Avatar of davesn

ASKER

is globalunlock the win32 version of globalfree?or no?
yes it is...supported on win95,98, and NT
ASKER CERTIFIED SOLUTION
Avatar of AzraSound
AzraSound
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of davesn

ASKER

if you want to see the full code send me an email and ill send it in the reply...when this is done it's being placed for free public use...so everyone i hope can benefit...also you may not notice any problems because it only seems to happen on my mx80...my soundblasrer doesn't give an error. also this issue popped up only within the last few additions to the code.
if it is only happening to one particular soundcard then i dont see it as a coding issue..but i will gladly take a look at it
AzraSound@aol.com
Avatar of davesn

ASKER

what do you think?

keep in mind that the error im getting is in the kernel32.dll..not in a soundcard module
Avatar of davesn

ASKER

pinshah... yes it's vb5 sp 3... so it's the latest vb for its version.
Avatar of davesn

ASKER

Adjusted points from 100 to 200
Avatar of davesn

ASKER

i've raised the value of this question to 200.. again if anyone would like to see the code, mail me at davesn302@aol.com
Avatar of davesn

ASKER

as azrasound and pinshah both tried to help me, i would like to split the points between them for their efforts. the solution to the original question wasn't apparent to them as they didn't have a way to know how the code was in error because the code did not cause errors for them...apparently the problem is specific to dual soundcard setups... the problem was in the way globalalloc needs to be called in order to call the getmixercontroldetails function... the source in its entirety will be available for free download at


www.shrinkwrapvb.com

in a few weeks...it is a vb class that wraps the mixer api into a usable form..

thanks, dave
Community Support has reduced points from 200 to 100
Hi all,

In order to split the points, I have reduced the points on this question to 100. davesn, you can now accept one of the Expert's comments as an anwers using the 'Accept Comment as Answer' button in the header of one of the comments.

For the second Expert, please post a new question in this topic area. The new question title should be 'For ExpertName -- 10315962' inserting the correct Expert name - with appropriate points assigned.

For your convenience, you can use this link to create the new question:
https://www.experts-exchange.com/bin/NewQForm?ta=31

darinw
Customer Service
Avatar of davesn

ASKER

thank you azrasound for your efforts...