#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    12
    Rep Power
    0

    Access vilation : pointer to a record


    I have the folowing code, where lptNewMessage is a pointer to my record type (PTMessageConv).

    New(lptNewMessage);
    lptNewMessage^.sMessage := 'adasdasdas';
    lptNewMessage^.sHasNotRead := 'asdasdasds';
    Dispose(lptNewMessage);

    I get an access violation error on the 3rd line. Is it because my String exceeds the allocated memory for the pointer? I don't know how the String really works, but shouldn't it take care of it's own memory needs?

    Here's my record type :
    PTMessageConv = ^TMessageConv;
    TMessageConv = class(TObject)
    sMessage : String;
    sHasNotRead : String;
    end;

    Edit : If I swap the 2nd and 3rd line of code, it will crash on the 2nd line instead of the 3rd. And if I swap the 2 elements of my definition, it will crash on the 2nd line instead of the 3rd. So it has something to do with the last field of my record.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    England
    Posts
    952
    Rep Power
    14
    TMessageConv is a class not a record. If you want a record, replace
    Code:
    TMessageConv = class(TObject)
    with
    Code:
    TMessageConv = record
    and it should work.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    12
    Rep Power
    0
    What you said makes sense...
    But I made the change you suggested, and I have the same error.

    Edit : Ok, I was using GetMem instead of New (and Dispose). It seems it's all working fine now. Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo