Re: Testing Delphi DLL crashes VB6 IDE



Rob Kennedy wrote:

Although the type in foo is now accurate, there's still the problem that s goes out of scope before the function returns, and thus the pointer is not valid by the time it arrives at the caller.

But since the address that s held is probably still within your process's address space, and COM probably hasn't re-allocated it to anything else, it probably still contains the same characters it did a moment ago when it was still a valid pointer, so the caller will blithely read it and get what you were expecting, so it appears to work. But a program that accidentally works most of the time is still broken.

Is this getting any closer?

...
uses Windows, ActiveX;
...
procedure DMesg( sText : WideString; sHead : WideString);
begin
Windows.MessageBoxW(0, PWideChar(sText), PWideChar(sHead), 0);
end;

function foo() : PWideChar;
var s : WideString;
begin
s := 'My dog''s got fleas';
result := ActiveX.SysAllocString(PWideChar(s));
end;

Kind regards,
Bruce.
begin:vcard
fn:Bruce M. Axtens
n:Axtens;Bruce
org:The Protium Project
adr:;;;;;;Australia
email;internet:bruce.axtens@xxxxxxxxx
title:Software Engineer
x-mozilla-html:FALSE
url:http://www.protiumblue.com
version:2.1
end:vcard



Relevant Pages

  • Re: CComPtr strange behaviour
    ... GetUIObjectOf takes an IUnknown* pointer it got from ... and tells the caller that it's really a pointer ...
    (microsoft.public.vc.atl)
  • Re: Testing Delphi DLL crashes VB6 IDE
    ... that s goes out of scope before the function returns, and thus the pointer is not valid by the time it arrives at the caller. ... But since the address that s held is probably still within your process's address space, and COM probably hasn't re-allocated it to anything else, it probably still contains the same characters it did a moment ago when it was still a valid pointer, so the caller will blithely read it and get what you were expecting, so it appears to work. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Should function argument be changed in function body?
    ... involved but even with something as simple as this it can make the caller ... handle an extra type that isn't natural to the problem. ... > You should try to avoid passing a non const pointer to a function unless ... It is probably more common for this reason for the status to be the return ...
    (comp.lang.c)
  • Allocating and returning new class in function
    ... from class method or operator). ... This would require caller to ... I would instantiate it dynamically and return pointer. ... should not rely on content of stack that is released when function is done. ...
    (comp.lang.cpp)
  • Re: [PATCH][RFC] Extend "memparse" to allow a NULL return pointer value.
    ... second parameter value if he has no interest in that returned value. ... stored in this pointer serves very important ... Caller must do this. ... If it returns `0' (zero), ...
    (Linux-Kernel)