Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-02

dynamic array and new/dispose

19 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Amit Bueno

I am writing an application on iMac, and having problem dynamically creating objects and reading them into an array:

// I got the following object:
     PChunk = ^Chunk;
     Chunk = Object
              SubChunks: array of PChunk;
              Data: Pointer; .
              Function NewSubchunk: integer;
              Procedure Read(var Fl: TStream);
              Constructor Create;
              Destructor Destroy;
             end;
// creating and reading more data
SubChunks[NewSubChunk]^.read(Fl);
// NewSubChunk function
Function Chunk.NewSubchunk: integer;
begin
  setlength(SubChunks, High(SubChunks)+2);
  New(subchunks[High(SubChunks)], create);
  SubChunks[High(SubChunks)]^.Data := Self.Data;
  Result := High(SubChunks);
end;
// ---------

The procedure fails when running the:

SubChunks[NewSubChunk]^.read(Fl);

All the procedure works fine on Delphi, but on fpc under iMac it fails.

Why?

Regards,

Amit

ik
to Amit Bueno
Amit Bueno wrote 63 lines
I am writing an application on iMac, and having problem dynamically creating objects and reading them into an array: // I got the following object: PChunk = ^Chunk; Chunk = Object SubChunks: array of PChunk; Data: Pointer; . Function NewSubchunk: integer; Procedure Read(var Fl: TStream); Constructor Create; Destructor Destroy; end; // creating and reading more data SubChunks[NewSubChunk]^.read(Fl); // NewSubChunk function Function Chunk.NewSubchunk: integer; begin setlength(SubChunks, High(SubChunks)+2); New(subchunks[High(SubChunks)], create); SubChunks[High(SubChunks)]^.Data := Self.Data; Result := High(SubChunks); end; // --------- The procedure fails when running the: SubChunks[NewSubChunk]^.read(Fl); All the procedure works fine on Delphi, but on fpc under iMac it fails. Why?

What is the error message you are having ?

Show quoted text 14 lines
Regards, *Amit* _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Ido
LINESIP - Opening the source for communication
http://www.linesip.com
http://www.linesip.co.il

Amit Bueno
to ik

The general error it raises is: "Access Violation"

Earlier message 92 lines
From: On Behalf Of ik Sent: Wednesday, February 02, 2011 10:31 AM To: FPC-Pascal users discussions Cc: Subject: Re: [fpc-pascal] dynamic array and new/dispose On Wed, Feb 2, 2011 at 10:20, Amit Bueno wrote: I am writing an application on iMac, and having problem dynamically creating objects and reading them into an array: // I got the following object: PChunk = ^Chunk; Chunk = Object SubChunks: array of PChunk; Data: Pointer; . Function NewSubchunk: integer; Procedure Read(var Fl: TStream); Constructor Create; Destructor Destroy; end; // creating and reading more data SubChunks[NewSubChunk]^.read(Fl); // NewSubChunk function Function Chunk.NewSubchunk: integer; begin setlength(SubChunks, High(SubChunks)+2); New(subchunks[High(SubChunks)], create); SubChunks[High(SubChunks)]^.Data := Self.Data; Result := High(SubChunks); end; // --------- The procedure fails when running the: SubChunks[NewSubChunk]^.read(Fl); All the procedure works fine on Delphi, but on fpc under iMac it fails. Why? What is the error message you are having ? Regards, Amit
Jonas Maebe
to Amit Bueno: I am writing an application on iMac, and having problem dynamically creating objects and…
Amit Bueno wrote 20 lines
// creating and reading more data SubChunks[NewSubChunk]^.read(Fl); // NewSubChunk function Function Chunk.NewSubchunk: integer; begin setlength(SubChunks, High(SubChunks)+2); New(subchunks[High(SubChunks)], create); SubChunks[High(SubChunks)]^.Data := Self.Data; Result := High(SubChunks); end; // --------- The procedure fails when running the: SubChunks[NewSubChunk]^.read(Fl); All the procedure works fine on Delphi, but on fpc under iMac it fails. Why?

Are you compiling in Delphi mode? Please *always* post compilable source code that demonstrates the problem when asking for help, not uncompilable snippets. Don't make people guess about what you might have written or about how you are compiling it.

