Solved

globalalloc globallock globalfree????

Posted on 2000-03-22
33
548 Views
Last Modified: 2011-09-20
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
   
0
Comment
Question by:davesn
  • 15
  • 9
  • 7
  • +1
33 Comments
 

Expert Comment

by:pinshah
ID: 2647940
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()




0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2647944
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
0
 

Expert Comment

by:pinshah
ID: 2647961
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

0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2647976
youre right..i didnt see it...however i dont see anywhere where he unlocks the memory
0
 

Author Comment

by:davesn
ID: 2647987
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..
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2647993
How are you able to free this memory when I dont see anywhere where you unlocked it??
0
 

Author Comment

by:davesn
ID: 2647995
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..)
0
 

Expert Comment

by:pinshah
ID: 2648000
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.

0
 

Author Comment

by:davesn
ID: 2648013
           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)
-----------------

0
 

Author Comment

by:davesn
ID: 2648014
           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)
-----------------

0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2648029
i'm not familiar with rtlmovememory but if it is same memory that you locked then this will cause error
0
 

Author Comment

by:davesn
ID: 2648034
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)
0
 

Expert Comment

by:pinshah
ID: 2648036
Can you explain more clearly?
0
 

Expert Comment

by:pinshah
ID: 2648042
Do u have Visual Studio Sp3
0
 

Author Comment

by:davesn
ID: 2648046
and if there is no error the global free statement is still cleaning up after the gobalalloc/lock functions...
0
 

Expert Comment

by:pinshah
ID: 2648047
Do u have Visual Studio Sp3
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.

 

Expert Comment

by:pinshah
ID: 2648052
Do u have Visual Studio Sp3
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2648054
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.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2648059
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.

0
 

Author Comment

by:davesn
ID: 2648068
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.
0
 

Author Comment

by:davesn
ID: 2648070
is globalunlock the win32 version of globalfree?or no?
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2648079
yes it is...supported on win95,98, and NT
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 100 total points
ID: 2648082
yes it is...supported on win95,98, and NT
0
 

Author Comment

by:davesn
ID: 2648097
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.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2648104
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
0
 

Author Comment

by:davesn
ID: 2653024
what do you think?

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

Author Comment

by:davesn
ID: 2656557
pinshah... yes it's vb5 sp 3... so it's the latest vb for its version.
0
 

Author Comment

by:davesn
ID: 2662779
Adjusted points from 100 to 200
0
 

Author Comment

by:davesn
ID: 2662780
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
0
 

Author Comment

by:davesn
ID: 2697788
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
0
 
LVL 3

Expert Comment

by:darinw
ID: 2697809
Community Support has reduced points from 200 to 100
0
 
LVL 3

Expert Comment

by:darinw
ID: 2697810
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:
http://www.experts-exchange.com/bin/NewQForm?ta=31

darinw
Customer Service
0
 

Author Comment

by:davesn
ID: 2697875
thank you azrasound for your efforts...
0

Featured Post

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.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

743 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

12 Experts available now in Live!

Get 1:1 Help Now