Access violation and stack overflow in DLL (XE5)

Is there some special XE5 setting needed for calling functions in a
DLL in the same way as was done in BDS2006?

I have migrated an application from BDS2006 to XE5. It has been worked
on since 2001 or thereabouts. It is a number crunching program doing
tomography evaluation. A lot of the math was developed by the original
author using FORTRAN and placed in 3 DLL files. The functions
implemented there are called from within the Delphi program.

Now I have been given a problem case where the processing is aborted
at the start with either a stack overflow or an access violation
exception. In the latter case the exception error message points to
the DLL1.dll file.

"Access violation at address 00ACD057 in module 'dll1.dll'. Read of
address 00090000"

If the data files are loaded into the non-migrated version of the
program (BDS2006 version) then there is no exception....
Same DLL files from a long time ago are still used because we do not
have the ability any longer to modify these, no compiler and no
FORTRAN knowledge either. So we are stuck with them for the time
being.

So I wonder if there is some project setting that could give me a
different interface to the DLL:s, for instance regarding memory
availability etc?


---
Bo Berglund
Sweden & Texas
Newsreader: Forte Free Agent 1.92/32.572
0
Bo
8/11/2015 10:39:01 PM
embarcadero.delphi.ide 3925 articles. 0 followers. Follow

11 Replies
494 Views

Similar Articles

[PageSpeed] 37

Bo Berglund wrote:

> Is there some special XE5 setting needed for calling functions in a
> DLL in the same way as was done in BDS2006?

If you pass any parameters of type PChar to the DLL you need to change
the delarations of the DLL functions in your Delphi code to use
PAnsichar instead, and also modify the calls to pass PAnsichars. Note
that you cannot simply cast a String to PAnsichar, you have to convert
it to Ansistring first.

This is all due to the Ansi -> Unicode migration that happened with
D2009 and is one case where the compiler cannot warn you that the
existing code may have problems.



-- 
Peter Below (TeamB)
0
Peter
8/12/2015 8:46:27 AM
On Wed, 12 Aug 2015 01:46:27 -0700, Peter Below <none@address.invalid>
wrote:

>Bo Berglund wrote:
>
>> Is there some special XE5 setting needed for calling functions in a
>> DLL in the same way as was done in BDS2006?
>
>If you pass any parameters of type PChar to the DLL you need to change
>the delarations of the DLL functions in your Delphi code to use
>PAnsichar instead, and also modify the calls to pass PAnsichars. Note
>that you cannot simply cast a String to PAnsichar, you have to convert
>it to Ansistring first.
>
>This is all due to the Ansi -> Unicode migration that happened with
>D2009 and is one case where the compiler cannot warn you that the
>existing code may have problems.

Yes, I know about that and I have checked the different function calls
into the 3 DLL:s and they all pass simple variables like integer,
double, pointer to arrays of double etc. So far I have seen no string
or char type of any kind. It is purely math involved.

I have today gone over older zipped versions of the exe and checked
how this particular dataset was processed using these. Then I found
that there are builds with XE5 that do work (up to build 635) (but
have other issues that have later been solved) and then the error
appears (from build 636)....

Right now I have the time border between exe:s working (2015-01-24)
and non-working (2015-01-27). The sources committed around these two
times are only differing regarding "nonrelated stuff" like handling of
language depentent strings for the GUI as far as I can tell.
But the commit intervals at the time was not that good so I cannot see
all the changes in source that were part of the change in behaviour
between b635 and b636.

IDE source changes:
Inside the subfolder source\__history I see a lot of files named like
ERTGraphics.pas.~5~ etc and these seem to be file states saved by XE5
while I was coding.

Question:
At what times does XE5 save these files?

I have looked at these and found three that changed between the two
dates, but in all cases the file differences were only these:
- All literal GUI strings were replaced with calls to the language
handler to retrieve the string valid for the active language.
- Several beautifying edits like "if something then begin" broken down
such that "begin" is on the next line

I find nothing but these and they cannot be involved in the problem
since the language calls have been added over several months before
and after, not only here...

