![]() |
shared library/dll problem
heya!
im having a bit of trouble with allocating memory across dll/so boundaries. how can i force all code in the application to use the same heap? hope someone can help? thanks! :D |
This is a classic problem on windows when you link with a dll which was itself linked against a different CRT, say maybe the DLL uses the multi-threaded runtime while your app uses single-threaded
If you compile the DLL yourself then just make sure you used the same runtime, if you only have the DLL in binary then you have to make your program use the same one. I'm not sure how you could go about forcing code the use the same heap. On windows for example the one process can have multiple heaps even if it's all using the same runtime. On the Unix side I really don't have the experience to say either way. At what level are you allocating and deallocating memory? I.e. raw APIs (say HeapAlloc on win) or C++ style with new and delete. |
hey Eoin!
thanks for the reply! mmm, well i dont have any experience with this either! my problem is ive written a plugin for maya (www.autodesk.com) and it uses alot of heap memory, well what happens is that is crashing very severly, and i eventually found out that the code is not in the same dll/so space, so not the same heap memory... what i assumed was that in order to prevent this i would have to make sure my code was allocating in the same heap as the maya? im using new and delete... i would really appreciate any further comments! :D PS: what i really dont understand is im actually compiling all the code into the plugin, theres no external libraries or stuff, just a plain dll/so |
Generally speaking the issue which arises with memory is attempting to allocate and then free memory with two incompatible methods. So with Win APIs you wouldn't use GlobalFree to free memory allocated with HeapAlloc. In C++ you wouldn't want to delete an array allocated with new[] say. (I'm typing these examples off my head so please excuse any brain farts.)
So the important thing you need to ensure is that Maya doesn't try to free any memory you've allocated. If it those do so there should be documentation detailing the correct way for your plugin to allocate that memory in the first place. I'd be surprised if it does this because of how many issues it could throw up. You mentioned about dll/so space, I can again only try and explain what happens on Windows- dlls are loaded into the same process space as the executable. So it should be safe for Maya code to read and write to memory your plugin has allocated up until your plugin frees that memory. Two possible causes your your issue might therefore be- 1) You allocate a block of memory small than Maya was expecting so it reads out of bounds. And 2) You free the memory before Maya was finished with it causing a read or write access error. |
heya!
thanks for the reply! ok i know, there are no new/delete mismatches (as far as i know), the problem occurs in this simple example: :
class Parser {the result is a seg fault... its clearly nothing im doing wrong in my class code, heres a reply i got from the API boards Quote:
hope you can help! thanks! :D |
I've never really liked the blanket statement that multiple inheritance is a bad thing. But if you never think you'll refer to ParserTranslator from a Parser* then making it a member is probably a good idea.
On to the issue at hand, the problem probably lies with the parser class and what happens when it gets copied. Instead of me trying to crudely explain things I'll point you to this gem of an article, The Law of The Big Two, by two excellent authors. If after reading that and giving things a try you still have problems post your new code and I'll take a look. As a p.s. Matthew Wilson's book, Imperfect C++, is an excellent read. |
thanks dude! that looks like a really interesting read! gonna read it at home, actually busy with a vfx job at the moment! will get back as soon as i read it!
:D |
heya!
well i read the article, im not doing anything wrong with regard to that, its specifically a dll/so problem, unless im wrong? |
Well are you absolutely sure that the Parser class never gets copied? As it stands in the code sample you posted it is not safe to copy so you should at least disable copying as the article suggests
:
class SelfishBeastieBetter yet use a boost::shared_ptr to manage that int*, then just as the article mentions you can forget about the destructor. |
hey Eoin! yeah you were right, i was forgetting to explictly set the copy constructor/assignment but that wasnt the problem, a real valid point though! thanks for pointing out!
as you can see the following code :
class Parser {and :
class Parser {both cause the same crash as before... buggery bollocks! im busy reading up stuff on how libraries work on linux, how to compile/create etc maybe that will give me an idea... any suggestions? any responses would be greatly appreciated! thanks :D |
| All times are GMT -5. The time now is 2:47 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC