Results 1 to 5 of 5

Thread: GlobalAllock, GlobalLock...What's the Deal ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    64

    GlobalAllock, GlobalLock...What's the Deal ?

    Hi all,

    Can anybody tell me a scenario where one *needs* to Allocate and lock a memory block/area before working with that memory block ? I often see code that allocates/block memory via the GlobalAllock & GlobalLock APIs before assigning data to variables. I am confused and would appreciate if anybody could shed some light on this.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: GlobalAllock, GlobalLock...What's the Deal ?

    Welcome to the forums.

    You should specify which language you are using.

    This isn't a perfect answer, but researching functions you are using (i.e., APIs) helps. Many times it tells you whether memory should be allocated with GlobalAlloc, LocalAlloc or VirtualAlloc for example. GlobalLock is easy enough: If you don't know if the memory was allocated as moveable or not, then use it when in doubt. Otherwise if it was not created as moveable, you shouldn't need to lock the memory pointer.

    Edited: Here are some things to read over
    Comparing Memory Allocation Methods
    Global and Local Functions
    Windows Memory Management
    Windows’s Virtual Address Space: A Short MSDN Story
    Last edited by LaVolpe; Dec 5th, 2011 at 12:21 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    64

    Re: GlobalAllock, GlobalLock...What's the Deal ?

    Hey thanks for the answer.

    I am using VB6/VBA.

    My point is why is there a need to allocate memory in the first place ? And in which scenarios it becomes necessary to allocate memory before storing data in it ?

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: GlobalAllock, GlobalLock...What's the Deal ?

    There is no single answer. If you are not using any APIs or other DLLs that require memory allocation, then the answer is no. VB does all this behind the scenes for you.

    Now, if you are using APIs or DLLs, then as mentioned previously, you need to research the API by going to MSDN.com and looking it up. If it specifically says to allocate memory using a specific method, then you should probably do that. GlobalAlloc & LocalAlloc for example use the same memory space but are wrappers to the the HeapAlloc function. Since they are wrappers, they have different ways of accessing/referencing the memory. If an API tells you to use one or the other, it is most likely because that API was coded to use that specific wrapper
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    64

    Re: GlobalAllock, GlobalLock...What's the Deal ?

    Thanks for the clarification.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width