Sigh, if only I could see what is in fact generating the exception.
Any ideas on how to do this?
Here is where I get by stepping through the code:
{code}
procedure StartForwardModel(const ResOrIP, GetJacobian: integer);
var
  ...
begin
  ...
  ForwardFE(@CallBackForw, Pointer(gfNodeX)^, Pointer(dNodeYa)^,
  Pointer(gfCond)^, Pointer(ForwSoln)^, Pointer(gfJacobian)^,
  Pointer(gnElecNodeID)^, Pointer(gnStingCMD)^, Pointer(gnParamX1)^,
  Pointer(gnParamX2)^, Pointer(gnParamY1)^, Pointer(gnParamY2)^,
  Pointer(gnInfElectrodes)^, Pointer(CenterNodeX)^,
  Pointer(CenterNodeY)^, Pointer(ElemArea)^, GetJacobian);
  ..
{code}
When I execute the ForwardFE call there is a stack overflöow error
message.

ForwardFE is a DLL function declared such:
{code}
interface
  procedure ForwardFE(CallBackForw: Pointer;
      var dllNodeX, dllNodeY, dllConductivity, dllVIcalc,
          dllJacobian, dllElecNodeID, dllStingCMD,
          dllParamX1, dllParamX2, dllParamY1, dllParamY2,
          dllInfElec, dllCenterNodeX, dllCenterNodeY,
          dllElemArea;
    const dllGetJacobian: integer); cdecl;
implementation
  procedure ForwardFE; external 'dll1.dll' name 'forwardfe';
{code}
I am rather confused by the declaration of the arguments as var but no
data type, is that customary?

But all of these are arrays of double or integer.

---
Bo Berglund
Sweden & Texas
Newsreader: Forte Free Agent 1.92/32.572
0
Bo
8/12/2015 1:27:00 PM
Bo Berglund wrote:

> Question:
> At what times does XE5 save these files?

Delphi creates these files every time you save the file - this is the
history what you've changed. Can be handy when you accidently delete
some part of the file.
0
Lajos
8/12/2015 2:44:51 PM
On Wed, 12 Aug 2015 07:44:51 -0700, Lajos Juhasz
<juhasz.lajos@gmail.com> wrote:

>Bo Berglund wrote:
>
>> Question:
>> At what times does XE5 save these files?
>
>Delphi creates these files every time you save the file - this is the
>history what you've changed. Can be handy when you accidently delete
>some part of the file.
Thanks, it means that it will save on every compile too (only changed
files of course). Good to know.


---
Bo Berglund
Sweden & Texas
Newsreader: Forte Free Agent 1.92/32.572
0
Bo
8/12/2015 3:09:38 PM
Bo wrote:

> Is there some special XE5 setting needed for calling functions
> in a DLL in the same way as was done in BDS2006?

No, because nothing has changed in this regard.  A function call is a function 
call, whether the function is in a DLL or not.  You have to be doing something 
wrong in the code that is calling the DLL functions.  Either the DLL functions 
are declared wrong at compile-time, or you are passing the wrong parameter 
values at run-time.  Please show the actual DLL function signatures (from 
the original .h file or documentation) and your relevant Delphi code that 
is declaring and calling the functions.

-- 
Remy Lebeau (TeamB)
0
Remy
8/12/2015 4:55:17 PM
On Tue, 11 Aug 2015 15:39:01 -0700, Bo Berglund
<bo.berglund@nospam.com> wrote:

>I have migrated an application from BDS2006 to XE5. It has been worked
>on since 2001 or thereabouts.

It turned out that the migration from BDS2006 to XE5 was behind the
problem.
Since XE5 was not able to upgrade the project file I created a new
Delphi project in XE5 and imported all the source files etc.
However, the linker setting for stack size was not something I thought
about and here is the problem when the default values were used.
The default max stack size is 1Mb but the original project had a max
size of 16 Mb and a min of 1 Mb....

When I changed these values to the ones used by BDS2006 the error
disappeared..

Strangely this should have affected all builds done with XE5 but it
did not, the change was happeniong at the end of January.

How exactly can one find out the most suitable setting for stack size?


