Recent

Author Topic: Error 204 or Invalid Pointer when using SetLength to 0 on a Dynamic Array  (Read 3430 times)

MarkMLl

  • Hero Member
  • *****
  • Posts: 7062
'rTakersList' is nowhere declared, but funnily enough, worked on and setlength'ed  :D %) :P

And more significant, it's got zeros slapped over its access structure with no consideration of what it contains.

Now /hopefully/ we've not intimidated OP to the extent that he never comes back.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

MaxM74

  • New Member
  • *
  • Posts: 14
I have changed the post, there is some copy/past errors.
Sorry for code tags is the first time i post here

1) is a simple list of records, in other part of the code i add record

Code: Pascal  [Select][+][-]
  1.   SetLength(rList, Length(rList)+1);
  2.  
  3.   rList[Length(rList)-1].Name:='The Name';
  4.   rList[Length(rList)-1].Inst :=aClass;  //this is a class(TNoRefCountObject, IMyInterface) Instance
  5.  

The question is SetLength(rList, 0) or rList :=nil calls  rList[ii].Inst:=Nil; ?

if i have time i do a complete test and post it. Thanks
« Last Edit: June 19, 2024, 08:54:55 am by MaxM74 »

MaxM74

  • New Member
  • *
  • Posts: 14
Code: Pascal  [Select][+][-]
  1.     try
  2.        if Assigned(rList.Inst)  then rList.Inst.DoSomething;
  3.     except
  4.     end;
Really????
Better write it like this:
Code: Pascal  [Select][+][-]
  1.     try
  2.        if Assigned(rList.Inst)  then rList.Inst.DoSomething;
  3.     except
  4.       // I just shot myself
  5.     end;

This usually happens when a noob has already had exceptions which he or she deemed nonsense and so "protected" the code in the wrong place and in the wrong way.
Now Suicide Sue has likely hidden the real error and can not debug it because there is no valid stackframe.... >:D

(That's twice I mentioned this today , but Eugene promissed he did not use that and he is not a noob)
The problem with this code is that assigned() can fail: not everything that passes assigned is really assigned. It just means the pointer is not nil.
That pointer can be invalid:use after free.
In this case it is even more suicidal because Assigned is already a protection test. If assigned fails or causes an error, you need to find out why. Not eat it with an empty exception.
Also do not mention FreeAndNil, plz. in this context. It just demonstrates why it is bad.

The real problem is the interface in the record: that is always assigned, but the class that supports that interface can be gone to heaven. Store the class reference, not the interface.

1) The class that support the interface is derived from TNoRefCountObject and freed after the code.

2)What do you mean with "class reference"? Is a plugin list, i don't know the class that implement the interface.

The question is why setting length of array to 0 set the Inst field to nil  ?
« Last Edit: June 19, 2024, 09:29:16 am by MaxM74 »

TRon

  • Hero Member
  • *****
  • Posts: 2856
I have changed the post, there is some copy/past errors.
Thank you for the correction(s) MaxM74.

However, I (and probably others as well) have an issue with modifying an original post after it was responded to.

When someone reads back this thread on a later date when things are long gone and forgotten then the initial reactions make no sense anymore whatsoever putting these reactions in some sort of a shady alternative reality. It becomes a bad read, provide strange impressions and while that could perhaps be cleared after reading the post I respond to you would have to re-read the thread to let it make sense.

So therefor the suggestion that next time when something like that happens (it happens to the best of use) then try acknowledge the fact of the error in a new post and also paste the corrected code in that same post. That way the discussion and reaction(s) still makes sense  :)

If you really want to be above and beyond then you can always edit your first post and make the same acknowledgement and refer to the message where the corrected code was posted.

just 2 cents.
« Last Edit: June 19, 2024, 09:04:49 am by TRon »

MarkMLl

  • Hero Member
  • *****
  • Posts: 7062
However, I (and probably others as well) have an issue with modifying an original post after it was responded to.

OK that's bad style, but now let's crack on and try to sort the problems.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

TRon

  • Hero Member
  • *****
  • Posts: 2856
OK that's bad style, but now let's crack on and try to sort the problems.
True and same as some of the comments but tbh I have no idea whatsoever ...

For now I would have to assume that the error is coming from somewhere else.

to TS: what is the error / stacktrace that FPC reports when you put a try except/finally block around the setlength ?
« Last Edit: June 19, 2024, 09:37:15 am by TRon »

Thaddy

  • Hero Member
  • *****
  • Posts: 15130
  • Censorship about opinions does not belong here.
I already gave him the answer, but he does not listen.
An interface reference is not the same as a reference to a class.
Of course the national anthem of the U.S.A. was written by Jimi Hendrix, didn't you know that?

