Hello,
I have obviously some problems with referencing and need help:
I have written an XML parser using the iDocument Interface. It implements the method ReadFile( registry, filename) which return a csRef<iDocument>. It opens a file, queries the iDocumentSystem, creates a csRef<iDocument> via CreateDocument(), opens an iFile and use the Parse() method.
To add more convinience, I implemented the ReadFile method twice. One version with the const char* filename and one with an iFile* as parameter.
First one just opens a new iFile and return the result of the other one.
Schematically it looks like:
csRef<iDocument> ReadFile(const char* filename);
csRef<iDocument> ReadFile(iFile *file);
csRef<iDocument> ReadFile(const char* filename)
{
// open file
...
return ReadFile(file);
}
csRef<iDocument> ReadFile(iFile*)
{
// get doc system
...
csRef<iDocument> document = docsys->CreateDocuemnt();
// do parsing and some other checks
...
return document;
}
Within the ReadFile Method, I can access all nodes and so on. Everything is looking fine. But when I return to the main program, it gets lost.
// gdb shows for 'p xmldoc':
// {<CS::Memory::CustomAllocated> = {<No data fields>}, obj = 0x6}
// Note: obj is not 0, probably some garbage on the stack
// then I read the file:
csRef<iDocument> xmldoc ( parser->ReadFile( GetObjectRegistry(), "actor/animtest.xml" ));
// here, gdb still shows for 'p xmldoc':
// {<CS::Memory::CustomAllocated> = {<No data fields>}, obj = 0x6}
// obj has not changed and is still the garbage
// so this check fails and xmldoc is considered as valid
if ( !xmldoc )
{
printf("File Parsing not successfull\n");
}
// Not really suprising, but here gdb still shows, that obj=0x6
csRef<iDocumentNode> root = xmldoc->GetRoot();
// Boom! I get a segmentation fault
Within ReadFile, gdb shows a valid looking obj, and since I can access the nodes, I consider this as correct.
When I open the file in the main program via calling the other ReadFile(iFile*) version, it is working! So the two pass return seems to dump the reference.
I also increased the reference count manually, to test if this might be a problem, but I get the same result.
Since I work with recursions, I need to be sure, that references can be returned through two or more recursions.
I hope I just handle csRef wrong and some casting and allocating or something els might fix it. But for the moment I have no idea
So I probably handle them wrong.
Thanks in advance.