---
Bo Berglund
Sweden & Texas
Newsreader: Forte Free Agent 1.92/32.572
0
Bo
8/12/2015 5:06:13 PM
On Wed, 12 Aug 2015 10:06:13 -0700, Bo Berglund
<bo.berglund@nospam.com> wrote:

>On Tue, 11 Aug 2015 15:39:01 -0700, Bo Berglund
><bo.berglund@nospam.com> wrote:
>
>>I have migrated an application from BDS2006 to XE5. It has been worked
>>on since 2001 or thereabouts.
>
>It turned out that the migration from BDS2006 to XE5 was behind the
>problem.
>Since XE5 was not able to upgrade the project file I created a new
>Delphi project in XE5 and imported all the source files etc.

And it actually seems to be that I used the migrated project file up
until the end of January at which time I decided that enough is enough
and followed Remy's advice and created a new project from scratch.
This new project improved a lot of visual things, but since I did not
know about the stack size setting the problems when using large data
sets went unnoticed.

I have verified via CVS that the project file did have the proper
stack settings until the end of January but nothing about stack
afterwards. So XE5 used the very low default setting.

Case closed.

---
Bo Berglund
Sweden & Texas
Newsreader: Forte Free Agent 1.92/32.572
0
Bo
8/12/2015 5:59:09 PM
Bo Berglund wrote:

> On Tue, 11 Aug 2015 15:39:01 -0700, Bo Berglund
> <bo.berglund@nospam.com> wrote:
> 
> > I have migrated an application from BDS2006 to XE5. It has been
> > worked on since 2001 or thereabouts.
> 
> It turned out that the migration from BDS2006 to XE5 was behind the
> problem.
> Since XE5 was not able to upgrade the project file I created a new
> Delphi project in XE5 and imported all the source files etc.
> However, the linker setting for stack size was not something I thought
> about and here is the problem when the default values were used.
> The default max stack size is 1Mb but the original project had a max
> size of 16 Mb and a min of 1 Mb....
> 
> When I changed these values to the ones used by BDS2006 the error
> disappeared..
> 
> Strangely this should have affected all builds done with XE5 but it
> did not, the change was happeniong at the end of January.
> 
> How exactly can one find out the most suitable setting for stack size?

Only by analyzing each and every possible execution path in the
program, which is completely impractical in most cases. Your DLL
probably uses large stack-based variables, like static array types, or
algorithm that involve deep recursion. Those are the main eaters of
stack space. In most Delphi applications 1 MB stack is generous, since
most "large" data types used are reference types, so the stack
variables only store pointers.

-- 
Peter Below (TeamB)
0
Peter
8/13/2015 9:19:39 AM
On Thu, 13 Aug 2015 02:19:39 -0700, Peter Below <none@address.invalid>
wrote:

>> How exactly can one find out the most suitable setting for stack size?
>
>Only by analyzing each and every possible execution path in the
>program, which is completely impractical in most cases. Your DLL
>probably uses large stack-based variables, like static array types, or
>algorithm that involve deep recursion. Those are the main eaters of
>stack space. In most Delphi applications 1 MB stack is generous, since
>most "large" data types used are reference types, so the stack
>variables only store pointers.

Well, I think most of the variables used are dynamic double arrays
passed by reference and maintained memory-wise by Delphi. I hope
dynamic arrays do not use the stack?

Here is an example call to a DLL function:
{code}
ForwardFE(@CallBackForw, Pointer(gfNodeX)^, Pointer(dNodeYa)^,
Pointer(gfCond)^,
    Pointer(ForwSoln)^, Pointer(gfJacobian)^, Pointer(gnElecNodeID)^,
    Pointer(gnStingCMD)^, Pointer(gnParamX1)^, Pointer(gnParamX2)^,
    Pointer(gnParamY1)^, Pointer(gnParamY2)^,
Pointer(gnInfElectrodes)^,        
    Pointer(CenterNodeX)^, Pointer(CenterNodeY)^, Pointer(ElemArea)^,
    GetJacobian);
{code}
As you can see the call contains pointers to the data. The GetJacobian
is an integer variable.


