Crystal Space
Welcome, Guest. Please login or register.
August 31, 2014, 07:50:39 am

Login with username, password and session length
Search:     Advanced search
9009 Posts in 2043 Topics by 8365 Members
Latest Member: Flypatty20
* Home Help Search Login Register
+  Crystal Space
|-+  Crystal Space Development
| |-+  Support
| | |-+  Problem with returning csRef
« previous next »
Pages: [1] Print
Author Topic: Problem with returning csRef  (Read 912 times)
Serran
Newbie
*
Posts: 10


View Profile Email
« on: June 17, 2011, 08:47:30 am »

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:
Code:
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.

Code:
// 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.
Logged
jorrit
Administrator
Hero Member
*****
Posts: 1706


View Profile
« Reply #1 on: June 17, 2011, 10:06:29 am »

Hmm, I think I need to see more source code to be able to tell you what is wrong. In particular the implementations of all those functions.

Greetings,
Logged
Serran
Newbie
*
Posts: 10


View Profile Email
« Reply #2 on: June 17, 2011, 10:53:17 am »

Okay, this is so embarassing,

I was just creating the code excerpt to write an answer and then I finally saw it!
The ReadFile(const char*) method haven't had a return statement at all.
It just ended with
Code:
csRef<iDocument> ReadFile(const char *filename)
{
 //...
 ReadFile(file);
}

So I guess it returned a void pointer, which caused the undefined behaviour. I wonder, why the compiler didn't warn me.

Since I haven't worked a lot with csRef, yet, I suspected the error to be caused by my reference handling, and not to be caused by a very basic programming issue.

Anyway, thanks for the quick answer and sorry for bothering!
Logged
Pages: [1] Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 4.233 seconds with 16 queries.