Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Don't return literal strings from functions!

Author
Message
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 21st Nov 2010 19:24 Edited at: 21st Nov 2010 19:27
I have a piece of code where reading a local integer causes my DBP app to crash. There is no obvious logic to the problem. I've pasted in the function at the end in whole (with the important bits marked with <== - it's hard going!), but the crux of it is around the variable myScore. Originally I just used totalScore, but I moved it to the former in case I had some bizarre corruption; it hasn't helped. There's a few quirks to the code that are the result of pinpointing the problem, such as printing out the same message multiple times to a log file.

In brief:

* totalScore is calculated.
* It is successfully displayed as a bitmap font.
* It is successfully printed to a log file and console. (IanM's LOG and CONSOLE commands)
* I move the value to another local variable, myScore (to debug)
* I do a few things in between to allow keyboard input of the player name <== THIS IS WHAT TRIPS IT, MORE LATER
* I print the value to the log file and console, this is where it crashes (usually but not always).
* If I remove the print to log file and console (or it gets past the log), it crashes at the next use of myScore, at the ENDFUNCTION command.

the Score is never used or updated in the keyboard input code. The addition of this code trips a totally unrelated variable. If I remove the call to the keyboard code, it works again.

I haven't included the keyboard code as it expands out into a few thousand lines of my standard routines. Basically, it creates a set of array elements (I have proven this is not the cause) and then uses these to create 39 images and put them on-screen on plains. Afterwards, they get deleted and the program continues and subsequently crashes.

Thanks in advance for any insight you can give. I think I've hit a problem with the inner workings of DBP given that cause and effect are unrelated. I'm expecting an "Yes, I hit that" response, hopefully with a workaround.



BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 21st Nov 2010 19:51
<Mail Back>

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 21st Nov 2010 21:00
My gut feel is that you've got some sort of stack corruption going on.

- In case you've been compiling FPSC, ensure RemoveSafetyCode is No, and SafeArrays is Yes.
- Have you tried eliminating the use of plug-ins one by one?

Beyond that, I haven't got too much to suggest, beyond hacking out sections of code until the problem disappears, then working from there.

Utility plug-ins (26-JUL-2010)
I'm applying terms of use that require you to wear a red nose and honk a horn whenever you use the Internet
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 22nd Nov 2010 09:42
Thanks Ian. Are there any tools that might help me work out what's causing this?

* I removed Image Kit, problem still exists.

* Game is based on Box 2D, impossible to remove.

* I have a significant amount of your plugins, too many to remove. They have never given me problems anywhere else. I managed to remove Matrix1 String manipulation, and Find Free X commands.

Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 22nd Nov 2010 11:26
What happens if you remove myTotal and use a static integer value? Will it still crash?

"Only the educated are free" ~Epictetus
"Imagination is more important than knowledge..." ~Einstein
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 22nd Nov 2010 11:47
Interesting thought, I just tried it. It still crashes when I:

ENDFUNCTION 1

The code that calls this is...

iScore = gameScore()
debugPrint("Score Complete")


And the debug output never gets reached. An EXIT PROMPT doesn't help either as this is a Windows-captured crash, not an Application-captured crash.

The XML crash log suggests ntdll.dll is at fault, so that removes DirectX from the equation...


Quote: "<ProblemSignatures>
<EventType>APPCRASH</EventType>
<Parameter0>Application.exe</Parameter0>
<Parameter1>1.0.0.0</Parameter1>
<Parameter2>4bd9fcf9</Parameter2>
<Parameter3>ntdll.dll</Parameter3>
<Parameter4>6.1.7600.16559</Parameter4>
<Parameter5>4ba9b21e</Parameter5>
<Parameter6>c0000005</Parameter6>
<Parameter7>000520cb</Parameter7>
</ProblemSignatures>
"


I also get this churned out...



and a 160 Megabyte dump file!

BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 22nd Nov 2010 12:06 Edited at: 22nd Nov 2010 12:14
[EDIT] Reposted now WinDbg correctly set up

Downloaded WinDbg to get into the dump file.

Quote: "
Microsoft (R) Windows Debugger Version 6.12.0002.633 X86
Copyright (c) Microsoft Corporation. All rights reserved.


Loading Dump File [J:\Users\Steve\AppData\Local\Temp\Debug 1.mdmp]
User Mini Dump File: Only registers, stack and portions of memory are available

Symbol search path is: http://msdl.microsoft.com/download/symbols
Executable search path is: M:\TGC\Projects\smartNetbook\smartVB\smartBalls
Windows 7 Version 7600 MP (2 procs) Free x86 compatible
Product: WinNt, suite: SingleUserTS Personal
Machine Name:
Debug session time: Mon Nov 22 10:57:18.000 2010 (UTC + 0:00)
System Uptime: 6 days 13:57:51.307
Process Uptime: 0 days 0:00:51.000
................................................................
............................
Loading unloaded module list
......
This dump file has an exception of interest stored in it.
The stored exception information can be accessed via .ecxr.
(11bc.1184): Access violation - code c0000005 (first/second chance not available)
eax=10006810 ebx=ee6b6811 ecx=fffffff5 edx=0000ffff esi=0280a258 edi=ee6b0000
eip=773320cb esp=00a5ed20 ebp=00a5ed54 iopl=0 nv up ei ng nz na pe nc
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00210286
ntdll!RtlpLowFragHeapFree+0xc5:
773320cb 8930 mov dword ptr [eax],esi ds:0023:10006810=51ec8b55
"


BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 22nd Nov 2010 15:08 Edited at: 22nd Nov 2010 15:16
* Local variable totalScore removed, and made part of a global Typed variable (gScore.totalScore)

* All local variables in this function declared as local:
. local n as integer
. local collector as integer
. local juggler as integer
. local col as integer

I wondered if the problem was triggered when the function ends. But I can make it crash by doing my debugPrint() on variable gScore.totalScore, so this is not the case either.

I reviewed the call to debugPrint (which doesn't do much, code is below). I found that when I try to debugPrint any variable it crashes. I made some more tests...

NO CRASH
x$ = str$(m)
debugPrint(str$(m))
x$ = "Now " + "Now"


CRASH

x$ = "Now " + str$(m)
debugPrint("Now " + str$(m))


So I have 2 scenarios that crash it:

1. Concatenating a literal string and a str$()
2. Exiting my function, with or without a return parameter.

Help!



DVader
22
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 22nd Nov 2010 15:52
Out of curiosity, if you change it from a function to a gosub does it still crash on return?

http://s6.bitefight.org/c.php?uid=103081
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 22nd Nov 2010 17:42
DVader, I've just used that idea to test more. I took the whole function and put it into the code where it is called - no function call now.

The code now gets much further, and starts to deinitialise the game, deleting all the images and plains. maybe 300-400 executed lines of code later, it dies in just the same way.

so frustrating!

BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 22nd Nov 2010 18:26 Edited at: 22nd Nov 2010 18:33
Totally baffled.

It is now definite that calling FontShowKeyboard() causes the crash. Without it, the game works forever (or as close to forever as I can keep playing for )

So here it is, the function that kills it - as you can see I have removed all meaningful code, it simply returns a string and it still crashes...



Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 22nd Nov 2010 18:29
Is there an error that is generated when the program crashes or does everything just stop and lock up? Is the console app you are using a call to a Windows console? I think the console tries to use ANSI character sets by default from the commandline but may be programmed to use UNICODE. Just throwing out crazy ideas here but maybe inkey$() or whatever method you are using to capture keyboard input uses a character set that the console isn't expecting and stores the input in a pointer that is bleeding into your integer values. You mentioned that the error occurs when your keyboard input routines are present (but you said you have proven that they are not the cause...) and possibly with debugprint.

So maybe try eliminating any console use and see if it does anything.

Enjoy your day.
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 22nd Nov 2010 18:35 Edited at: 22nd Nov 2010 18:37
Thanks Latch, I'll try that. At the moment the keyboard function doesn't actually accept input, it returns "test". However, I will certainly remove the Console, it's just a switch in my parameter file to turn it off. In fact, I can't believe I haven't already tried it

Quote: "Is there an error that is generated when the program crashes or does everything just stop and lock up? "


It's a Windows 7 application crash, that creates a mini dump (mdmp). No feedback generated from DBP, I tried an EXIT PROMPT but it doesn't trigger.


Meetings to go to first, then I return!

Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 22nd Nov 2010 18:37 Edited at: 22nd Nov 2010 18:38
Instead of returning the string "test" directly, try assigning it to a string variable and then returning that. There have been bugs in dbpro related to that kind of thing.

Also make sure there are no #constants causing problems.

[b]
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 22nd Nov 2010 19:41
Yes, there is an existing bug that causes problems when you return a literal string from a function - you should always return a string variable instead.

This is a guess ... but I think it's related to the literal string being returned being freed back to the heap (where it technically didn't come from in the first place), allowing that memory to be reused for other purposes, such as for other strings, arrays, images, objects, sprites etc, but your program also continues to use it as a string.

Utility plug-ins (26-JUL-2010)
I'm applying terms of use that require you to wear a red nose and honk a horn whenever you use the Internet
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 22nd Nov 2010 21:43
Excellent! That was indeed the problem, I've never come across that issue before.

Thanks all for the help, especially IanM and Diggsey for finally nailing it.

Quote: "This is a guess ... but I think it's related to the literal string being returned being freed back to the heap (where it technically didn't come from in the first place)"


My earlier Googling brought up issues with releasing strings and corrupting pointers and data. It seems your deductions are spot on.

Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 23rd Nov 2010 16:32
Beat me to it, that was my next guess after seeing the function. DB seems really picky about what/how you return values in a function.

I've also noticed it seems to hate when you use "tab" inline with the #Constant command.

"Only the educated are free" ~Epictetus
"Imagination is more important than knowledge..." ~Einstein

Login to post a reply

Server time is: 2026-07-21 12:51:16
Your offset time is: 2026-07-21 12:51:16