dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: [RESOLVED] Project run on Windows but on Linux getting segmentation fault (Mem access violation)

  1. #1
    Join Date
    Jul 2017
    Posts
    86

    [RESOLVED] Project run on Windows but on Linux getting segmentation fault (Mem access violation)

    Edited: Check my reply, I located the problem.

    Hello!

    This is my Project on GitHub. You can check it out, it's really small because I just started it. I also support a premake script if you don't use visual studio 2019.

    The problem is what I said in the title.

    I have two projects, VampEngine (Shared Library) and Application (ConsoleApp) which links against VampEngine and uses it. On both Windows and Linux everything compiles and link just fine
    (On windows with visual studio, on Linux with code blocks) but when I run the program in Linux it crashes. Of course, since it links in both OS's you can assume that I'm not exporting any c++ symbols.

    I would really appreciate it if you check my code (again it's very small) or if you can't, can you suggest me what might go wrong based on your experience?

    I believe it's not an actual bug in my code, but a Shared-Dynamic library problem. I'm creating shared libraries for windows most of the times and I don't really have too much experience in Linux.
    The only difference I know is use _declspec on windows (visual c++), on Linux use nothing.

    Also, check the macros which I use for symbol exportation:
    Code:
    #ifndef VAMP_ENGINE_API_H
    #define VAMP_ENGINE_API_H
    
    //Windows DLL export.
    #ifdef VAMP_PLATFORM_WINDOWS
    	#ifdef VAMP_BUILD_DLL
    		#define VAMP_API _declspec(dllexport)
    	#else
    		#define VAMP_API _declspec(dllimport)
    	#endif
    
    //Unix Systems.
    #else
    	#define VAMP_API
    #endif
    
    #endif
    Only in VampEngine/src/Core.h I export symbols:
    Code:
    #ifndef VAMP_ENGINE_CORE_H
    #define VAMP_ENGINE_CORE_H
    #include "VampEngine/API.h"
    #include <iostream>
    
    VAMP_API void* Vamp_Core_Constructor(const char *title, unsigned int width, unsigned int height);
    VAMP_API void  Vamp_Core_Deconstructor(void* obj);
    VAMP_API void  Vamp_Core_MainLoop(void *obj);
    
    namespace VampEngine
    {
    
    	class Window;
    
    	class Core
    	{
    
    	public:
    
    		Window* const window;
    
    	public:
    
    		Core(std::string title, unsigned int width, unsigned int height);
    
    		~Core();
    
    		void MainLoop();
    	};
    }
    #endif


    I you get confused with what I'm writing below just forget it

    Let me tell you quickly how the program runs:

    0) Application/Main.cpp(client) includes VampEngine plus an entry point (main function) which decleres VampEngine::Application *VampEngine::CreateApp
    1) Application/Main.cpp(client) Inherits VampEngine::Application which is a header only file.
    2) Application/Main.cpp(client) implements VampEngine::CreateApp which return an instance of (1).
    3) The entry point (client) calls VampEngine::CreateApp.
    4) The constructor of the base class (client) VampEngine::Application calls a c external function which create a VampEngine::Core object inside the dll.
    5) Then the entry point (client) is using the app object to call app->core->MainLoop() . Core lives in the dll.
    6) MainLoop() is implemented both in the dll and the client. In the client is just a wrapper which calls a c extern function which calls the MainLoop() of the dll.

    and this is how the app is starting

    Thanks
    Last edited by babaliaris; May 17th, 2019 at 05:27 PM.

  2. #2
    Join Date
    Jul 2017
    Posts
    86

    Re: Project run on Windows but on Linux getting segmentation fault (Mem access violat

    OMG I FOUND IT but I'm not sure how to solve it.

    The Core class has two implementations of the Core::MainLoop(). One is implemented inside the DLL and the other in the client. The client's implementation is just a wrapper which calls a c extern function (which lives in the dll) which actually calls the MainLoop() implemented inside the DLL.

    Now in visual c++, because I'm only exporting that c extern function, Core::MainLoop() acts in a polymorphic way. The client calls his implementation of Core::MainLoop() which calls the extern function and eventually the Core::MainLoop() inside the dll runs.

    On linux, I believe by default all the symbols are getting exported. And this is the behavior I saw using a debugger:

    The client calls Core::MainLoop() , this calls the extern function, the extern function calls again Core::MainLoop() but instead of running the dll implementation it actually runs the the clients implementation. It's like the clients Core::MainLoop() is calling itself over and over again!

    Do you know how I can solve this?

    Thank you.

    Check The behavior graphically:
    Name:  Untitled-Diagram.jpg
Views: 20
Size:  22.0 KB
    Name:  asd.jpg
Views: 21
Size:  24.7 KB
    Last edited by babaliaris; May 17th, 2019 at 06:55 PM.

  3. #3
    Join Date
    Jul 2017
    Posts
    86

    Re: [RESOLVED] Project run on Windows but on Linux getting segmentation fault (Mem ac


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)