Jonas

Amit Bueno
to Jonas Maebe

I am compiling in a delphi mode.
Sorry for not posting a runnable source code,.
The code portion was taken from a huge source code, which is probably hard to detach from the rest of the code.
It is reading a TStream object, which is an input 3ds file given to the app.

---
Amit

Earlier message 38 lines
-----Original Message----- From: On Behalf Of Jonas Maebe Sent: Wednesday, February 02, 2011 11:17 AM To: FPC-Pascal users discussions Subject: Re: [fpc-pascal] dynamic array and new/dispose On 02 Feb 2011, at 09:20, Amit Bueno wrote: > // creating and reading more data > SubChunks[NewSubChunk]^.read(Fl); > > // NewSubChunk function > Function Chunk.NewSubchunk: integer; > begin > setlength(SubChunks, High(SubChunks)+2); > New(subchunks[High(SubChunks)], create); > SubChunks[High(SubChunks)]^.Data := Self.Data; > Result := High(SubChunks); > end; > > // --------- > The procedure fails when running the: > SubChunks[NewSubChunk]^.read(Fl); > > All the procedure works fine on Delphi, but on fpc under iMac it > fails. > Why? > Are you compiling in Delphi mode? Please *always* post compilable source code that demonstrates the problem when asking for help, not uncompilable snippets. Don't make people guess about what you might have written or about how you are compiling it. Jonas
ik
to Amit Bueno
Amit Bueno wrote 7 lines
I am compiling in a delphi mode. Sorry for not posting a runnable source code,. The code portion was taken from a huge source code, which is probably hard to detach from the rest of the code. It is reading a TStream object, which is an input 3ds file given to the app.

Can you write a small proof of concept that does exactly the same thing with the problem ?

Show quoted text 50 lines
--- Amit -----Original Message----- From: On Behalf Of Jonas Maebe Sent: Wednesday, February 02, 2011 11:17 AM To: FPC-Pascal users discussions Subject: Re: [fpc-pascal] dynamic array and new/dispose On 02 Feb 2011, at 09:20, Amit Bueno wrote: > // creating and reading more data > SubChunks[NewSubChunk]^.read(Fl); > > // NewSubChunk function > Function Chunk.NewSubchunk: integer; > begin > setlength(SubChunks, High(SubChunks)+2); > New(subchunks[High(SubChunks)], create); > SubChunks[High(SubChunks)]^.Data := Self.Data; > Result := High(SubChunks); > end; > > // --------- > The procedure fails when running the: > SubChunks[NewSubChunk]^.read(Fl); > > All the procedure works fine on Delphi, but on fpc under iMac it > fails. > Why? > Are you compiling in Delphi mode? Please *always* post compilable source code that demonstrates the problem when asking for help, not uncompilable snippets. Don't make people guess about what you might have written or about how you are compiling it. Jonas _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Ido
LINESIP - Opening the source for communication
http://www.linesip.com
http://www.linesip.co.il

Amit Bueno
to ik

Have you got the prof.rar?

Amit

Earlier message 68 lines
From: On Behalf Of ik Sent: Wednesday, February 02, 2011 11:29 AM To: FPC-Pascal users discussions Subject: Re: [fpc-pascal] dynamic array and new/dispose On Wed, Feb 2, 2011 at 11:18, Amit Bueno wrote: I am compiling in a delphi mode. Sorry for not posting a runnable source code,. The code portion was taken from a huge source code, which is probably hard to detach from the rest of the code. It is reading a TStream object, which is an input 3ds file given to the app. Can you write a small proof of concept that does exactly the same thing with the problem ? --- Amit -----Original Message----- From: On Behalf Of Jonas Maebe Sent: Wednesday, February 02, 2011 11:17 AM To: FPC-Pascal users discussions Subject: Re: [fpc-pascal] dynamic array and new/dispose On 02 Feb 2011, at 09:20, Amit Bueno wrote: > // creating and reading more data > SubChunks[NewSubChunk]^.read(Fl); > > // NewSubChunk function > Function Chunk.NewSubchunk: integer; > begin > setlength(SubChunks, High(SubChunks)+2); > New(subchunks[High(SubChunks)], create); > SubChunks[High(SubChunks)]^.Data := Self.Data; > Result := High(SubChunks); > end; > > // --------- > The procedure fails when running the: > SubChunks[NewSubChunk]^.read(Fl); > > All the procedure works fine on Delphi, but on fpc under iMac it > fails. > Why? > Are you compiling in Delphi mode? Please *always* post compilable source code that demonstrates the problem when asking for help, not uncompilable snippets. Don't make people guess about what you might have written or about how you are compiling it. Jonas
Jonas Maebe
to Amit Bueno
Amit Bueno wrote
Have you got the prof.rar?

