www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Access Violation: Maybe it's my fault?

reply Federico"lox" Lucignano <Federico"lox"_member pathlink.com> writes:
Hi guys,
I was doing some experiments in D and while experimenting a wrote down this
little xml generator, but when i launch the resulting executable it gives
"Error: Access Violation" at the end of execution (it seems just after return 0
in main() function).

Still I don't know the language very well so I don't know if I'm doing something
wrong, can someone help me?

Thnx in advance.

/*
small XML test (compile with the -debug compiler option)

Author: Federico "Lox" Lucignano (federico alchimiedigitali.net)
Homepage: 
Started: Sat, 16 April 2005 12.37.45 GMT
Last Update: Sat, 16 April 2005 12.50.45 GMT
*/

debug import std.stdio;
debug import std.string;
debug import std.process;
debug import std.perf;
debug import std.file;

extern (C)
{
struct Attribute
{
char[] name;
char[] value;
Node* owner;

char[] toString()
{
return name ~ "=\"" ~ value ~ "\"";
}

void dispose()
{
owner = null;
delete name;
name = null;
delete value;
value = null;

return;
}
}

struct Node
{
Attribute*[] attributes;
Node*[] childNodes;
Node* parent;
char[] name;
char[] value;

uint depth()
{
Node* current = parent;
uint result = 0;

while(!(current is null))
{
result++;
current = current.parent; 
}

return result;
}

bit appendAttribute(Attribute* a)
{
try
{
a.owner = this;
attributes.length = attributes.length + 1;
attributes[length - 1] = a;
}
catch(Error e)
{
return false;
}

return true;
}

bit appendChild(Node* n)
{
try
{
n.parent = this;
childNodes.length = childNodes.length + 1;
childNodes[length - 1] = n;
}
catch(Error e)
{
return false;
}

return true;
}

char[] print()
{
char[] indentation;
char[] result;

for(uint x = 0; x < depth; x++)
{
indentation ~= "\t";
}

result ~= indentation;
result ~= "<" ~ name;

foreach(Attribute* a; attributes)
{
result ~= " ";
result ~= a.toString();
}

result ~= ">\n";

foreach(Node* n; childNodes)
{
result ~= n.print();
result ~= "\n";
}

if(value.length > 0)
result ~= indentation ~ "\t" ~ value ~ "\n";

result ~= indentation;
result ~= "</" ~ name ~ ">";

return result;
}

void dispose()
{
foreach(Attribute* a; attributes)
{
a.dispose();
delete a;
}

attributes.length = 0;
attributes = null;

foreach(Node* n; childNodes)
{
n.dispose();
delete n;
}

childNodes.length = 0;
childNodes = null;

parent = null;
delete name;
name = null;
delete value;
value = null;
return;
}
}
}

debug
{
int main(char[][] args)
{
uint nodeCount = 0;
uint attCount = 0;

writefln("Generating tree");

PerformanceCounter chrono = new PerformanceCounter();
chrono.start();

Node* n = new Node;
nodeCount++;

n.name = "root";

for(uint x = 0; x < 4; x++)
{
Attribute* a = new Attribute;
a.name = "att" ~ toString(x);
a.value = "val";
n.appendAttribute(a);
a = null;
attCount++;
}

for(uint x = 0; x < 100; x++)
{
Node* nn = new Node;
nn.name = "cNode";
nn.value = "nodeValue";

for(uint y = 0; y < 4; y++)
{
Attribute* a = new Attribute;
a.name = "att" ~ toString(y);
a.value = "val";
nn.appendAttribute(a);
a = null;
attCount++;
}

for(uint y = 0; y < 100; y++)
{
Node* nnn = new Node;
nnn.name = "ccNode";
nnn.value = "Prova prova prova prova prova prova prova prova prova prova prova
prova prova prova";

for(uint z = 0; z < 4; z++)
{
Attribute* a = new Attribute;
a.name = "att" ~ toString(z);
a.value = "val";
nnn.appendAttribute(a);
a = null;
attCount++;
}

nn.appendChild(nnn);
nnn = null;
nodeCount++;
}

n.appendChild(nn);
nn = null;
nodeCount++;
}

chrono.stop();

float nodesPerSec = (cast(float)nodeCount / chrono.microseconds) * 1000000.00;
float attsPerSec = (cast(float)attCount / chrono.microseconds) * 1000000.00;
float entitiesPerSec = (cast(float)nodeCount / chrono.microseconds) *
1000000.00;
float millisecs = cast(float)chrono.microseconds / 1000.00;
float kb = cast(float)n.print().length / 1024.00;
float kbs = (kb / chrono.microseconds) * 1000000.00;

writefln("\nGenerated:");
writefln("\n\t- %d nodes in %f milliseconds (%f nodes/s)", nodeCount, millisecs,
nodesPerSec);
writefln("\n\t- %d attributes in %f milliseconds (%f attributes/s)", attCount,
millisecs, attsPerSec);
writefln("\n\t- %d total entities in %f milliseconds (%f entities/s)", nodeCount
+ attCount, millisecs, nodesPerSec + attsPerSec);
writefln("\n\t- %f Kbytes in %f milliseconds (%f Kb/s)", kb, millisecs, kbs);
writefln("\nWriting result on disk.");
std.file.write("C:\\result.xml", n.print());

n.dispose();
delete n;

delete chrono;

system("PAUSE");
return 0;
}
}
Apr 16 2005
parent reply Manfred Nowak <svv1999 hotmail.com> writes:
Federico"lox" Lucignano <Federico"lox"_member pathlink.com> wrote:
[...]
 it gives "Error: Access Violation" at the end of execution (it
 seems just after return 0 in main() function).

Not confirmed. Runs fine on Win32-dmd 0.121. -manfred
Apr 16 2005
next sibling parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
I am getting constantly after exit:

Unhandled exception in xxx.exe(NTDLL.DLL): 0xC0000008: Invalid Handle.

while debugging in VS any D app with GUI.

Even in standard HelloWorld from D distribution.

Exception happens after return from WinMain:

7C90EB38   nop
7C90EB39   nop
7C90EB3A   nop
7C90EB3B   nop
7C90EB3C   nop
7C90EB3D   push        ebp
7C90EB3E   mov         ebp,esp
7C90EB40   sub         esp,50h
7C90EB43   mov         dword ptr [esp+0Ch],eax
7C90EB47   mov         eax,fs:[00000018]
7C90EB4D   mov         eax,dword ptr [eax+1A4h]
7C90EB53   mov         dword ptr [esp],eax
7C90EB56   mov         dword ptr [esp+4],0
7C90EB5E   mov         dword ptr [esp+8],0
7C90EB66   mov         dword ptr [esp+10h],0
7C90EB6E   push        esp
7C90EB6F   call        7C90EBAC
7C90EB74   mov         eax,dword ptr [esp]     <<<<<<<<<< exception is here 
<<<<<<<<<<<<<
7C90EB77   mov         esp,ebp
7C90EB79   pop         ebp
7C90EB7A   ret

7C90EB7B   nop  ---------- this function is involved too ------------
7C90EB7C   lea         esp,[esp]
7C90EB83   lea         ecx,[ecx]
7C90EB86   nop
7C90EB87   nop
7C90EB88   nop
7C90EB89   nop
7C90EB8A   nop
7C90EB8B   mov         edx,esp
7C90EB8D   sysenter
7C90EB8F   nop
7C90EB90   nop
7C90EB91   nop
7C90EB92   nop
7C90EB93   nop
7C90EB94   ret

Andrew.



"Manfred Nowak" <svv1999 hotmail.com> wrote in message 
news:d3rt62$adn$1 digitaldaemon.com...
 Federico"lox" Lucignano <Federico"lox"_member pathlink.com> wrote:
 [...]
 it gives "Error: Access Violation" at the end of execution (it
 seems just after return 0 in main() function).

Not confirmed. Runs fine on Win32-dmd 0.121. -manfred

Apr 17 2005
prev sibling parent Federico "lox" Lucignano <Federico_member pathlink.com> writes:
In article <d3rt62$adn$1 digitaldaemon.com>, Manfred Nowak says...
Federico"lox" Lucignano <Federico"lox"_member pathlink.com> wrote:
[...]
 it gives "Error: Access Violation" at the end of execution (it
 seems just after return 0 in main() function).

Not confirmed. Runs fine on Win32-dmd 0.121. -manfred

Ok, maybe it's linker's fault :-P Let's say I don't like OPTLINK inability to link against Platform SDK .libs, so I've found a Watcom Linker modified version for DMD that works great with any type of .libs, maybe the error is a consequence of this? "When Rome will Fall Then the World"
Apr 17 2005