---
Bo Berglund
Sweden & Texas
Newsreader: Forte Free Agent 1.92/32.572
0
Bo
8/13/2015 11:00:30 AM
Bo Berglund wrote:

> > Your DLL probably uses large stack-based variables, like static
> > array types, or algorithm that involve deep recursion.

> Here is an example call to a DLL function:
....
> As you can see the call contains pointers to the data.

Yes, but internally dll function(s) (dll uses the same stack as exe)
can store big temporary objects on stack.

As you mentioned dlls are wtitten on FORTRAN. Many years ago I used
FORTRAN but (as I understand now) I never thought about vars placement
at that time. :-)

Deep recursion also can arise suddenly. Some months ago I got stack
overflow in flood fill algoritm during processing of large image.

--
Alex
0
Alex
8/14/2015 4:04:54 AM
Bo Berglund wrote:

> > Only by analyzing each and every possible execution path in the
> > program, which is completely impractical in most cases. Your DLL
> > probably uses large stack-based variables, like static array types,
> > or algorithm that involve deep recursion. Those are the main eaters
> > of stack space. In most Delphi applications 1 MB stack is generous,
> > since most "large" data types used are reference types, so the stack
> > variables only store pointers.
> 
> Well, I think most of the variables used are dynamic double arrays
> passed by reference and maintained memory-wise by Delphi. I hope
> dynamic arrays do not use the stack?

No, they are reference types. But as Alex said in his reply you have no
control over how the DLL *uses* the data you hand it. That is probably
the problem part, as the DLL uses the stack of the host application
thread that makes the DLL call.


-- 
Peter Below (TeamB)
0
Peter
8/15/2015 9:56:01 AM
Reply:

Similar Artilces:

Delphi BDE
I (still) support two well established Delphi apps requiring BDE (Paradox tables). Suddenly they have started generating a consistent Access Violation (in Win XP) when starting to run in the IDE. The apps compile OK and the resulting exes run without complaint outside the IDE.It's not just my apps - the demo Fishfact and other demos generate an access violation as well as they start to run (after compiling OK). I have made no changes for a long time to the VCL etc. This happens in Delphi 4 and Delphi 7. I have tried re-installing Delphi and BDE and doing a system restore. The access vio...

Delphi XE IDE keeps crashing with Stack overflow message
I'm using Delphi XE on Windows 7. I have been using it successfully for over a year, first on Windows XP, then about six months ago moving to Win 7. In the past couple of weeks I've been getting this issue where I get a message window pop up in the IDE, saying "Stack overflow - save your work and restart Delphi XE". Shortly afterwards the IDE crashes. This is now happening almost every time I use it. There doesn't seem to be any rhyme nor reason behind when this will happen, and I'm normally not doing anything other than browsing code. Any ideas? We are...

XE5 DEBUG QUESTION
This is a C++ XE5 DEBUG QUESTION I have an application. My customer is getting the following error. Access violation at address XXXX in module USER32.dll Read of address XXXX. I cant reproduce the problem on my end within the debugger. Assuming this isn't a stack overflow ... Is there some way to reverse engineer the line of code by looking at a map file or something? When I google USER32.dll I get back suggestions that it might be a call to MessageBox. I'm an old timer. A long time ago in a land far away I used a DOS extender with BC 5.01. When youd geta GOF yo...

access violation in module vcl100.bpl when starting Delphi 2007 IDE
Hi, I'm using Delphi 2007 in a VMware virtual machine. The guest operating system is Windows 7 32-bit. This has worked fine for months but since yesterday, I keep getting this error message when starting the IDE: Access violation at address 201302BC in module 'vcl100.bpl'. Read of address 00000060. The call stack shows: Menu.TMenuItem.SetShortCut ExtCtrls.TTimer.Timer ExtCtrls.TTimer.WndProc Classes.StdWndProc DispatchMessageA Forms.TApplication.ProcessMessage From.TApplication.HandleMessage Forms.TApplication.Run BaseThreadInitThunk FYI, the following com...

Delphi XE Access violation in module 'dbxfb.dll'
Hi to all, I'm testing dbExpress and some times I get an access violation with this code: {code} function TForm1.GetBlobText(id: Integer): String; begin Result := ''; if (id > 0) then try if not SQLConnection1.Connected then SQLConnection1.Connected := True; SQLQuery1.Close; SQLQuery1.Sql.Text := Format('SELECT id, somefield, txt_field FROM my_table WHERE id = %d', [id]); SQLQuery1.Open; Result := SQLQuery1.FieldByName('txt_field').AsString; except ...

ShowModal for Android Delphi XE5 Update 2, got access violation
Hello everyone, I've done my iOS app, try to compile to android system. But I found the showmodal doesn't work that way, I understand that, Android does different with iOS, then I wrote a testing app with existing demo source "ScrollableFormDemo" I added a second form in that probject called TForm1 and A Button in the main form. procedure TFormMain.Button1Click(Sender: TObject); begin Form1 := TForm1.Create(nil); Form1.ShowModal( procedure(ModalResult: TModalResult) begin // if OK was pressed and an item is selected, pick it Form1.Disp...

Access violation rtl140.bpl Error, Delphi 2010 IDE wont load
I get this error message below when I try to start Delphi 2010. While it's loading up this message pops up and the only thing that loads is the Tool Palette and Structure windows. How can fix this? I've tried repairing and reinstalling but the problem persists. Access violation at address 500115A7 in module 'rtl140.bpl'. Read of address 00000000. [500115A7]{rtl140.bpl } System.UTF8ToUnicodeString (Line 22482, "System.pas" + 2) + $0 [5029A838]{vcl140.bpl } Controls.TWinControl.DefaultHandler (Line 9860, "Controls.pas" + 30) + $17 [502C18E6]{vcl140.b...

Delphi XE Access violation in module 'dbxfb.dll' #2
Hi to all. I'm forced to migrate a Delphi 2007 application to Delphi XE using DbExpress and the dbxfb.dll driver. I had to make a trick for loading Blob fields into a TRichEdit. I got enforced to write this code: {code} procedure TForm1.LoadMemo(id: Integer); var I: Integer; strText: AnsiString; myStream: TMemoryStream; begin SQLQuery1.Close; SQLQuery1.Sql.Text := 'SELECT id, txt_field FROM my_table WHERE id = ' + IntToStr(id); SQLQuery1.Open; strText := SQLQuery1.FieldByName('txt_field').AsString; myStream := TMemoryStream...

Debugging DLL compiled with Delphi 7 in Delphi XE5
Dear Sirs/Madams, We are considering changing from Delphi 7 to Delphi XE5. Our project consists of a dll and an .exe file. I have begun converting the .exe file to Delphi XE5 and have successfully accessed the DLL compiled with Delphi 7. When I debug the DLL (using the "run parameters" and changing "host application" to the .exe file) in Delphi 7 I can set breakpoints etc. and they are triggered. However, when I attempt to debug the project in XE5, I get the following message: Module Load: xxx.dll. No Debug Info. Base Address: $015A0000. Process xxx.exe (2928) ...

Stack overflow when opening a Delphi 7 frame in Delphi XE4
I am trying to port a Delphi 7 application to Delphi XE4. I have installed all the required component libraries in Delphi XE4 and I managed to open the Delphi 7 project in Delphi XE4. However, when I try to open one of the units containing a VCL frame I get the error "stack overflow - Save your work and restart RAD studio XE4". If I don't open any frames and try to build the project, the compiler can't find the components in the above mentioned frame unit and stops with an error. What should I do? Pier I figured out what the problem was. I has a TsiLang compo...

msado15.dll,ntdll.dll Access Violation in XP (sp3)
Hi every one.. Im try something.. I coded a little exe on win 7 64 bit with XE2... one form one adoquery one adoconnection one datasource one dbgrid my query has got one parameters... I create parameters by code. my code block: adoquery1.Close; adoquery1.SQL.Clear; adoquery1.Parameters.Clear; adoquery1.Parameters.CreateParameter('pr_param1',ftinteger,pdInput,0,0); showmessage(adoquery1.Parameters[0].Name); adoquery1.SQL.ADD('SELECT (SELECT table1.field1 FROM table1 WHERE table1.OPKODU=table2.OPERASYONKODU) AS OPISIM,* FROM table2 WHERE INCKEYNO=:pr_param1)...

Stack Overflow from IDE only
Hi, I just migrated a program from D6 to D2009. Whenever I start the program from the IDE it stops ins ntdll.DbgBreakPoint. The call stack shows the following: :774c8b2f ntdll.DbgBreakPoint + 0x1 :77529bd8 ; ntdll.dll :77529c3b ntdll.RtlUnhandledExceptionFilter + 0x12 :775158a2 ; ntdll.dll :77489a3a ; ntdll.dll :774e5f79 ntdll.RtlRaiseStatus + 0xb4 :774e5f4b ntdll.RtlRaiseStatus + 0x86 :774e5dd7 ntdll.KiUserExceptionDispatcher + 0xf :774c1808 ntdll.LdrInitializeThunk + 0x10 The even log: Module Load: gdiplus.dll. No Debug Info. Base Address: $73FB0000. Process ldw.exe (4684) ...

How to Track Access Violation in a DLL
I am using CB2009 and CB6.0. I am looking for guidance on how to setup my program and dlls so I can track an Access Violations in a dll. Right now I do not know any good way to do this. Can anyone recommend an Embarcadero article or provide personal advice on the best way to setup my EXE and DLLs for tracking Access Violations in a dll. Thanks Patrick M. <patrick mikula> wrote in message news:164379@forums.codegear.com... > I am looking for guidance on how to setup my program and dlls so I can > track an Access Violations in a dll. Right now I do not know any good ...

Access violation in pbdwe90.dll
Hello, we have got an application server interface written with PBNI. When the application server load rises, the Access violation exception in PBDWE90.DLL module occurs. It seems to be a problem in DW_TextLine symbol. The part of the callstack is bellow: 719f1c50() pbdwe90.dll!DW_TextLine() + 0x17d8 bytes [Frames below may be incorrect and/or missing, no symbols loaded for pbdwe90.dll] pbdwe90.dll!DW_TextLine() + 0x1e23 bytes Access violation occurs always on the same memory address (0x719fcc50). Powerbuilder version: 9.0.2 Build 7610 Operating system: Windows 2...

Web resources about - Access violation and stack overflow in DLL (XE5) - embarcadero.delphi.ide

CP violation - Wikipedia, the free encyclopedia
In particle physics , CP violation is a violation of the postulated CP-symmetry : the combination of C-symmetry ( charge conjugation symmetry) ...

James-O'Keefe-Parole-Violations - Flickr - Photo Sharing!
James O'Keefe, liar and scam artist, has been spotted in NYC in violation of his parole in Lousiana.

Civil Rights Violation on Video Pledge and Invocation - YouTube
Civil Rights Violation on Video Pledge and Invocation - YouTube

UAE Police issue more than 25,000 traffic violations involving lorries in Q1 of 2014 - The National
Police said several people have been killed in accidents involving lorries so far this year.

Russia closes 12 McDonald's restaurants over hygiene violations
Russia has temporarily closed 12 branches of McDonald's on health grounds and is carrying out more than 100 inspections, the US fast food chain ...

CFMEU implicated in threats against Melbourne contractor and site entry violations
Victoria's powerful construction union allegedly told a company it is on a &quot;hit list&quot; and would suffer industrial strife as payback ...

Report alleges labour violations at Apple supplier in China
Report claims dangerous conditions, labour violations at another Apple plant.

Hints of Eurovision vote violation against Russia
The head of Azerbaijan's state broadcaster has questioned the vote tally in the Eurovision Song Contest where it embarrassingly gave "nul points" ...

Nick Kyrgios issued code violation at Japan Open - The Courier-Mail Search Search
NICK Kyrgios has hit out at the Australian media following his loss to Frenchman Benoit Paire at the Japan Open on the weekend.

Nick Kyrgios issued code violation at Japan Open
NICK Kyrgios has hit out at the Australian media following his loss to Frenchman Benoit Paire at the Japan Open on the weekend.

Resources last updated: 1/19/2016 5:07:58 PM