You are reporting an access violation in
StrNew(S)
where S
is of type PChar
. The explanation for that, with probability very close to 1, is that S
is not in fact a pointer to null terminated array of WideChar
.
In Delphi 7, PChar
is an alias for PAnsiChar
. That is a pointer to null terminated array of AnsiChar
, i.e. 8 bit characters. In Delphi XE2, PChar
is an alias for PWideChar
, a pointer to null terminated array of WideChar
, i.e. 16 bit characters.
It helps to understand what StrNew
does. It walks the array until it finds a null character. For 8 bit text that is a single zero byte. For 16 bit text, the null is a zero 16 bit word. Then it allocates a new block of memory of the same length as the input string, and makes a copy into that new memory. The source code is:
function StrNew(const Str: PWideChar): PWideChar;
var
Size: Cardinal;
begin
if Str = nil then Result := nil else
begin
Size := StrLen(Str) + 1;
Result := StrMove(WideStrAlloc(Size), Str, Size);
end;
end;
The only plausible failure mode is that when StrLen
walks the array, it attempts an invalid memory read. And that can only happen if your input parameter is invalid. In other words, this must be a programming error on your part.
One possible explanation is that you are in fact passing 8 bit text to this function despite promising to pass 16 bit text. An easy mistake to make, especially if you are not yet fully familiar with the Unicode change. The 8 bit text has a zero terminator, but the byte that follows happens not to be zero. Or the zero byte falls at an odd numbered offset from the start. And then StrNew
continues walking the buffer, but now it is off the end and it so happens that it doesn't find a zero word before overrunning into an address that has not been allocated. And that is an access violation.
If that is so then solution will be either:
- Change the function's parameter to be of type
PAnsiChar
, and fix the dubious casting at the call site.
- Pass the function 16 bit text as it requires.
In your update you include the address which cannot be read, 0xffffffff
. This is -1
in hex. And that would seem to be the most prosaic of errors. Your pointer is completely bogus! Your exact error message can be reproduced with this code: StrNew(PChar(-1))
.
I don't have enough information here to tell you why your pointer is bogus. Hopefully you've learnt some debugging and diagnostic techniques that will enable you to solve the problem. At least you now know that the error is in your code.
Assuming that BuscaName2 and SearchName2 are one and the same thing, then you need look no further. Local procedures can only be called from a containing function. As @Rob correctly says, the use of @ with procedures is almost always incorrect and is a warning sign of serious problems with your code.