4

I have to split my application into several logical modules.

mainapp:

  • module1.so
  • module2.so
  • module3.so
  • and so on

Where each module is an *.so library, which will be loaded during runtime.

Each module shares the same interface and will return some array of data. For example:

int *ptr = module1->getIntData();

Is it OK, to free/delete this memory on mainapp side?

int *ptr = module1->getIntData();
delete ptr; //(or free(ptr))

What about a malloc/free implementations. Is it possible, that library will use another one then mainapp?

5
  • 1
    You are probably OK, however it seems that if you provide a createThing() method you should also provide a destroyThing() method for consistency, if nothing else.
    – trojanfoe
    Apr 5, 2016 at 7:51
  • Are you following any synchronisation mechanism? e.g. mutex or something alse.
    – someone
    Apr 5, 2016 at 7:51
  • 1
    Actually it's more a matter of convention than a technical question. That is, technically it's ok, but it's important to explicitly agree which side owns data allocated by library methods. Apr 5, 2016 at 7:52
  • 2
    Also I'd suggest to return something like std::unique_ptr, not raw pointers, because unique_ptr/shared_ptr allow to specify deleters upon object creation, thus simplifying memory management and making explicit remove functions excessive. Apr 5, 2016 at 7:58
  • The choice between unique_ptr, shared_ptr and weak_ptr depends on ownership policy. Apr 5, 2016 at 8:00

1 Answer 1

5

I would strongly recommend that the module which does the allocation is also responsible for doing the de-allocation. Thus:

int *ptr = module1->getIntData();
...
module1->freeIntData(ptr);

This allows different modules to use different allocators (malloc/free, new/delete, slab allocator, etc) without difficulty.

On Posix systems there can only be one implementation of malloc (and free) in a process, so if the definition of getIntData is "returns a pointer which must be free'd by free" then you would be alright. On the other hand, I think it would be possible to write two C++ compilers which could be used to write module1 and module2, but which couldn't delete memory allocated by the other's new. (Although I don't think such compilers currently exist).

If there is the remotest glimmer of a chance you might ever have to port this lot to Windows, then you really want the modules to deallocate the memory they allocated. Different DLLs can have different heaps and all manner of fun problems can ensue. (As @trojanfoe says in comments: Just the difference between debug and release builds can be enough to cause grief.)

I would only recommend using std::unique_ptr if you can guarantee that all the modules are always going to be built with the same version of the same compiler using identical compiler flags. (I am a strong believer in keeping dynamic library interfaces as simple and C-like as possible.)

4
  • "ensure" should be "ensue". Also, if my memory serves me, it only takes a Windows DLL to be built in debug mode and the executable to be built in release mode to demonstrate the issue you are solving with your answer.
    – trojanfoe
    Apr 5, 2016 at 8:13
  • std::unique_ptr could be nice, as I can pass custom (library) deallocator to it.
    – Dejwi
    Apr 5, 2016 at 8:17
  • @trojanfoe: Ha! Nice freudian slip :-) Apr 5, 2016 at 8:23
  • @Dejwi: I wouldn't trust compilers/libraries to make std::unique_ptr have the same ABI in debug and release builds. Apr 5, 2016 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.