4

Is it possible to set a custom allocator for OpenCV 2.3.1? I have a memory pool created and I want OpenCV to use that pool for what it needs.

Is that possible? If it is, how can it be done?

Updated:

I made some developments since last answer, but I still have some problems. This is the code I have now:

CvAllocFunc allocCV() 
{ 
     return (CvAllocFunc) MEMPOOL->POOLalloc(sz); 
}

CvFreeFunc deallocCV()
{
    return (CvFreeFunc) MEMPOOL->POOLfree(ptr);
}

...

cvSetMemoryManager(allocCV(),deallocCV(),data);

Now, my question is, how can I have access to the size and the pointer to the data I want to allocate and later to deallocate?

Just found out: The version of OpenCV I'm using (2.3.1) throws an error when using cvSetMemoryManager. The reason is in its source code:

void cvSetMemoryManager( CvAllocFunc, CvFreeFunc, void * )
{
    CV_Error( -1, "Custom memory allocator is not supported" );
}

I was very disappointed with this. I guess I can't use a custom memory pool with OpenCV anymore!

André
  • 75
  • 1
  • 6

1 Answers1

1

Right from the docs

http://opencv.willowgarage.com/documentation/c/core_utility_and_system_functions_and_macros.html#setmemorymanager

Use the

void cvSetMemoryManager(CvAllocFunc allocFunc=NULL, CvFreeFunc freeFunc=NULL, void* userdata=NULL)

function.

Your code

cvSetMemoryManager(allocCV(),deallocCV(),data);

is WRONG.

See what happens: you call allocCV and deallocCV functions (they return some pointer which is quietly converted to cvAllocFunc) !

And this is only the beginning. See the docs for alloc/dealloc semantics. You should write allocCV/deallocCV with correct signatures.

void* CV_CDECL allocCV(size_t size, void* userdata)
{
     return ((YourMemPoolTypeWhichIDoNotKnow*)userdata)->POOLalloc(size);
}

int CV_CDECL deallocCV(void* pptr, void* userdata)
{
    return ((YourMemPoolTypeWhichIDoNotKnow*)userdata)->POOLfree(pptr);
}

and then pass your MEMPOOL in the 'userdata' parameter:

cvSetMemoryManager(&allocCV, &deallocCV, (void*)MEMPOOL);

This is a standard way for a library to provide user callbacks.

Viktor Latypov
  • 14,289
  • 3
  • 40
  • 55
  • I tried it already but I couldn't get it to work. I don't know if i was doing something wrong, or if it is because of the opencv's version. I'm using OpenCV 2.3.1 and that function is from 2.1 and I didn't find an equivalent for my version – André May 22 '12 at 15:06
  • Ok, I tried the way above and it compiles with no errors, but at runtime, it throws an exception when it tries to run cvSetMemoryManager. Something like: First-chance exception at 0x7c... :\ any idea? – André May 22 '12 at 16:23
  • Many reasons can lead to this. You are using the OpenCV in a DLL ? Check if the DLL and your project have identical run-time libraries. – Viktor Latypov May 22 '12 at 16:39
  • The project I'm working on uses OpenCV and it works well, but when I try to execute cvSetMemoryManager it throws an error... Anyway, this might be a problem with my code. Thanks for your help – André May 22 '12 at 17:03
  • I'm looking at the code for 2.4.2, and found cvSetMemoryManager() is doing nothing except just throwing exception with string of "Custom memory allocator is not supported". Hmm.. seems maintainer(s) remove this feature from around 2.x. :( – Joonhwan Oct 19 '12 at 02:23