Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 39

Thread: Its killing me! Allocating memory in software!

  1. #11
    Variable Bitrate
    Join Date
    Nov 2007
    Posts
    262
    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).
    Lots of ways to skin that cat. Declare a base TPluginMessage, for example, and pass those around. Derive types from TPluginMessage that add the extra stuff you need. I don't know if Delphi has run-time type information; if it does not, TPluginMessage needs a field that indicates which derived type it really is.

    So:

    if (currentMessage.Type == MessageType.SongMessage)
    {
    TSongMessage songMessage = (TSongMessage) currentMessage;
    Play(songMessage);
    }

    ...or whatever. I haven't done much Delphi and that little bit was several years ago, so you'll have to translate some of the syntax.

  2. #12
    Vendor - Qube colin's Avatar
    Join Date
    Dec 2006
    Posts
    796
    Quote Originally Posted by ClockWorK View Post
    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
    From MSDN:
    GMEM_FIXED
    0x0000
    Allocates fixed memory. The return value is a pointer.

    GMEM_ZEROINIT
    0x0040
    Initializes memory contents to zero.

    GPTR
    0x0040
    Combines GMEM_FIXED and GMEM_ZEROINIT.
    Quote Originally Posted by malcom2073 View Post
    Does delphi not have a Variant type?
    Yes, but you need to convert things to a variant first I think? Its hard to use, really.

    Quote Originally Posted by NSFW View Post
    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.
    Delphi has a NEW function, but the problem is you have to know what you're creating. You cant specify a size and expect Delphi to create the memory.

    Quote Originally Posted by NSFW View Post
    Lots of ways to skin that cat. Declare a base TPluginMessage, for example, and pass those around. Derive types from TPluginMessage that add the extra stuff you need. I don't know if Delphi has run-time type information; if it does not, TPluginMessage needs a field that indicates which derived type it really is.

    So:

    if (currentMessage.Type == MessageType.SongMessage)
    {
    TSongMessage songMessage = (TSongMessage) currentMessage;
    Play(songMessage);
    }

    ...or whatever. I haven't done much Delphi and that little bit was several years ago, so you'll have to translate some of the syntax.
    No way. I did try that however, I made 10 different Broadcast functions each with different parameters, it didn't work and was a bigger pain than this is.

  3. #13
    Variable Bitrate
    Join Date
    Nov 2007
    Posts
    262
    Quote Originally Posted by colin View Post
    Delphi has a NEW function, but the problem is you have to know what you're creating. You cant specify a size and expect Delphi to create the memory.
    But you do know what you're creating when you allocate the memory, right?

    No way.
    No way, what?

  4. #14
    Vendor - Qube colin's Avatar
    Join Date
    Dec 2006
    Posts
    796
    Quote Originally Posted by NSFW View Post
    But you do know what you're creating when you allocate the memory, right?
    Sort of. The program runs off of this broadcast system, so it will broadcast hundreds, if not thousands of messages. I can't say that every similar message will share a data type. Sometimes, the "INIT_DONE" broadcast will have a string attached to it, sometimes it will have an integer. I need a function that doesn't care. Just copies it blindly and sends it back blindly too.

    Quote Originally Posted by NSFW View Post
    No way, what?
    I tried that, it was a pain and it didnt work right. The problem is just that there are SO many broadcasts, I can't be doing comparisons for every message because there are too many. It will also be harder in the future because if I don't anticipate a data type, I can't pass it later on for plugins. For example, HIDDevice -- I would have completely forgotten about that. What about when I finish writing the CD player units and stuff, I'll probably have to pass over device handles and CD objects. Can't predict that.

  5. #15
    TMF
    TMF is offline
    Constant Bitrate TMF's Avatar
    Join Date
    Apr 2006
    Location
    Waterloo, IA
    Posts
    121
    Follow this link. http://delphi.about.com/od/windowssh...m_copydata.htm

    This shows how to use the special Windows Message WM_COPYDATA to pass data from one program to another.
    Action figures sold separately

  6. #16
    TMF
    TMF is offline
    Constant Bitrate TMF's Avatar
    Join Date
    Apr 2006
    Location
    Waterloo, IA
    Posts
    121
    I'm not real active on this forum, but I program Delphi full time and have been doing it since Turbo Pascal 1.0. Someday I may have to write my own front-end.
    Action figures sold separately

  7. #17
    What can I say? I like serial. Curiosity's Avatar
    Join Date
    Mar 2004
    Location
    Florence Yall, BFKY
    Posts
    2,684
    Are the plugins seperate processes or within the same process like a DLL? There's no need for WM_COPYDATA in the same process.

    SendMessage is synchronous and PostMessage is async. When using SendMessage, the message is sent and replied before SendMessage returns, so you can deallocate the memory immediately after the call. I normally use PostMessage then free the data after use so there's no waiting.

    I do a lot of serialized programming too, and use the windows messaging system to handle it but in C++ and there are so many ways to accomplish what you're trying to do. Like define each message type as WM_USER+n or have every structure type pointer in your main message structure with a 'Type' variable that defines what it is, or union or void pointer. That's kind of how variants work. You should try not to allocate a struct with another allocated struct within it though. It gets too complicated.

    So a basic idea:
    MyData = (MyDataType1) allocate(sizeof MyDataType1)
    fill it in
    PostMessage(window, WM_USER+1, 1, MyData)

    Then when you recieve the WM_USER+1, cast LPARAM as your data, use it, free it.

  8. #18
    Vendor - Qube colin's Avatar
    Join Date
    Dec 2006
    Posts
    796
    Looks like SendMessage is the way to go right now. Ill check it all out and hope for the best. Guess Ill email myself all the files and try to get it done at work today

    I wanted to avoid it and everything, but I guess I'm stuck with it.

  9. #19
    Vendor - Qube colin's Avatar
    Join Date
    Dec 2006
    Posts
    796
    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: broadcasts WM_DRIVEATTACHED
    ...
    Units 1 - 7 would now get WM_DRIVEATTACHED before they got WM_INIT or else the functions wouldnt be able to return!
    ...
    Unit4: etc...

  10. #20
    TMF
    TMF is offline
    Constant Bitrate TMF's Avatar
    Join Date
    Apr 2006
    Location
    Waterloo, IA
    Posts
    121
    First, have you tried using SendMessage?
    Action figures sold separately

Page 2 of 4 FirstFirst 1234 LastLast

Similar Threads

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