Board index » delphi » Problem With Allocated Memory and Using Its Pointer

Problem With Allocated Memory and Using Its Pointer

Hi all,

Ok, so I'm at wits end.  I've a List of "DataElements" holding records such
as:

  TDataElementRecord = record
    ElementType         : TDataElement;
    SourceConstant      : string;
    SourceDataField     : string;
    pAttributeRecord    : Pointer;
  end;

pAttributeRecord can point to 1 of 3 different types of records depending
on its type.  So I'm allocating memory like this:

  case ElementType of
    type1 : begin
      GetMem(DataElementRec^.pAttributeRecord, sizeof(TType1Element));
    end;

    type2  : begin
      GetMem(DataElementRec^.pAttributeRecord, sizeof(Type2Element));
    end;

    type3   : begin
      DataElementRec^.pAttributeRecord := nil;
    end;
  end;

But when I'm trying to assign values to this record I sometimes gpf.  It's
intermittent, but I've been able to watch it hit during my assignment to
the last field in my Type1 record.

  case DataElementRec^.ElementType of
      Type1 : begin
        with TAnchorElement(DataElementRec^.pAttributeRecord^) do begin
          // field assignments

        end;
      end;
   .
   .

  end;

It's certainly looks like I'm stepping on memory somewhere.  My first
thought is that my original call to GetMem is not correct.  Something about
the sizeof operation.  I don't think it's returning the actual size of my
structure, but I'm uncertain.  

There may be other ways of handling this problem and I'd be interested in
hearing them.  But for the life of me I don't know why this doesn't work
and it's driving me crazy.  I've got a number of lists to deal with and
I've stepped through them watching the pointer addresses and it all works
out beautifully.  I can even watch

  TAnchorElement(DataElementRec^.pAttributeRecord^).FieldName

in the de{*word*81} and it works out great.  But as I approach the end of that
structure, it will gpf.  I must not have enough memory allocated all the
time!

Can anyone shed any light?  Have any ideas?

Many thanks,

-- Jim Burns
   Consultant      Technology Dynamics
                     Pearland, Texas
                       (713) 485-0410
                         jimbu...@technologydynamics.com

 

Re:Problem With Allocated Memory and Using Its Pointer


Quote
Jim Burns wrote:

>   TDataElementRecord = record
>     ElementType         : TDataElement;
>     SourceConstant      : string;
>     SourceDataField     : string;
>     pAttributeRecord    : Pointer;
>   end;

> pAttributeRecord can point to 1 of 3 different types of records depending
> on its type.  So I'm allocating memory like this:[with GetMem]

I don't know what TType1Element and Type2Element are, but if they are
objects, you obviously should call their constuctors. If they're records,
and you're using Delphi 2, you should look into the Initialize and Finalize
functions. They _might_ help you.

BTW. It seems your doing fairly object oriented work, but without all the
support Object Pascal gives you for it. Any specific reason why you're using
the TDataElement type at all? Wouldn't separate classes desceding from an
(abstract?) class TDataElementClass be easier/better?

M.

--
Ettertraktet kaffe, er det ekstra god kaffe?

mailto:martin.lars...@delfi-data.msmail.telemax.no
http://www.delfidata.no/users/~martin

Other Threads