No. I've just looked at the posts held for moderation and your mail is over 5MB. That is way too large to send to a mailing list. First of all, make sure to only include source code, not compiled units, programs or resources. Secondly, if it's that big, put it online on a website and only send the link to the mailing list.

Jonas

Amit Bueno

Seems that the ios doesn't allocated properly my objects,. after allocation when trying to run a function from the object it fails. I tried replacing the New(subchunks[X]) to SubChunks[X] := AllocMem(sizeof(Chunk));
Again this works on the windows, it doesn't on the ios.

Have you got any ideas?
---
Amit

Amit Bueno wrote 32 lines
Attached is the shrinked prof.rar file (481kb). You can download it as well from: http://bueno.co.il/files/prof.rar --- Amit -----Original Message----- From: On Behalf Of Jonas Maebe Sent: Wednesday, February 02, 2011 3:28 PM To: FPC-Pascal users discussions Subject: Re: [fpc-pascal] dynamic array and new/dispose On 02 Feb 2011, at 14:10, Amit Bueno wrote: > Have you got the prof.rar? No. I've just looked at the posts held for moderation and your mail is over 5MB. That is way too large to send to a mailing list. First of all, make sure to only include source code, not compiled units, programs or resources. Secondly, if it's that big, put it online on a website and only send the link to the mailing list. Jonas _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
ik
to Amit Bueno
Amit Bueno wrote 8 lines
Seems that the ios doesn't allocated properly my objects,. after allocation when trying to run a function from the object it fails. I tried replacing the New(subchunks[X]) to SubChunks[X] := AllocMem(sizeof(Chunk)); Again this works on the windows, it doesn't on the ios. Have you got any ideas?

Does "X" actually exists as an index (I mean the value of it) ?

Show quoted text 45 lines
--- Amit On Wed, Feb 2, 2011 at 3:34 PM, Amit Bueno wrote: > Attached is the shrinked prof.rar file (481kb). > You can download it as well from: > http://bueno.co.il/files/prof.rar > > > --- > Amit > > -----Original Message----- > From: > On Behalf Of Jonas Maebe > Sent: Wednesday, February 02, 2011 3:28 PM > To: FPC-Pascal users discussions > Subject: Re: [fpc-pascal] dynamic array and new/dispose > > > On 02 Feb 2011, at 14:10, Amit Bueno wrote: > > > Have you got the prof.rar? > > No. I've just looked at the posts held for moderation and your mail is > over > 5MB. That is way too large to send to a mailing list. First of all, make > sure to only include source code, not compiled units, programs or > resources. > Secondly, if it's that big, put it online on a website and only send the > link to the mailing list. > > > Jonas > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Ido
LINESIP - Opening the source for communication
http://www.linesip.com
http://www.linesip.co.il

Amit Bueno
to ik

The SubChunks are -- 'var subchunks array of pchunk;' contained in chunk before allocating the subchunks a setlength(subchunks, High(subchunks)+2) is called.
---
Amit

ik wrote 77 lines
On Wed, Feb 2, 2011 at 16:57, Amit Bueno wrote: > Seems that the ios doesn't allocated properly my objects,. after > allocation when trying to run a function from the object it fails. > I tried replacing the New(subchunks[X]) to SubChunks[X] := > AllocMem(sizeof(Chunk)); > Again this works on the windows, it doesn't on the ios. > > Have you got any ideas? > Does "X" actually exists as an index (I mean the value of it) ? > --- > Amit > > On Wed, Feb 2, 2011 at 3:34 PM, Amit Bueno wrote: > >> Attached is the shrinked prof.rar file (481kb). >> You can download it as well from: >> http://bueno.co.il/files/prof.rar >> >> >> --- >> Amit >> >> -----Original Message----- >> From: >> On Behalf Of Jonas >> Maebe >> Sent: Wednesday, February 02, 2011 3:28 PM >> To: FPC-Pascal users discussions >> Subject: Re: [fpc-pascal] dynamic array and new/dispose >> >> >> On 02 Feb 2011, at 14:10, Amit Bueno wrote: >> >> > Have you got the prof.rar? >> >> No. I've just looked at the posts held for moderation and your mail is >> over >> 5MB. That is way too large to send to a mailing list. First of all, make >> sure to only include source code, not compiled units, programs or >> resources. >> Secondly, if it's that big, put it online on a website and only send the >> link to the mailing list. >> >> >> Jonas >> _______________________________________________ >> fpc-pascal maillist - >> http://lists.freepascal.org/mailman/listinfo/fpc-pascal >> > > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > Ido LINESIP - Opening the source for communication http://www.linesip.com http://www.linesip.co.il _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
ik
to Amit Bueno
Amit Bueno wrote
The SubChunks are -- 'var subchunks array of pchunk;' contained in chunk before allocating the subchunks a setlength(subchunks, High(subchunks)+2) is called.
But does the X variable point to existed index in the array ? if not, then
it explains the "access violation".
Show quoted text 88 lines
--- Amit On Wed, Feb 2, 2011 at 5:15 PM, ik wrote: > > > > On Wed, Feb 2, 2011 at 16:57, Amit Bueno wrote: > >> Seems that the ios doesn't allocated properly my objects,. after >> allocation when trying to run a function from the object it fails. >> I tried replacing the New(subchunks[X]) to SubChunks[X] := >> AllocMem(sizeof(Chunk)); >> Again this works on the windows, it doesn't on the ios. >> >> Have you got any ideas? >> > > Does "X" actually exists as an index (I mean the value of it) ? > > > >> --- >> Amit >> >> On Wed, Feb 2, 2011 at 3:34 PM, Amit Bueno wrote: >> >>> Attached is the shrinked prof.rar file (481kb). >>> You can download it as well from: >>> http://bueno.co.il/files/prof.rar >>> >>> >>> --- >>> Amit >>> >>> -----Original Message----- >>> From: >>> On Behalf Of Jonas >>> Maebe >>> Sent: Wednesday, February 02, 2011 3:28 PM >>> To: FPC-Pascal users discussions >>> Subject: Re: [fpc-pascal] dynamic array and new/dispose >>> >>> >>> On 02 Feb 2011, at 14:10, Amit Bueno wrote: >>> >>> > Have you got the prof.rar? >>> >>> No. I've just looked at the posts held for moderation and your mail is >>> over >>> 5MB. That is way too large to send to a mailing list. First of all, make >>> sure to only include source code, not compiled units, programs or >>> resources. >>> Secondly, if it's that big, put it online on a website and only send the >>> link to the mailing list. >>> >>> >>> Jonas >>> _______________________________________________ >>> fpc-pascal maillist - >>> http://lists.freepascal.org/mailman/listinfo/fpc-pascal >>> >> >> >> _______________________________________________ >> fpc-pascal maillist - >> http://lists.freepascal.org/mailman/listinfo/fpc-pascal >> > > > Ido > LINESIP - Opening the source for communication > http://www.linesip.com > http://www.linesip.co.il > > > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Ido
LINESIP - Opening the source for communication
http://www.linesip.com
http://www.linesip.co.il

Amit Bueno
to ik

Of course it does -- otherwise it won't have worked on delphi under windows. The X = High(SubChunks);
---
Amit

ik wrote 113 lines
On Wed, Feb 2, 2011 at 17:18, Amit Bueno wrote: > The SubChunks are -- 'var subchunks array of pchunk;' contained in chunk > before allocating the subchunks a setlength(subchunks, High(subchunks)+2) > is called. > But does the X variable point to existed index in the array ? if not, then it explains the "access violation". > --- > Amit > > On Wed, Feb 2, 2011 at 5:15 PM, ik wrote: > >> >> >> >> On Wed, Feb 2, 2011 at 16:57, Amit Bueno wrote: >> >>> Seems that the ios doesn't allocated properly my objects,. after >>> allocation when trying to run a function from the object it fails. >>> I tried replacing the New(subchunks[X]) to SubChunks[X] := >>> AllocMem(sizeof(Chunk)); >>> Again this works on the windows, it doesn't on the ios. >>> >>> Have you got any ideas? >>> >> >> Does "X" actually exists as an index (I mean the value of it) ? >> >> >> >>> --- >>> Amit >>> >>> On Wed, Feb 2, 2011 at 3:34 PM, Amit Bueno wrote: >>> >>>> Attached is the shrinked prof.rar file (481kb). >>>> You can download it as well from: >>>> http://bueno.co.il/files/prof.rar >>>> >>>> >>>> --- >>>> Amit >>>> >>>> -----Original Message----- >>>> From: >>>> On Behalf Of Jonas >>>> Maebe >>>> Sent: Wednesday, February 02, 2011 3:28 PM >>>> To: FPC-Pascal users discussions >>>> Subject: Re: [fpc-pascal] dynamic array and new/dispose >>>> >>>> >>>> On 02 Feb 2011, at 14:10, Amit Bueno wrote: >>>> >>>> > Have you got the prof.rar? >>>> >>>> No. I've just looked at the posts held for moderation and your mail is >>>> over >>>> 5MB. That is way too large to send to a mailing list. First of all, >>>> make >>>> sure to only include source code, not compiled units, programs or >>>> resources. >>>> Secondly, if it's that big, put it online on a website and only send >>>> the >>>> link to the mailing list. >>>> >>>> >>>> Jonas >>>> _______________________________________________ >>>> fpc-pascal maillist - >>>> http://lists.freepascal.org/mailman/listinfo/fpc-pascal >>>> >>> >>> >>> _______________________________________________ >>> fpc-pascal maillist - >>> http://lists.freepascal.org/mailman/listinfo/fpc-pascal >>> >> >> >> Ido >> LINESIP - Opening the source for communication >> http://www.linesip.com >> http://www.linesip.co.il >> >> >> >> _______________________________________________ >> fpc-pascal maillist - >> http://lists.freepascal.org/mailman/listinfo/fpc-pascal >> > > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > Ido LINESIP - Opening the source for communication http://www.linesip.com http://www.linesip.co.il _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
ik
to Amit Bueno

I have created a proof of concept from your code:

----------------------------
{$mode delphi}
program test;
type
  PChunk = ^Chunk;
  Chunk = Object
       SubChunks : array of PChunk;
       data : Pointer;
       Function NewSubchunk: integer;
  end;
function Chunk.NewSubchunk :integer;
var
 x : integer;
begin
  x := High(SubChunks)+2;
  setlength(SubChunks, x);
  New(subchunks[x]);
  SubChunks[x]^.Data := Self.Data;
  Result := x;
end;
var
 ac : Chunk;
begin
 ac.data := PChar('Hello world');
 writeln(ac.NewSubchunk);
end.

-----------------------

Does it work for you (I do not have any apple to test it on, but it works on my Linux) ?

And yes it leaks memory :)

Ido
LINESIP - Opening the source for communication
http://www.linesip.com
http://www.linesip.co.il

Amit Bueno wrote 128 lines
Of course it does -- otherwise it won't have worked on delphi under windows. The X = High(SubChunks); --- Amit On Wed, Feb 2, 2011 at 5:27 PM, ik wrote: > On Wed, Feb 2, 2011 at 17:18, Amit Bueno wrote: > >> The SubChunks are -- 'var subchunks array of pchunk;' contained in >> chunk >> before allocating the subchunks a setlength(subchunks, High(subchunks)+2) >> is called. >> > > But does the X variable point to existed index in the array ? if not, then > it explains the "access violation". > > >> --- >> Amit >> >> On Wed, Feb 2, 2011 at 5:15 PM, ik wrote: >> >>> >>> >>> >>> On Wed, Feb 2, 2011 at 16:57, Amit Bueno wrote: >>> >>>> Seems that the ios doesn't allocated properly my objects,. after >>>> allocation when trying to run a function from the object it fails. >>>> I tried replacing the New(subchunks[X]) to SubChunks[X] := >>>> AllocMem(sizeof(Chunk)); >>>> Again this works on the windows, it doesn't on the ios. >>>> >>>> Have you got any ideas? >>>> >>> >>> Does "X" actually exists as an index (I mean the value of it) ? >>> >>> >>> >>>> --- >>>> Amit >>>> >>>> On Wed, Feb 2, 2011 at 3:34 PM, Amit Bueno wrote: >>>> >>>>> Attached is the shrinked prof.rar file (481kb). >>>>> You can download it as well from: >>>>> http://bueno.co.il/files/prof.rar >>>>> >>>>> >>>>> --- >>>>> Amit >>>>> >>>>> -----Original Message----- >>>>> From: >>>>> On Behalf Of Jonas >>>>> Maebe >>>>> Sent: Wednesday, February 02, 2011 3:28 PM >>>>> To: FPC-Pascal users discussions >>>>> Subject: Re: [fpc-pascal] dynamic array and new/dispose >>>>> >>>>> >>>>> On 02 Feb 2011, at 14:10, Amit Bueno wrote: >>>>> >>>>> > Have you got the prof.rar? >>>>> >>>>> No. I've just looked at the posts held for moderation and your mail is >>>>> over >>>>> 5MB. That is way too large to send to a mailing list. First of all, >>>>> make >>>>> sure to only include source code, not compiled units, programs or >>>>> resources. >>>>> Secondly, if it's that big, put it online on a website and only send >>>>> the >>>>> link to the mailing list. >>>>> >>>>> >>>>> Jonas >>>>> _______________________________________________ >>>>> fpc-pascal maillist - >>>>> http://lists.freepascal.org/mailman/listinfo/fpc-pascal >>>>> >>>> >>>> >>>> _______________________________________________ >>>> fpc-pascal maillist - >>>> http://lists.freepascal.org/mailman/listinfo/fpc-pascal >>>> >>> >>> >>> Ido >>> LINESIP - Opening the source for communication >>> http://www.linesip.com >>> http://www.linesip.co.il >>> >>> >>> >>> _______________________________________________ >>> fpc-pascal maillist - >>> http://lists.freepascal.org/mailman/listinfo/fpc-pascal >>> >> >> >> _______________________________________________ >> fpc-pascal maillist - >> http://lists.freepascal.org/mailman/listinfo/fpc-pascal >> > > > Ido > LINESIP - Opening the source for communication > http://www.linesip.com > http://www.linesip.co.il > > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Amit Bueno
to ik

Seems that I found a workaround.

When calling the NewSubChunk in the following syntax:

SubChunks[NewSubChunk]^.read(fl);  /// raise an exception.

While

MyIndex := NewSubChunk;
SubChunks[MyIndex]^.read(fl);  /// raise an exception.

Doesn't raise any excetion….

Maybe the experts could say why, I don't know why…

Amit

