{"action":"opened","issue":{"url":"https://api.github.com/repos/mshabunin/test2/issues/135","labels_url":"https://api.github.com/repos/mshabunin/test2/issues/135/labels{/name}","comments_url":"https://api.github.com/repos/mshabunin/test2/issues/135/comments","events_url":"https://api.github.com/repos/mshabunin/test2/issues/135/events","html_url":"https://github.com/mshabunin/test2/issues/135","id":95434893,"number":135,"title":"FLANN Index params should be copied to avoid dangling pointer","user":{"login":"mshabunin","id":3304494,"avatar_url":"https://avatars.githubusercontent.com/u/3304494?v=3","gravatar_id":"","url":"https://api.github.com/users/mshabunin","html_url":"https://github.com/mshabunin","followers_url":"https://api.github.com/users/mshabunin/followers","following_url":"https://api.github.com/users/mshabunin/following{/other_user}","gists_url":"https://api.github.com/users/mshabunin/gists{/gist_id}","starred_url":"https://api.github.com/users/mshabunin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mshabunin/subscriptions","organizations_url":"https://api.github.com/users/mshabunin/orgs","repos_url":"https://api.github.com/users/mshabunin/repos","events_url":"https://api.github.com/users/mshabunin/events{/privacy}","received_events_url":"https://api.github.com/users/mshabunin/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/mshabunin/test2/labels/auto-transferred","name":"auto-transferred","color":"ededed"},{"url":"https://api.github.com/repos/mshabunin/test2/labels/bug","name":"bug","color":"fc2929"},{"url":"https://api.github.com/repos/mshabunin/test2/labels/category:%20documentation","name":"category: documentation","color":"ededed"},{"url":"https://api.github.com/repos/mshabunin/test2/labels/priority:%20normal","name":"priority: normal","color":"ededed"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-07-16T13:37:52Z","updated_at":"2015-07-16T13:37:52Z","closed_at":null,"body":"Transferred from http://code.opencv.org/issues/2050\r\n```\n|| Marius Shekow on 2012-06-14 20:23\n|| Priority: Normal\n|| Affected: None\n|| Category: documentation\n|| Tracker: Bug\n|| Difficulty: None\n|| PR: None\n|| Platform: None / None\n```\n\nFLANN Index params should be copied to avoid dangling pointer\n-----------\n```\nHi,\r\n\r\nin my program I was creating a flann index in one method (assigned to a member variable of type cv::flann::Index), let's say method* A()*, so that I can use it for searching in another method, say *B()*. Example:\r\n<pre>\r\nflannIndex = cv::flann::Index(myData32f, cv::flann::KDTreeIndexParams());\r\n</pre>\r\n\r\nHowever, searching for the k-NN in B() would cause an exception, because the index parameters are deleted at the point when A() exits. I.e.\r\n<pre>\r\ncv::flann::IndexParams::~IndexParams()\r\n{\r\n    delete &get_params(*this);\r\n}\r\n</pre>\r\nis called, which causes a dangling pointer at:\r\n<pre>\r\nvoid runKnnSearch_(void* index, const Mat& query, Mat& indices, Mat& dists,\r\n                  int knn, const SearchParams& params)\r\n{\r\n   [some code left out]\r\n    ((IndexType*)index)->knnSearch(_query, _indices, _dists, knn,\r\n                                  * (const ::cvflann::SearchParams&)get_params(params)*);\r\n}\r\n</pre>\r\n\r\nThe current way the FLANN wrapper is coded forces one to either create and use the index in the same method, or to create the index parameter at a safe place (e.g. member variable of your class) such that it is not deleted prematurely. It would be better instead, if the CTOR of cv::flann::Index actually copies the parameter!\n```\n\nHistory\n-------\n##### Kirill Kornyakov on 2012-06-15 07:12\n```\nBTW, are you sure that this is an OpenCV bug, and not the FLANN itself? In the latter case you should contact original authors of the library...\n-   Category set to flann\n```\n##### Marius Shekow on 2012-06-16 07:56\n```\nWell, the get_params() function essentially converts the cv:flann:*IndexParams to the flann-native index-parameters object. The deletion of the cv:flann:*IndexParams object when getting out of scope causes the dangling pointer, so I'd reckon it's a OpenCV problem.\r\n\r\nAlso, I'd like to note that it really seems to be only possible to define the cv::flann::Index object in one method but use it in another, if:\r\n* You create the cv::flann::Index on the heap, not the stack\r\n* The cv::Mat data that is indexed is available throughout the lifetime (again, why does the wrapper not internally store the cv::Mat object somewhere such that this is not necessary?)\r\n* The IndexParam object is permanently available (as already noted)\n```\n##### Vadim Kantorov on 2012-08-01 12:37\n```\nMarius Shekow has a seemingly complete description.\r\n\r\nI just bumped into this bug. Sth like:\r\n\r\n@Mat features(...);\r\ncv::flann::KDTreeIndexParams indexParams(5);\r\ncv::flann::Index index(features, indexParams);\r\nindex.knnSearch(...);\r\n@\r\nworks. While\r\n\r\ncv::flann::Index BuildIndex(Mat features)\r\n{\r\n    cv::flann::KDTreeIndexParams indexParams(5);\r\n    cv::flann::Index index(features, indexParams);\r\n    return index;\r\n\r\n}\r\n\r\n@Mat features(...);\r\ncv::flann::Index index = BuildIndex(features);\r\nindex.knnSearch(...);\r\n@\r\ndoes not because a good copy constructor of cv::flann is not implemented. It would be better to have it somewhere very clear in the documentation, or somehow to notify the user when they try to copy the Index object.\n```\n##### Andrey Kamaev on 2012-08-15 13:26\n```\n-   Assignee set to Vadim Pisarevsky\n```\n##### Andrey Kamaev on 2012-11-23 10:51\n```\n-   Category changed from flann to documentation\n```\n##### Vadim Kantorov on 2012-11-26 11:08\n```\nI guess it would be much better to provide a better wrapper interface for FLANN which will hold a reference to the \"features\" matrix (via a Mat object, not just a pointer to float array) and to the \"params\" object (via a shared ptr). The current behavior is confusing and fixing documentation should only be a partial solution from my point of view.\n```"}}