Skip to main content
gamedev.net
🔒 Locked

Strange Exception

Started by phyl0men0 Dec 10, 2002 at 11:37 AM 5 replies 1k views
Original Post
phyl0men0
phyl0men0
Hi All Ok, here''s the problem. I''m working on a little engine and it is organized like this: The engine(Win32 project) starts and loads the app(DLL), which uses the engine. The App receives a handle to the engine and can begin to setup the engine. The app also has the ability to set the window title(through an std::string). And here occures the error. When I pass more than 15 chars in the string, an exception is thrown. Here''s an image of the error window which appears: If it helps, I could also upload the source code. Do you know what''s the problem? greetz phyl0men0
Evil Bill
Evil Bill
You''re using some funky implementation of std::string, where it has a char[16] buffer, so that it doesn''t need to allocate dynamic memory for short strings. For more than 15 chars, the string needs to allocate memory for its buffer. Now, what happens, is you''ll be doing string s = "Some string longer than 15 letters" in the EXE, and then passing it to your DLL. Your DLL will be doing something like s += " long". This means that the internal string buffer needs to be deleted and then realloced. But its being deleted from inside the DLL, when it was allocated from the EXE. You''ll need to either pass a const string& to the function, or not adjust the string in the DLL.

HTH

Member of the Unban Mindwipe Society (UMWS)
phyl0men0
phyl0men0
Thx for the quick reply, but the whole is organized a bit different.
I''ve declared a kind of a "settings" struct. I setup such a struct in the DLL and then pass it to the engine. The engine reads in that struct and initialize itself with the parameters passed through the settings struct.

I hope you understand what I tried to explain.

thx phyl0men0
Evil Bill
Evil Bill
Are you able to pass a constant settings object? Or do you need to modify the contents in some way?
You could try making the settings class have a char* instead of a string, that would imply that you shouldn''t append to it. I''m sure theres a better way - i just can''t think of it now.

Member of the Unban Mindwipe Society (UMWS)
phyl0men0
phyl0men0
Well, now I''m using an char* and it works well.
Thank you!

But I still wonder what exactly was the error?
When I pass a settings struct pointer to the engine, it also worked without an error. But when I tried to pass it as an normal or an constant parameter, it ended in the described error.

greetz phyl0men0
Evil Bill
Evil Bill
Ah, you were passing it by value? I assumed you''d be passing it by reference. Generally, you want to pass anything larger than the sizeof a pointer (4 bytes on most machine) by constant reference under C++ or by a constant pointer in C.
It''ll be because a copy of the original data is made when you pass the string by value. This copy is made in the EXE, before the function is called. Then, it''ll be getting destroyed when the function terminates (in the DLL). So you''re allocating in the EXE and freeing in the DLL.

HTH

Member of the Unban Mindwipe Society (UMWS)
phyl0men0
phyl0men0
Ok, thx a lot!!!

Now all works like it should!
I pass the settings struct as a reference and can use an std::string instead a char*.

Greetz phyl0men0

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.