MaxM74

  • New Member
  • *
  • Posts: 14
I already gave him the answer, but he does not listen.
An interface reference is not the same as a reference to a class.

I cannot do something like

Code: Pascal  [Select][+][-]
  1. TInfo = record
  2.     Name: String;
  3.     Inst: TSomeImplementer;
  4.   end;
  5.  

because the code of those who implement the interface is in libraries, I only maintain a list of implementers. when it is necessary the Main Application call

Inst.DoSomething   where Inst is declared as IMyInterface
« Last Edit: June 19, 2024, 12:48:42 pm by MaxM74 »

MaxM74

  • New Member
  • *
  • Posts: 14
Stack Call

#0 FPC_INTF_DECR_REF+21 at :0
#1 FPC_FINALIZE+280 at :0
#2 SYSTEM_$$_RECORDRTTI$POINTER$POINTER$TRTTIPROC+73 at :0
#3 FPC_FINALIZE+266 at :0
#4 FPC_FINALIZE_ARRAY+103 at :0
#5 FPC_DYNARRAY_CLEAR+124 at :0
#6 TDigIt_Bridge_Takers.Destroy(TDigIt_Bridge_Takers($000000000160FB50), ^$__vtbl_ptr_type($0000000000000001)) at DigIt_Bridge_Impl.pas:232
#7 SYSTEM$_$TOBJECT_$__$$_FREE+27 at :0
#8 TDigIt_Bridge.Destroy(TDigIt_Bridge($000000000160FA50), ^$__vtbl_ptr_type($0000000000000001)) at DigIt_Bridge_Impl.pas:312
#9 SYSTEM$_$TOBJECT_$__$$_FREE+27 at :0
« Last Edit: June 19, 2024, 01:56:18 pm by MaxM74 »

MarkMLl

  • Hero Member
  • *****
  • Posts: 7062
OK. Now I think that gets you to the point where you need to produce as simple a project as you can to demonstrate the problem, so that Thaddy can show you an adequate workaround.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 15130
  • Censorship about opinions does not belong here.
Well, my other post from today happens to give a solution  :) although I am not satisfied with that yet.

https://forum.lazarus.freepascal.org/index.php/topic,67624.msg520956/topicseen.html#new

That code works and would also work in this case.
(It can transparently create instances of classes that support an interface or treat an initialized interface as an instance)

But now he writes that he has no control over the interfaces and that is actually a complicating matter.
What is definitely wrong is the way he uses that record.
IF there is an instance, he would call Supports() to know if a particular class instance supports that interface.
Storing the interface instead of the class reference will never work.
He can do something like this:
Code: Pascal  [Select][+][-]
  1. TInfo = record
  2.     Name: String;
  3.     Inst: TSomeImplementer;
  4.   end;
and use is and as and supports.
But there needs to be at least a pointer to a genuine instance.

 
« Last Edit: June 19, 2024, 04:29:53 pm by Thaddy »
Of course the national anthem of the U.S.A. was written by Jimi Hendrix, didn't you know that?

MarkMLl

  • Hero Member
  • *****
  • Posts: 7062
But now he writes that he has no control over the interfaces and that is actually a complicating matter.

We need to see what he's doing. I am sure we would all be happy if he supplied a couple of simple units, one of which was annotated "Stuff I'm working on" and the other something like "Supplied by a third party, outside my control".

The important thing is that he believes that that combination of units presents him with an intractable problem.

I'm trying to keep things moving and people talking to each other, now that we've got past the initial hiccoughs.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

cdbc

  • Hero Member
  • *****
  • Posts: 1327
    • http://www.cdbc.dk
Hi
How about:
Code: Pascal  [Select][+][-]
  1. TInfo = record
  2.     Name: String;
  3.     Obj: TObject; // maybe TInterfacedObject?!?
  4.   end;
and then use:
Code: Pascal  [Select][+][-]
  1.  if Info.Obj.Getinterface(SomeGUID,Inst) then Inst.Dosomething;
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

jamie

  • Hero Member
  • *****
  • Posts: 6366
as already mentioned somewhere in this mess, there is no INSTANCE of that class.

it must be something like this somewhere:
 
Code: Pascal  [Select][+][-]
  1.  Inst := TSomeClass.Create(someOptionalParameter);
  2.  

maybe my old brain missed something..

The only true wisdom is knowing you know nothing

MarkMLl

  • Hero Member
  • *****
  • Posts: 7062
maybe my old brain missed something..

Yes, that we're already trying to work constructively towards a solution but need a compilable test project from OP.

At this stage we still don't know what he's got in there, so can't say for certain that he's skipped the instantiation.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018