0

I have a procedure that accepts 2 string parameters, one of them has a default value. Inside the procedure, I want to concatenate one and the other and some literals to form one larger string. Somehow, I'm getting an AV... any ideas?

code is something like this

{$WRITEABLECONST ON}
constructor MyClass.Create(s1: string; s2: string = GlobalConstant);
var s3: string;
begin
    ....
    if (s2 = '') then s2 := GlobalConstant + ' (' + s1 + ')';      // AV here
    ....
end;

If I assign GlobalConstant + ' (' + s1 + ') to s3, then assign s3 to s2 the AV disappears. Why? What is wrong with writing to the string parameter directly?

mghie
  • 32,028
  • 6
  • 87
  • 129
Sam
  • 2,663
  • 10
  • 41
  • 60
  • using the code you've provided, i'm unable to reproduce with d2007. – glob Feb 16 '10 at 05:39
  • There's nothing at all wrong with that code. Hard to imagine why it would AV there. How ia "GlobalConstant" declared? – Nick Hodges Feb 16 '10 at 06:06
  • @Nick Hodges. I know it's declared fine because the code works in another iteration. Yesterday it was failing on the 6th iteration, today it failed on the 3rd... and I had AV's in other places too, so I'm leaning towards Paul-Jan's answer. Something else is a miss. Thanks anyway Nick, I appreciate it. – Sam Feb 16 '10 at 11:39
  • Sam, Nick didn't ask *whether* it was declared; he asked *how*. Show more code. In particular, show the place where this function gets called. Also, is the `$WriteableConst` directive relevant to this example? It shouldn't be. – Rob Kennedy Feb 16 '10 at 17:06
  • @Rob Kennedy, Sorry, but I can't show more code. The constant is declared with the most basic of const statements in the unit that houses MyClass. The $WritableConst directive happens to be in the code so I thought I'd show it since it sounds like it might be relevant (I didn't know it wasn't). The question has been answered. There is nothing wrong with the way the strings are being concatenated or assigned, and the AV could be appearing here as a result of bad code elsewhere. I'm adequately convinced, now I just have to learn how to use FastMM. Thanks again guys. – Sam Feb 16 '10 at 21:19

1 Answers1

6

Something else in your code is wrong, indirectly resulting in an Access Violation in that location (corruption). Use FastMM with FullDebugMode on to figure out what it is, and how to solve it.

Paul-Jan
  • 16,746
  • 1
  • 63
  • 95
  • @Paul-Jan, Thank you, sounds plausible to me. I'll give that a try and report back tomorrow. – Sam Feb 16 '10 at 11:44
  • I've read that page you provided the link for, and the 2 that it recommends as well. I'm using FastMM4, enabled FullDebugMode (by using the FastMM4OptionsInterface GUI), UseDebugDCUs, Stack Frames, Local Symbols, Reference Info... rebuilt Project and ran, but I don't get a log file (or maybe I can't find it!) or any MessageBoxes. I still get an AV, but now the code breaks inside class function TObject.InheritsFrom(AClass: TClass): Boolean; in WIN32\RTL\SYS\System.pas... :-) I had a full head of hair yesterday, it'll be all gone by tomorrow! – Sam Feb 16 '10 at 23:52
  • Oops, forgot to create a .map file! Project Options - Linker - Detailed Map file. (Come-on, feel sorry for the newbie.) – Sam Feb 17 '10 at 03:42
  • I downloaded the latest version of madExcept, and it works like a charm. I want to say "better than FastMM" but they're both good tools, but I found madExcept easier and more user-friendly. Anyway, the code was referencing an object that hadn't been initialised. It's weird how the AV appeared in different places though... Thanks. – Sam Feb 18 '10 at 03:18