Error 204 or Invalid Pointer when using SetLength to 0 on a Dynamic Array lazarus / 21-General ---------------------------------------------------------------------- MaxM74 · 2024-06-18 11:49 FPC is 3.3.1 i have a dynamic array of this type Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i typeTMyInfo = record    Name: String;    Inst: IMyInterface; //Impemented with a TNoRefCountObject descendent class  end; var rList: array of TMyInfo;  while destroying the application form i do this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var   i:Integer; begin  for i:=0 to Length(rList)-1 do  begin    try       if Assigned(rList[i].Inst)  then rList[i].Inst.DoSomething;    except    end;  end;    SetLength(rList, 0); //EXCEPTION Invalid Pointer Operation HEREend;  If i change the code in this way i have no exception: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var   i:Integer; begin  for i:=0 to Length(rList)-1 do  begin    try       if Assigned(rList[i].Inst)  then rList[i].Inst.DoSomething;        rList[i].Name:='';       FillChar(rList[i], SizeOf(rList[i]), 0);    except    end;  end;    SetLength(rList, 0); //NO EXCEPTIONend;   Can anyone explain to me why? I have to think that SetLength tries to free "the contents of the record" by calling what? ---------------------------------------------------------------------- AlexTP · 2024-06-18 11:52 Please use forum CODE tag for code blocks. And... can you give the complete minimal example program? ---------------------------------------------------------------------- rvk · 2024-06-18 12:21 Quote from: MaxM74 on June 18, 2024, 01:49:10 pmCode: [Select]   SetLength(rTakersList, 0); //EXCEPTION Invalid Pointer Operation HEREI agree with the code tags. This is unreadable. But I see you do SetLength on rTakenList. What is rTakenList? You itterate through rList. Did you mean to do SetLength(rList, 0) ??? ---------------------------------------------------------------------- Bart · 2024-06-18 12:37 In essence there is no need to free the memory of the dynamic array when your program ends. (It may be good practice though). Instead of using SetLength(SomeArry, 0) you can use SomeArray := nil, saves you typing ;-) Bart ---------------------------------------------------------------------- cdbc · 2024-06-18 12:37 Hi Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i rList[i].instRegards Benny ---------------------------------------------------------------------- MarkMLl · 2024-06-18 13:11 I agree that OP deserves criticism for not using code tags: there's even a button for it on the standard forum UI. /Particularly/ in the case of somebody less experienced, there is absolutely no excuse for him to go back and read his message after posting it, and it should be immediately apparent that the example's been agrbled by the markup. However the thing that stands out, in the code that is supposedly working, is this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i FillChar(rTakersList, SizeOf(rTakersList), 0);  /What/ is rTakersList? If it is either something like a TList or is itself a dynamic array this is a recipe for disaster since it's attempting to zero an opaque access structure, not a data space. MarkMLl ---------------------------------------------------------------------- Zvoni · 2024-06-18 13:30 Agree with cdbc: I'm more surprised this compiles..... ---------------------------------------------------------------------- MarkMLl · 2024-06-18 14:53 We need somebody to render Joanna into an LLM, that can reject postings with apparent Pascal in the body :-) Actually, there's probably some very simple tests that could be used, e.g. the appearance of .[ in what should be undecorated English. MarkMLl ---------------------------------------------------------------------- Thaddy · 2024-06-18 15:22 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     try     if Assigned(rList.Inst)  then rList.Inst.DoSomething;    except    end;Really???? Better write it like this:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     try       if Assigned(rList.Inst)  then rList.Inst.DoSomething;    except      // I just shot myself    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.... (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. ---------------------------------------------------------------------- MarkMLl · 2024-06-18 17:27 Quote from: Thaddy on June 18, 2024, 05:22:41 pmThis 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.... Give him a break Thaddy. We're all piling on fairly hard because of the lack of code tags and the incomplete example, and when we see what he was actually trying to do things might make sense. MarkMLl ---------------------------------------------------------------------- Thaddy · 2024-06-18 18:33 Mark, I merely pointed out where the error came from. Use after free. ---------------------------------------------------------------------- rvk · 2024-06-18 18:39 Quote from: Thaddy on June 18, 2024, 08:33:39 pmMark, I merely pointed out where the error came from. Use after free. You mean could come from. (Or do you have that magic ball I lost  ) Besides... we have no idea what rTakersList is and where it comes from (the line that produces the error). ---------------------------------------------------------------------- Thaddy · 2024-06-18 19:04 Well, look at the record? No christal ball necessary. ---------------------------------------------------------------------- rvk · 2024-06-18 19:09 Quote from: Thaddy on June 18, 2024, 09:04:02 pmWell, look at the record? No christal ball necessary. The record in tList has nothing to do with freeing rTakersList, of which we know nothing about. So yeah, I find it really amazing you can find fault in this non compilable code, which errors on a line which has nothing to do with interfaces. ---------------------------------------------------------------------- cdbc · 2024-06-18 20:03 Hi The only list thingy I can find declared is:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var rList: array of TMyInfo;'rTakersList' is nowhere declared, but funnily enough, worked on and setlength'ed  Regards Benny ---------------------------------------------------------------------- MarkMLl · 2024-06-18 20:22 Quote from: cdbc on June 18, 2024, 10:03:33 pm'rTakersList' is nowhere declared, but funnily enough, worked on and setlength'ed  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 ---------------------------------------------------------------------- MaxM74 · 2024-06-19 06:46 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   SetLength(rList, Length(rList)+1);   rList[Length(rList)-1].Name:='The Name';  rList[Length(rList)-1].Inst :=aClass;  //this is a class(TNoRefCountObject, IMyInterface) Instance  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 ---------------------------------------------------------------------- MaxM74 · 2024-06-19 06:53 Quote from: Thaddy on June 18, 2024, 05:22:41 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     try     if Assigned(rList.Inst)  then rList.Inst.DoSomething;    except    end;Really???? Better write it like this:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     try       if Assigned(rList.Inst)  then rList.Inst.DoSomething;    except      // I just shot myself    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.... (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  ? ---------------------------------------------------------------------- TRon · 2024-06-19 07:00 Quote from: MaxM74 on June 19, 2024, 08:46:20 amI 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. ---------------------------------------------------------------------- MarkMLl · 2024-06-19 07:17 Quote from: TRon on June 19, 2024, 09:00:15 amHowever, 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 ---------------------------------------------------------------------- TRon · 2024-06-19 07:30 Quote from: MarkMLl on June 19, 2024, 09:17:47 amOK 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 ? ---------------------------------------------------------------------- Thaddy · 2024-06-19 07:42 I already gave him the answer, but he does not listen. An interface reference is not the same as a reference to a class. ---------------------------------------------------------------------- MaxM74 · 2024-06-19 10:45 Quote from: Thaddy on June 19, 2024, 09:42:14 amI 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TInfo = record    Name: String;    Inst: TSomeImplementer;  end;  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 ---------------------------------------------------------------------- MaxM74 · 2024-06-19 11:18 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 ---------------------------------------------------------------------- MarkMLl · 2024-06-19 13:23 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 ---------------------------------------------------------------------- Thaddy · 2024-06-19 13:57 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/t opicseen.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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TInfo = record    Name: String;    Inst: TSomeImplementer;  end;and use is and as and supports. But there needs to be at least a pointer to a genuine instance. ---------------------------------------------------------------------- MarkMLl · 2024-06-19 14:58 Quote from: Thaddy on June 19, 2024, 03:57:23 pmBut 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 ---------------------------------------------------------------------- cdbc · 2024-06-19 16:04 Hi How about:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TInfo = record    Name: String;    Obj: TObject; // maybe TInterfacedObject?!?  end;and then use:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  if Info.Obj.Getinterface(SomeGUID,Inst) then Inst.Dosomething;Regards Benny ---------------------------------------------------------------------- jamie · 2024-06-19 21:51 as already mentioned somewhere in this mess, there is no INSTANCE of that class. it must be something like this somewhere:  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  Inst := TSomeClass.Create(someOptionalParameter);  maybe my old brain missed something.. ---------------------------------------------------------------------- MarkMLl · 2024-06-20 05:56 Quote from: jamie on June 19, 2024, 11:51:30 pmmaybe 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 ---------------------------------------------------------------------- Zvoni · 2024-06-20 05:57 Noone asked the question: What is happening within DoSomething? I agree with Thaddy: Probably Use after Free ---------------------------------------------------------------------- MarkMLl · 2024-06-20 06:01 Quote from: Zvoni on June 20, 2024, 07:57:33 amI agree with Thaddy: Probably Use after Free Fixable using FreeAndNil()  >:-) MarkMLl ---------------------------------------------------------------------- MaxM74 · 2024-06-20 06:41 It's an error in my code, but it made me understand that during the resetting of the list the compiler decreases the RefCount of the Interfaces and therefore if it reaches zero they are freed. If I want to free them when I say I have to derive from TNoRefCountObject. Take a look on Attachments placing breakpoints in the destroys. ---------------------------------------------------------------------- MarkMLl · 2024-06-20 06:47 I'm hoping that Thaddy takes a look at that since he's been so insistent that it's easily fixable, and since I claim little expertise in interfaces etc. However I think it's fair to say that refcounts are fairly arcane, and I've only once before come across a case where somebody was considering the refcount of an interface at the application level (and IIRC that was related to getting property values while debugging, so in itself was fairly arcane). I'm very glad that we're collectively getting this sorted out... MarkMLl ---------------------------------------------------------------------- cdbc · 2024-06-20 07:11 Hi If OP is in charge of the interfaces in the libraries, the solution should be simple: switch the interface declaration to {$interfaces CORBA} or ofc use the 'TNoRefcountObject' to instantiate the interfaces... If not he could try to store the interface references in a weak reference i.e.: a pointer and the typecast it when needed?!? Regards Benny ---------------------------------------------------------------------- MarkMLl · 2024-06-20 07:21 Quote from: cdbc on June 20, 2024, 09:11:03 amIf OP is in charge of the interfaces in the libraries, OP's already said QuoteI cannot do something like ... because the code of those who implement the interface is in libraries, I only maintain a list of implementers. MarkMLl ---------------------------------------------------------------------- MaxM74 · 2024-06-20 07:27 Quote from: cdbc on June 20, 2024, 09:11:03 amHi If OP is in charge of the interfaces in the libraries, the solution should be simple: switch the interface declaration to {$interfaces CORBA} or ofc use the 'TNoRefcountObject' to instantiate the interfaces... If not he could try to store the interface references in a weak reference i.e.: a pointer and the typecast it when needed?!? Regards Benny I think I'll use them without RefCount, already the fact that I don't know when they will be released worries me. I also had problems declaring a private field of an interface type in a class that implements another interface, the field is inexplicably freed before the class is freed causing me quite a few problems. So I decided to always derive from TNoRefCountObject The Structure is like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TMyParams = class(TInterfacedObject, IParams)   //private fields, etc   //implementation of IParams methodsend; TAnotherClass = class(TInterfacedObject, ITaker)  protected    rParams: TMyParams;   //implementation of ITaker methods      constructor Create;end; constructor TAnotherClass.Create;begin  inherited Create;   rParams :=TMyParams.Create;end; ---------------------------------------------------------------------- MarkMLl · 2024-06-20 07:38 Quote from: MaxM74 on June 20, 2024, 09:27:18 amI think I'll use them without RefCount, already the fact that I don't know when they will be released worries me. OK but as specific questions (to try to anticipate what others might ask): * Is the library yours, i.e. could you modify it if that appeared appropriate? * Is it simply loaded into the FPC/Lazarus project, are you statically linking it, or is it demand-loaded? MarkMLl ---------------------------------------------------------------------- Thaddy · 2024-06-20 08:43 Quote from: MarkMLl on June 20, 2024, 08:01:53 amFixable using FreeAndNil()  >:-) MarkMLl As Basil Fawlty said, "Don't mention the war." ---------------------------------------------------------------------- Zvoni · 2024-06-20 10:04 Quote from: Thaddy on June 20, 2024, 10:43:42 amAs Basil Fawlty said, "Don't mention the war." And as Manuel would answer: "¿Qué?" ---------------------------------------------------------------------- MaxM74 · 2024-06-20 10:45 Quote from: MarkMLl on June 20, 2024, 09:38:16 amQuote from: MaxM74 on June 20, 2024, 09:27:18 amI think I'll use them without RefCount, already the fact that I don't know when they will be released worries me. OK but as specific questions (to try to anticipate what others might ask): * Is the library yours, i.e. could you modify it if that appeared appropriate? * Is it simply loaded into the FPC/Lazarus project, are you statically linking it, or is it demand-loaded? MarkMLl The Libraries are dinamically load. Think of an application that loads plugins from a folder. I'm on the Application side, plugins could be written in any language. For this reason I store a field of type Interface in the list record. In "self-written" libraries and internal plugins I will certainly derive from TNoRefCountObject ---------------------------------------------------------------------- MarkMLl · 2024-06-20 10:53 Quote from: MaxM74 on June 20, 2024, 12:45:27 pmThe Libraries are dinamically load. Think of an application that loads plugins from a folder. I'm on the Application side, plugins could be written in any language. For this reason I store a field of type Interface in the list record. In "self-written" libraries and internal plugins I will certainly derive from TNoRefCountObject  So if I interpret that correctly, you're basically inferring a certain reference counting behaviour on the part of the libraries/plugins you're using. I'm definitely not saying you're wrong doing so. If anybody's at fault it's the supplier of the external binary who (presumably) hasn't documented things adequately. MarkMLl