Inside Sabertooth
Learn how Sabertooth uses 3ds Max to create 3D interactive projects, including HBO Go’s Game of Thrones interactive experience
  • 1/3
You are here: Forum Home / Autodesk® FBX® / FBX SDK / Dynamic Linking Woes w/ Destructor
  RSS 2.0 ATOM  
2 pages: 1.2 last

Dynamic Linking Woes w/ Destructor
Rate this thread
 
50012
 
Permlink of this thread  
avatar
  • keres
  • Posted: 30 November 2010 01:03 PM
  • Total Posts: 20
  • Joined: 30 November 2010 08:56 PM

Hello guys and gals,

I am having some memory corruptions (causing a break) once my application tries to exit, and after several days of trying to debug this I am out of ideas. There is no code associated with this crash, as the entire destructor and every other one in every other object executes just fine. I have set my program up so this destructor is called last, and the program breaks on the final closing bracket of its definition.

Keep in mind, this does not happen in release configuration. (using fbxsdk_20113_1.lib and not fbxsdk_20113_1d.lib)

I believe this is because I am unintentionally mixing debug and release libraries. My linker is currently using /MDd, and I am using the dynamic libraries for sure. This is a list of all the libraries going into my linker.
Direct3D libraries are determined to be either debug or release in the little config manager that comes with its SDK, so those should definitely be debug libraries.

d3d9.lib
d3dx9d.lib
dinput8.lib
dxguid.lib
winmm.lib
wininet.lib
fbxsdk_20113_1d.lib

Any help is greatly appreciated! I’m so stressed because I have no clue why this is acting the way it is.

Thank you very much,
-Thomas



Replies: 1
/userdata/avatar/3yvwf23us_doug100x100.png

Try calling Destroy() on the importer, then the SDK manager before you process the shutdown for the other parts of your program.

Author: Doug Rogers

Replied: 30 November 2010 01:38 PM  
avatar
  • keres
  • Posted: 30 November 2010 02:08 PM

I do. Actually, I create and destroy everything that has to do with FBX SDK in the same function that is executed on start up (which imports a model and copies it to the DirectX buffers.) FBX SDK doesn’t have anything to do with the destructor that breaks the program, but I know that it is what is causing the problem because I wasn’t having this just before I did my very first integration test.



Replies: 1
/userdata/avatar/3yvwf23us_doug100x100.png

How about a test.  Just load the FBX file in, but do not copy into your D3D structures, then exit after import to see if it crashes then.

Author: Doug Rogers

Replied: 30 November 2010 02:13 PM  
avatar
  • keres
  • Posted: 30 November 2010 02:16 PM

The crashes still happened before I copied the data over to the D3D buffers. The minute I started working with FBX SDK in my project, these crashes started happening.

Remember, this only happens in debug builds. Release works absolutely fine, so I am almost certain it is a problem with my linking. I just don’t know what…



Replies: 2
/userdata/avatar/3yvwf23us_doug100x100.png

Can you try to link statically to see if that works?

Author: Doug Rogers

Replied: 30 November 2010 02:20 PM  
/userdata/avatar/3yvwf23us_doug100x100.png

Can you try to link statically to see if that works?

Author: Doug Rogers

Replied: 30 November 2010 02:27 PM  
avatar
  • keres
  • Posted: 30 November 2010 02:23 PM

I am about to try that. Honestly, I’m really wanting to link dynamically. The only reason why I am using FBX SDK is to provide a layer of convenience for artists I may work with.
Soon I will also be legally required to dynamically link to other libraries (such as Qt.) I’m a bit new to the whole linking thing, though I have a decent understanding of it. There have never been problems like what I am having now in my other projects.

Edit:

I just noticed something. What is the difference between fbxsdk_md2008.lib and fbxsdk_20113_1.lib? The latter comes with a DLL, but shouldn’t the first one mentioned because it is dynamic?



Replies: 1
/userdata/avatar/3yvwf23us_doug100x100.png

I have noticed that sometime I need to rebuild the whole project to get VS2010 to work properly.  It does not handle file dependencies as well as 2008 did.

One is for DLL and other is for static.

fbxsdk_md2008.lib should be the static linked version. There is some info in the FAQ, I think.

Author: Doug Rogers

Replied: 30 November 2010 02:28 PM  
avatar
  • keres
  • Posted: 30 November 2010 02:32 PM

Please read my edit in the post above ^^

I just tried staticly linking, and the results are the exact same. Just to clarify, I changed the linker input to fbxsdk_mt2008d.lib and the configuration to /MTd.

The break leads me to dbgdel.cpp, which google tells me is a cause of memory being allocated in deleted in separate stacks (causing corruption.) Why doesn’t this happen in release mode?



Replies: 2
/userdata/avatar/3yvwf23us_doug100x100.png

Release mode does not have the debug checks and sometimes just exits.  Look at the ViewScene example that has DLL and non DLL builds.

Author: Doug Rogers

Replied: 30 November 2010 02:37 PM  
/userdata/avatar/3yvwf23us_doug100x100.png

Release mode does not have the debug checks and sometimes just exits.  Look at the ViewScene example that has DLL and non DLL builds.

Author: Doug Rogers

Replied: 30 November 2010 02:39 PM  
avatar

You can get an error in dbgdel.cpp if you try to free memory twice. It’s something to check.



Replies: 1
/img/forum/dark/default_avatar.png

I have literally gone through every delete and resource removal, and I am 99% certain this is not the case (though it would understandably be if it was. Deleting twice is bad!)

Author: keres

Replied: 01 December 2010 01:57 AM  
avatar
  • keres
  • Posted: 30 November 2010 02:50 PM

I’m assuming that is happening, just not throwing the error…

Well the output is:

First-chance exception at 0x011331ea (msvcr90d.dll), 0xC0000005Access violation reading location 0xcdcdcdc1.
Unhandled exception at 0x011331ea (msvcr90d.dll), 0xC0000005Access violation reading location 0xcdcdcdc1.

If I put a break point at the end of my very last destructor (at the closing brace) then it reaches it. The minute I resume the program, it crashes.



Replies: 1
/userdata/avatar/3yvwf23us_doug100x100.png

0xcdcdcdcd are put in uninitialized data in debug mode (and freed memory is also set).

http://www.codeguru.com/cpp/w-p/win32/tutorials/article.php/c9535

Have you ever used shared_ptr in tr1?  It makes all the clean up issues go away.  I use it everywhere.

http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c15361

Author: Doug Rogers

Replied: 30 November 2010 02:58 PM  
avatar
  • keres
  • Posted: 01 December 2010 02:17 AM

Are you saying that using shared_ptr instead of regular pointers is better in every situation? This is quite strange as I am barely using pointers in my code, and I’m hopefully not being an idiot with it. For every new, I have a delete. Even then, I always test to see if the pointer is null before deleting it. At most I use a dozen or so new keywords for making objects, and all of them have corresponding deletes in the destructor of whatever holds it.



Replies: 1
/userdata/avatar/3yvwf23us_doug100x100.png

Yes. Allowing the compiler to automatically free memory is better in every situation that I have ever encountered and is being adopted as the way to handle memory in large scale application.  shared_ptr can be used for every memory allocation, since you can associate your own destructor code to it.

You can also get the debug message if you overwrite an allocated block of memory.

Author: Doug Rogers

Replied: 01 December 2010 02:30 AM  
avatar
  • keres
  • Posted: 01 December 2010 03:41 AM

Is the overhead any different than using smaller pointers? I’m reading up on shared_ptr and it doesn’t seem ultra-helpful, so I assume that it is going right over my head. Please know I am an optimization Nazi :) if something is harder to use, but faster, I’d rather use it.

You suggest that replacing all my ~15 or so pointers with shared_ptr will fix this memory leak? Something like:

shared_ptr<foofoo(new foo);

...and I wouldn’t have to call delete foo;, the program would automagically delete it?

Hopefully I am not overstaying my place in these forums by asking this. Your help is greatly appreciated, sir! :) Please excuse my ignorance on this subject…



Replies: 1
/userdata/avatar/3yvwf23us_doug100x100.png

I have found it to be ultra helpful.  Memory leaks are much easier to fix.
The over head is quite small as far as I can see.  This pointer class is taken from boost, so you can see the actual implementation there, if you like.

>You suggest that replacing all my ~15 or so pointers with shared_ptr will fix this memory leak?
It’s not going to hurt and it may fix it or help you find out where it is.  It may not even be a leak.

These are in a header:

// destructor templates for smart_ptr         
template <class _Type>
class 
releaser 

public:

  
void operator()(_Type *p
  

    p
->Release(); 
  

}




template <class _Type>
class 
deleter

public:
  
void operator()(_Type *p
  

    delete p
;
  

}



#define smart_ptr std::tr1::shared_ptr

I define the pointer like this:

// class I want to allocate
class FBXMesh
{
   
...
};

typedef smart_ptr<FBXMeshspFBXMesh;

Then the allocation is:

spFBXMesh                spMesh(new FBXMeshdeleter<FBXMesh>());  // pointer definition

// or
  
spFBXMesh                spMesh;
   
// reassign a pointer (also frees up a previous assignment
  
spMesh.reset(new FBXMeshdeleter<FBXMesh>());  // pointer definition

When spMesh goes out of scope, the deleter function is called.

Author: Doug Rogers

Replied: 01 December 2010 04:36 AM  
avatar

Intel makes some very good tools for tracking down issues like this, too.

You can get evaluation copies here:
http://software.intel.com/en-us/articles/intel-software-evaluation-center/

Specifically Inspector:
http://software.intel.com/en-us/articles/tools-for-memory-checking/



Replies: 0
avatar
  • keres
  • Posted: 01 December 2010 04:51 AM

Honestly, I’m trying to avoid boost altogether. In a desktop application it might be nice, but I have yet to see a game that uses it. Many of the guys I talk with that work on other games haven’t even touched it. I think that Windows has a shared_ptr that isn’t in boost, so I will try that when I get home today.



Replies: 1
/userdata/avatar/3yvwf23us_doug100x100.png

It was just developed under boost, it is now part of tr1 in Visual Studio. 
I was merely saying that if you wanted to see an implementation, you could look at boost.

For VS2008, you will need to add the feature pack to get it.  It is incorporated directly into VS2010.

http://www.microsoft.com/downloa...8B326C48E7&displaylang=en
You also get unordered sets and maps which can make look ups O(1) not O(log(n))

Author: Doug Rogers

Replied: 01 December 2010 04:52 AM  
2 pages: 1.2 last