Page 3 of 4 FirstFirst 1234 LastLast
Results 21 to 30 of 39

Thread: Its killing me! Allocating memory in software!

  1. #21
    Vendor - Qube colin's Avatar
    Join Date
    Dec 2006
    Posts
    798
    Im working on it right now. I don't want to change hundreds of lines of code to use it though if it wont work!

  2. #22
    Variable Bitrate
    Join Date
    Nov 2007
    Posts
    303
    Quote Originally Posted by colin View Post
    Okay, I'm having trouble with this again!

    How would it work? It won't follow a QUEUE system at all if I use SendMessage!

    Lets pretend the message WM_INIT gets sent to 7 units.

    Unit1: does its thing
    Unit2: does its thing
    Unit3: puts WM_DRIVEATTACHED in your application's queue
    ...
    Unit4: etc...
    Your application pulls WM_DRIVEATTACHED from its queue and broadcasts it
    Units 1 - 7 would now get WM_DRIVEATTACHED after they got WM_INIT and all is right with the world.
    Correction above. ...and then your application pulls messages from its queue and broadcasts them via SendMessage.

    Although it's not at all clear to me that SendMessage/PostMessage is the right tool for the job. Those are for operating on the operating system's event queue. If you're dealing with GUI event like button presses and such, you don't need to do anything special. If you're dealing with events that are specific to your application (and it sounds like you are) then it makes more sense to decouple that from the operating system's event queue.

    Assuming I understand your goals (which I admit IS a bit of an assumption), then what you want is probably something like what's commonly called the "observer" pattern. Dig into this and see if it looks useful:

    http://www.google.com/search?q=observer+pattern+delphi

  3. #23
    Vendor - Qube colin's Avatar
    Join Date
    Dec 2006
    Posts
    798
    Doesn't work like that though:

    * WM_INIT is added to the queue.
    Unit1: does its thing
    Unit2: does its thing
    Unit3: puts WM_DRIVEATTACHED in your application's queue
    Once WM_DRIVEATTACHED is in the queue, the function returns so that WM_INIT can continue down the line! The pointer to the data for WM_DRIVEATTACHED is now gone.
    Unit4: does its thing with the init
    Unit5: does its thing with the init
    etc...
    WM_INIT is removed from the queue. It's pointer data is freed.
    WM_DRIVEATTCHED is the next message in the queue.
    Unit1: tries to do it's thing. Unfortunately, there is no data any more

  4. #24
    Vendor - Qube colin's Avatar
    Join Date
    Dec 2006
    Posts
    798
    A bit more insight:

    * Create an object for INIT
    * Unit1
    * Unit2
    * Unit3
    *** Responds to INIT by finding attached drives
    *** Finds drive C -- creates a drive object to be passed to other units later
    *** Finds drive D -- creates a drive object to be passed to other units later
    *** Finds a bunch of other drives...
    *** Adds them all to the queue, pointers to the data and all.
    *** Returns from it's processing function -- freeing the pointers
    * Unit4
    * Unit5

    INIT message has made it through the line. It's processing is done -- freeing it's pointer.

    Now the next message comes up with drive C as the object. However, the pointer is now invalid because the pointer was freed.

  5. #25
    Variable Bitrate
    Join Date
    Jul 2006
    Location
    Brisbane, Australia
    Posts
    346
    Colin,

    I'm a bit out of touch with this stuff and current programming environments. To me, it seems the problem is that you are working with local variables that are destroyed as you leave the procedure.

    The static modifier on a variable declaration tells the compiler to make a variable stay around until next time a procedure is called instead of releasing it on exit.

    I would approach this by keeping a linked list in global memory of the queue data.

    If you google linked list, you should find some example code. Each entry in the list will be a struct and contain a pointer to the previous and the next entry. The first entry has previous = NULL and the last entry has next = NULL so you can test for the first and last entries. I would maintian global pointers to firstrecord and lastrecord. An empty list would have NULL values for both of these. You probably need a initialize_list() and empty_list() functions called at the beginning and end of your program.


    Now I might be way off here about your probelm, but hopefully, I have given some ideas to think about.

    EDIT: You would also have procedures/methods to AddEntry(), ProcessEntry() and DeleteEntry(). Ideally all wrapped up in a classs so it is robust and contained. So when you add to the queue you call AddEntry() and when you have processed it with ProcessEntry(), call DeleteEntry()
    RodW
    2007 Toyota Hilux with a CarPC..

    Worklog: http://www.mp3car.com/vbulletin/work...ota-hilux.html
    OziExplorer GPS Embedded in RR: http://www.mp3car.com/vbulletin/sb-s...iexplorer.html

  6. #26
    Vendor - Qube colin's Avatar
    Join Date
    Dec 2006
    Posts
    798
    Quote Originally Posted by rodweb View Post
    Colin,

    I'm a bit out of touch with this stuff and current programming environments. To me, it seems the problem is that you are working with local variables that are destroyed as you leave the procedure.

    The static modifier on a variable declaration tells the compiler to make a variable stay around until next time a procedure is called instead of releasing it on exit.

    I would approach this by keeping a linked list in global memory of the queue data.

    If you google linked list, you should find some example code. Each entry in the list will be a struct and contain a pointer to the previous and the next entry. The first entry has previous = NULL and the last entry has next = NULL so you can test for the first and last entries. I would maintian global pointers to firstrecord and lastrecord. An empty list would have NULL values for both of these. You probably need a initialize_list() and empty_list() functions called at the beginning and end of your program.


    Now I might be way off here about your probelm, but hopefully, I have given some ideas to think about.

    EDIT: You would also have procedures/methods to AddEntry(), ProcessEntry() and DeleteEntry(). Ideally all wrapped up in a classs so it is robust and contained. So when you add to the queue you call AddEntry() and when you have processed it with ProcessEntry(), call DeleteEntry()
    Ill look around at the static modifier, thanks.

    I did take a programming class this year at school, and we did a lot of very complex work with structures such as linked lists. I know what you mean, but it doesn't help my case in any way. I can store pointers in a queue no problem, I can't keep the data.

  7. #27
    Variable Bitrate
    Join Date
    Nov 2007
    Posts
    303
    How are you allocating the data for the entries in the linked list?
    How are you allocating the data for the (for example) "drive" objects?

    It sounds like you're using local variables, as rodweb said. You need to explicitly allocate the memory you want to keep, using 'new' or something equivalent.

  8. #28
    Vendor - Qube colin's Avatar
    Join Date
    Dec 2006
    Posts
    798
    I was using local variables.

    My code would be something like this (for example):

    Code:
    Function
    - Create object
    - Add it to queue
    - Free Object
    
    Queue
    - TRY TO copy memory **
    - Dispatch the message
    - Check if queue is empty
    My individual functions have to be able to free the memory they use. Im trying to copy it right now using Move, but that's not working either. How the hell can I queue memory!!!

  9. #29
    Mod - Driveline forum
    Auto Apps:loading...
    ClockWorK's Avatar
    Join Date
    Oct 2003
    Location
    Michigan
    Posts
    703
    From the beginning, it has sounded like GLOBAL variables and the current method (memory allocation) should work. There's no reason for pointers to be destroyed if they are stored in global variables in some module (in my opinion, a module works better than a class for these kind of things).

    If I'm not mistaken, most 'best practices' documents say to not use 'STATIC'. Go Global.

  10. #30
    What can I say? I like serial. Curiosity's Avatar
    Join Date
    Mar 2004
    Location
    Florence Yall, BFKY
    Posts
    2,684
    Quote Originally Posted by colin View Post
    I was using local variables.

    My code would be something like this (for example):

    Code:
    Function
    - Create object
    - Add it to queue
    - Free Object
    
    Queue
    - TRY TO copy memory **
    - Dispatch the message
    - Check if queue is empty
    My individual functions have to be able to free the memory they use. Im trying to copy it right now using Move, but that's not working either. How the hell can I queue memory!!!
    Well, there's your problem.
    Here:
    Function
    - Create object
    - Add it to queue

    Queue Processor
    - Check Queue
    - Remove Object From Queue
    - Dispatch Object
    - Free Object

Page 3 of 4 FirstFirst 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
  •