Page 4 of 4 FirstFirst 1234
Results 31 to 39 of 39

Thread: Its killing me! Allocating memory in software!

  1. #31
    Vendor - Qube colin's Avatar
    Join Date
    Dec 2006
    Posts
    796
    Quote Originally Posted by ClockWorK View Post
    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.
    I said I can't. I don't want to start creating arrays of pointers that are referenced by arrays of messages. It shouldn't be that messy!

    Quote Originally Posted by Curiosity View Post
    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
    I won't know the type, I can't call a generic free function. The functions have to clean up after themselves.

    I think you guys are underestimating this problem. There are hundreds, if not thousands of different messages. One message can have ANY type of data attached to it. WM_FLASHREQUEST for example often contains integers, but more likely contains a string. Every here and there it will send a stringlist, and one or twice it sends an object. I got the memory allocation working with a lot of debugging.

    I figured out that I just need to send the pointer, the size, and a message. I then COPY from the start of the pointer until I reach the size. Then I can let the original calling function continue on and clean up after itself. When I send integers, it works.

    Here's the funny thing: If I send size as SizeOf(PChar), it will always be 4 (because PChar is a pointer and pointers allocate 4 bytes). If I send size as the Length of the string (5 in this case), it will cause an access violation.
    Remember: size was sent as 5 -- im sure of this because I have variable watches all over the place. If I send a static number, '5', it works just fine, copies 5 bytes, and everyone's happy!

    With a StringList, it works fine, but afterwards I get an error about privileged instructions! Meaning that I'm messing up a pointer in the stack.

  2. #32
    What can I say? I like serial. Curiosity's Avatar
    Join Date
    Mar 2004
    Location
    Florence Yall, BFKY
    Posts
    2,684
    I think we all understand what you're trying to do. You'll get it.

    Yes, a pointer is 32 (or 64) bits or 4 bytes. An integer is also 32 bits. A pointer points to an address in memory. That address has to remain allocated to access it. Don't use a pointer as an integer or an integer as a pointer and you'll have less trouble.

    Edit: Ok, SizeOf() should be used for data storage size. For strings, use StrLen() or whatever it is for Delphi or better the declared size of the string.

  3. #33
    Variable Bitrate
    Join Date
    Jul 2006
    Location
    Brisbane, Australia
    Posts
    346
    Quote Originally Posted by colin View Post
    I said I can't. I don't want to start creating arrays of pointers that are referenced by arrays of messages. It shouldn't be that messy!



    I won't know the type, I can't call a generic free function. The functions have to clean up after themselves.

    I think you guys are underestimating this problem. There are hundreds, if not thousands of different messages. One message can have ANY type of data attached to it. WM_FLASHREQUEST for example often contains integers, but more likely contains a string. Every here and there it will send a stringlist, and one or twice it sends an object. I got the memory allocation working with a lot of debugging.

    I figured out that I just need to send the pointer, the size, and a message. I then COPY from the start of the pointer until I reach the size. Then I can let the original calling function continue on and clean up after itself. When I send integers, it works.

    Here's the funny thing: If I send size as SizeOf(PChar), it will always be 4 (because PChar is a pointer and pointers allocate 4 bytes). If I send size as the Length of the string (5 in this case), it will cause an access violation.
    Remember: size was sent as 5 -- im sure of this because I have variable watches all over the place. If I send a static number, '5', it works just fine, copies 5 bytes, and everyone's happy!

    With a StringList, it works fine, but afterwards I get an error about privileged instructions! Meaning that I'm messing up a pointer in the stack.

    Sorry have not used Pascal since I decided to move to C over 20 years ago as it has much better support for pointers, so it is a lot easier to do this stuff in C.

    I'd be looking for some example code written in Delphi for message handling as the message should also return the size of the object that the pointer is pointing to. I'd also be looking for a way to either:
    1. overlay one variable over another or;
    2. how to define a pointer to a structure for each message you want to handle. (preferred)

    Assuming you go with option 2, you need to fetch the pointer and the data block size, allocate a block of memory the same size in memory you control. Lets say that WM_FLASH_REQUEST is one event you are interested in. I am Assuming there is a defined datatype for the parameter block that is returned by WM_FLASH_REQUEST in the Delphi Language.

    Then do something like:

    Define a variable called PointerToMyWM_FLASH_REQUEST with a type of WM_FLASH_REQUESTDatatype (however Delphi defines it)
    Then do something like:

    Code:
    If Message = WM_FLASH_REQUEST
      PointerToMyWM_FLASH_REQUEST = MyRetrievedPointer  // Now you can point to the block
      MyNewWM_FLASH_REQUESTVariable = NEW(WM_FLASH_REQUESTDatatype) // Now you can store it in memory you own
     MyNewWM_FLASH_REQUESTVariable  = PointerToMyWM_FLASH_REQUEST
    EndIf
    .
    I would say the reason your code fails using the length of the Pstring is because it returns an Integer so you need to convert it to another data type that is expected by the procedure you call. In C you would use a Cast to achieve this. Also, I thought Pstrings include a length parameter at the first byte in the string, so make sure you account for it when passing a string length.
    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

  4. #34
    Vendor - Qube colin's Avatar
    Join Date
    Dec 2006
    Posts
    796
    There is no defined data structure for what is returned.

    And as far as the StrLen thing, in Delphi it is Length(object).

    If I have a PChar('Hello'), SizeOf returns 4 -- that makes sense, but I can't use it. Length returns 5 -- that's right. If I call the function and allocate memory:

    Broadcast(WM_FLASHREQUEST, Chr, 5) -- works
    Broadcast(WM_FLASHREQUEST, Chr, Length(Chr)) -- doesnt work.

    Although Length(Chr) returns 5. I checked INSIDE the broadcast function, it receives the number 5 for size either way.
    One way works, one way doesnt. Hmm...

    Other than that and the StringList thing, it works.

  5. #35
    What can I say? I like serial. Curiosity's Avatar
    Join Date
    Mar 2004
    Location
    Florence Yall, BFKY
    Posts
    2,684
    You just need to create a unified data structure for all of your message types. Even if they are just one string or integer. Or each structure could have an identifier as the first member. Cast it to a generic struct and check the ID, then cast to the proper struct. Or use the wParam for info on that or a different WM_ message for each type as I said previously.


    SizeOf(PChar) will always return 4 because PChar is a pointer. Just like name doesn't mean your name or mine, or the name of the movie I'm watching.

    The length of 'Hello' is 5 but requires 6 bytes. Always use Length(Chr) + 1 for strings.

  6. #36
    Vendor - Qube colin's Avatar
    Join Date
    Dec 2006
    Posts
    796
    So define a bunch of different types of message and use them for the data? Last thing I wanted to do but I guess I'll give it a shot.

  7. #37
    Vendor - Qube colin's Avatar
    Join Date
    Dec 2006
    Posts
    796
    Wait, how would I use my single broadcast function then?

    I want to do the following:

    Broadcast ( Message: Cardinal; Data: _________; Size(optional): Integer );

  8. #38
    Variable Bitrate
    Join Date
    Nov 2007
    Posts
    262
    Quote Originally Posted by colin View Post
    I won't know the type, I can't call a generic free function. The functions have to clean up after themselves.
    You don't need to know the type, if you define a base type that has a member function that releases the memory associated with that type. In C++ these are called destructors.

    Your goal of having functions that "clean up after themselves" is not compatible with your goal of allocating memory in one place, putting it in a queue, and being able to use it later.

    I think you guys are underestimating this problem. There are hundreds, if not thousands of different messages. One message can have ANY type of data attached to it. WM_FLASHREQUEST for example often contains integers, but more likely contains a string. Every here and there it will send a stringlist, and one or twice it sends an object. I got the memory allocation working with a lot of debugging.
    It sounds like part of your problem is that you're trying to re-invent the windows messaging system. Or at least you're trying to create your own messaging system that's basically a clone of the Windows messaging system with your own veneer on top of it. My suggestion is don't do that.

    If your front end needs a messaging system to allow plugins to interact, create a messaging system that does exactly that. The number of messages will be small because you're going to create them one at a time as you discover the need for each one.

    Let the operating system handle everything that isn't unique to your application. That's what operating systems are for. In addition to saving you a lot of trouble, it makes plugin development easier because potential developers don't have to relearn your rewrite of the Windows messaging system - they can use what they already know about Windows, and just learn your messages for your events.

  9. #39
    What can I say? I like serial. Curiosity's Avatar
    Join Date
    Mar 2004
    Location
    Florence Yall, BFKY
    Posts
    2,684
    This is what came up for me on google
    http://delphi.about.com/od/objectpas.../aa070803a.htm

    You would create a record for each message. Allocate one of those and pass it as a parameter.

    Oh yeah, if you can do class objects that would be much easier. Study out how PChar and those things work. That may give you a basic understanding for Delphi.

Page 4 of 4 FirstFirst 1234

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
  •