Page 1 of 4 1234 LastLast
Results 1 to 10 of 39

Thread: Its killing me! Allocating memory in software!

  1. #1
    Vendor - Qube colin's Avatar
    Join Date
    Dec 2006
    Posts
    796

    Its killing me! Allocating memory in software!

    I cant continue writing my frontend until I get this fixed! It runs off of a message system basically. Each file has a receiving function that gets messages and responds to them, this way it's modular and sort of plugin based. The problem is, I cant pass data between them!

    Anybody who knows software, here's where it gets DEEP.

    When I want to send a "broadcast" -- a message -- I call the function Broadcast with a Message(string) and some Data(pointer). It gets added to a queue and the pointer gets destroyed.

    Thats all fine, except by the time the message gets to the front of the queue, the pointer is destroyed! So, I want to COPY all the data (regardless of what type) into memory somewhere and then use that pointer instead.

    How the hell do I do that?! I tried using LocalAlloc, I tried using weird typecasting, I tried so many things! The function has to work for strings, integers, lists, and any generic objects (HID_Device objects too).

    Im developing in Delphi but that shouldn't matter.

  2. #2
    Variable Bitrate
    Join Date
    Nov 2007
    Posts
    262
    I picture it working like this:

    * allocate the data structure
    * put it into the queue
    * pull structures off the queue
    * pass structure around to each plugin
    * when all plugins have processed the structure, release the memory

    What is it about your code that's different from what I just described?

  3. #3
    Fusion Brain Creator 2k1Toaster's Avatar
    Join Date
    Mar 2006
    Location
    Colorado, but Canadian!
    Posts
    10,045
    I have never been able to get global (system wide) memory allocation to work properly. Is there a reason you arent using COM or something?
    Fusion Brain Version 6 Released!
    1.9in x 2.9in -- 47mm x 73mm
    30 Digital Outputs -- Directly drive a relay
    15 Analogue Inputs -- Read sensors like temperature, light, distance, acceleration, and more
    Buy now in the MP3Car.com Store

  4. #4
    Mod - Driveline forum
    Auto Apps:loading...
    ClockWorK's Avatar
    Join Date
    Oct 2003
    Location
    Michigan
    Posts
    718
    I could be way off here. If so, sorry.

    GlobalAlloc only returns a handle to a memory location. To get a true pointer, you need to call GlobalLock after GlobalAlloc.

    Also, that pointer is only useful within your process. Other processes cannot directly access that memory location in your process.


    Edit: Also, for the frontend I'm working on (DriveLine), my plan was that the plugins would communicate only with the frontend, via SendMessage. Plugins can set or retrieve certain values based on the params. So one plugin can send a command to the frontend telling it the current speed. Then, another plugin can query the frontend for the speed.

  5. #5
    Variable Bitrate
    Join Date
    Nov 2007
    Posts
    262
    ah... if you're dealing with multiple processes it gets a bit more complicated than what I described. In that case, used named pipes or something.

  6. #6
    Vendor - Qube colin's Avatar
    Join Date
    Dec 2006
    Posts
    796
    Quote Originally Posted by NSFW View Post
    I picture it working like this:

    * allocate the data structure
    * put it into the queue
    * pull structures off the queue
    * pass structure around to each plugin
    * when all plugins have processed the structure, release the memory

    What is it about your code that's different from what I just described?
    Nothing. The problem lies in putting it into the queue. If a function allocates memory, as soon as that function completes, the memory is gone. You can't keep it globally because what if that function gets called 100 times and each time puts something into a queue? you need different structures each time, and you need to keep them!

    Quote Originally Posted by 2k1Toaster View Post
    I have never been able to get global (system wide) memory allocation to work properly. Is there a reason you arent using COM or something?
    COM has unnecessary overhead for my application. Using it simply to pass messages between forms in one application is silly.

    Quote Originally Posted by ClockWorK View Post
    I could be way off here. If so, sorry.

    GlobalAlloc only returns a handle to a memory location. To get a true pointer, you need to call GlobalLock after GlobalAlloc.

    Also, that pointer is only useful within your process. Other processes cannot directly access that memory location in your process.


    Edit: Also, for the frontend I'm working on (DriveLine), my plan was that the plugins would communicate only with the frontend, via SendMessage. Plugins can set or retrieve certain values based on the params. So one plugin can send a command to the frontend telling it the current speed. Then, another plugin can query the frontend for the speed.
    GlobalAlloc, LocalAlloc and HeapAlloc do return handles, however if you use LPTR or LMEM_FIXED in your flags, you automatically get a pointer because the memory is not movable. I only want it within one process.

    Your sendmessage idea is good, except what about when the information you're sending isn't a string or integer or basic type? I need to be able to call ONE function (sendmessage for example) with any type of data. It has to be able to process Integers, Strings, Chars, Pointers, StringLists (delphi thing), HID_Devices, TSongs (custom classes).

  7. #7
    Mod - Driveline forum
    Auto Apps:loading...
    ClockWorK's Avatar
    Join Date
    Oct 2003
    Location
    Michigan
    Posts
    718
    If you don't call GlobalLock, you will never have a true pointer. Seriously. But maybe you never even need a real pointer. If you call these at the load of your app, and store them in global variables, I don't see why it wouldn't work.


    AppStart:
    Allocate Memory and store handle into global variable.
    Lock Memory and store pointer into global variable.

    AppEvents:
    Use the global variables to access the allocated memory.

    AppClose:
    Unlock and free the allocated memory

  8. #8
    North of the land of Hey Huns
    Auto Apps:loading...

    Join Date
    Jun 2004
    Location
    Westminster, MD
    Posts
    2,127
    Quote Originally Posted by colin View Post
    I need to be able to call ONE function (sendmessage for example) with any type of data. It has to be able to process Integers, Strings, Chars, Pointers, StringLists (delphi thing), HID_Devices, TSongs (custom classes).

    Does delphi not have a Variant type?
    "stop with the REINSTALLS, what do you think we got some lame-o installer!!!" - mitchjs
    RevFE
    My Shop

  9. #9
    What can I say? I like serial. Curiosity's Avatar
    Join Date
    Mar 2004
    Location
    Florence Yall, BFKY
    Posts
    2,684
    GlobalAlloc and LocalAlloc are the same in current operating systems, so use LocalAlloc just because it's easier. But yeah, your problem is local vs. global variables. locals are allocated on the stack unless declared static. globals are outside the scope of the function. That's pretty much universal for any language.

  10. #10
    Variable Bitrate
    Join Date
    Nov 2007
    Posts
    262
    Quote Originally Posted by colin View Post
    Nothing. The problem lies in putting it into the queue. If a function allocates memory, as soon as that function completes, the memory is gone. You can't keep it globally because what if that function gets called 100 times and each time puts something into a queue? you need different structures each time, and you need to keep them!
    So that is what's different from what I described.

    You need a way to allocate memory and control when it's released. In C++ you'd use "new" and I'm guessing Delphi has something similar. If you use Local/GlobalAlloc, put the handle into your queued structure, so that the code that pulls structures from the queue can also pass the handle to Local/GlobalFree (I think that's what it's called...) after passing the structure to all of the plugins.

Page 1 of 4 1234 LastLast

Similar Threads

  1. Really bad software
    By jimisbell in forum GPS
    Replies: 0
    Last Post: 05-02-2009, 10:39 PM
  2. Open Source offroad navigation software
    By JuniorGeezer in forum GPS
    Replies: 0
    Last Post: 02-22-2009, 11:22 AM
  3. LOG: Software installation and setup
    By d_sellers1 in forum Software & Software Development
    Replies: 9
    Last Post: 06-08-2006, 09:11 PM
  4. Suggestion for the Software forum
    By Confused in forum Mp3car Forum Suggestions/Comments
    Replies: 11
    Last Post: 11-02-2004, 07:55 AM

Bookmarks

Posting Permissions

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