Earlier message 145 lines
From: On Behalf Of ik Sent: Wednesday, February 02, 2011 5:48 PM To: FPC-Pascal users discussions Subject: Re: [fpc-pascal] dynamic array and new/dispose I have created a proof of concept from your code: ---------------------------- {$mode delphi} program test; type PChunk = ^Chunk; Chunk = Object SubChunks : array of PChunk; data : Pointer; Function NewSubchunk: integer; end; function Chunk.NewSubchunk :integer; var x : integer; begin x := High(SubChunks)+2; setlength(SubChunks, x); New(subchunks[x]); SubChunks[x]^.Data := Self.Data; Result := x; end; var ac : Chunk; begin ac.data := PChar('Hello world'); writeln(ac.NewSubchunk); end. ----------------------- Does it work for you (I do not have any apple to test it on, but it works on my Linux) ? And yes it leaks memory :) Ido LINESIP - Opening the source for communication http://www.linesip.com http://www.linesip.co.il On Wed, Feb 2, 2011 at 17:29, Amit Bueno wrote: Of course it does -- otherwise it won't have worked on delphi under windows. The X = High(SubChunks); --- Amit On Wed, Feb 2, 2011 at 5:27 PM, ik wrote: On Wed, Feb 2, 2011 at 17:18, Amit Bueno wrote: The SubChunks are -- 'var subchunks array of pchunk;' contained in chunk before allocating the subchunks a setlength(subchunks, High(subchunks)+2) is called. But does the X variable point to existed index in the array ? if not, then it explains the "access violation". --- Amit On Wed, Feb 2, 2011 at 5:15 PM, ik wrote: On Wed, Feb 2, 2011 at 16:57, Amit Bueno wrote: Seems that the ios doesn't allocated properly my objects,. after allocation when trying to run a function from the object it fails. I tried replacing the New(subchunks[X]) to SubChunks[X] := AllocMem(sizeof(Chunk)); Again this works on the windows, it doesn't on the ios. Have you got any ideas? Does "X" actually exists as an index (I mean the value of it) ? --- Amit On Wed, Feb 2, 2011 at 3:34 PM, Amit Bueno wrote: Attached is the shrinked prof.rar file (481kb). You can download it as well from: http://bueno.co.il/files/prof.rar --- Amit -----Original Message----- From: On Behalf Of Jonas Maebe Sent: Wednesday, February 02, 2011 3:28 PM To: FPC-Pascal users discussions Subject: Re: [fpc-pascal] dynamic array and new/dispose On 02 Feb 2011, at 14:10, Amit Bueno wrote: > Have you got the prof.rar? No. I've just looked at the posts held for moderation and your mail is over 5MB. That is way too large to send to a mailing list. First of all, make sure to only include source code, not compiled units, programs or resources. Secondly, if it's that big, put it online on a website and only send the link to the mailing list. Jonas
Amit Bueno
to Jeppe Johansen

I tested it both on ARM, and on i386 (iphonesim).

I used the latest fpc svn trunk. Options I can say it wasn't something exceptional, only –Mdelphi or something.

Amit

Earlier message 15 lines
From: On Behalf Of Jeppe Johansen Sent: Wednesday, February 02, 2011 8:44 PM To: FPC-Pascal users discussions Subject: Re: [fpc-pascal] dynamic array and new/dispose Den 02-02-2011 17:26, Amit Bueno skrev: Snipped And you say it was on iOS? On ARM I suppose? What sort of compiler version and options do you use?
Jonas Maebe
to Jonas Maebe: No. I've just looked at the posts held for moderation and your mail is over 5MB. That is…
Jonas Maebe wrote 6 lines
On 02 Feb 2011, at 14:10, Amit Bueno wrote: > Have you got the prof.rar? No. I've just looked at the posts held for moderation and your mail is over 5MB. That is way too large to send to a mailing list.

I just saw the new message in the moderation queue, but it's still over 600KB (email makes files about 30% bigger because of the inefficient encoding), which is too big for a mailing list (50-100KB is the maximum I'll let through, <=50KB is let through automatically). However, it's also available for download now:

***
Attached is the shrinked prof.rar file (481kb).
You can download it as well from:
http://bueno.co.il/files/prof.rar

---
Amit
***

Jonas

Jonas Maebe
to Amit Bueno: Seems that I found a workaround. When calling the NewSubChunk in the following syntax:…
Amit Bueno wrote 10 lines
Seems that I found a workaround. When calling the NewSubChunk in the following syntax: SubChunks[NewSubChunk]^.read(fl); /// raise an exception. While MyIndex := NewSubChunk; SubChunks[MyIndex]^.read(fl); /// raise an exception. Doesn't raise any excetion…. Maybe the experts could say why, I don't know why…

The reason is that SubChunks (a dynamic array) can be nil before NewSubChunk() (a function that allocates 2 extra elements for that array) is called. So if the compiler first loads SubChuncks (=nil) in a register, then calls NewSubChunk, and then indexes this nil pointer using that function result, you will get at an access violation.

The original code is wrong because Pascal does not define a particular order in which such expressions have to be evaluated by the compiler. In general, calling functions with side effects inside expressions that can be affected by those side effects is very bad practice (and dangerous, as you've noticed).

The fact that it doesn't crash in the Delphi version that you tested is pure coincidence, and it may very well crash is older or newer versions.

Jonas