# SF2SearchEx Access Violation when passing in image co-ordinates

**URL:** https://forum.commonvisionblox.com/t/sf2searchex-access-violation-when-passing-in-image-co-ordinates/1468
**Category:** Programming Questions
**Created:** [August 5, 2021, 10:49am UTC](https://forum.commonvisionblox.com/t/sf2searchex-access-violation-when-passing-in-image-co-ordinates/1468 "2021-08-05T10:49:08Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Austin](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@Austin](https://forum.commonvisionblox.com/u/Austin)
#### Post date: [August 5, 2021, 10:49am UTC](https://forum.commonvisionblox.com/t/sf2searchex-access-violation-when-passing-in-image-co-ordinates/1468/1 "2021-08-05T10:49:08Z")

</div>

Hello,

We have been having random crashes when running the SF2SearchEx function. We use this for character recognition so we loop around this function x10 passing in each character model. We know the area of where these characters are located so we set the (Left, Top, Right, Bottom) image co-ordinates.

Although not the actual code we use on our system I have managed to make a quick test function to prove the fault. All the models are the same and the image is also the same each time I have made it so the search area changes each time but I can get it to happen very quickly on a fixed area.

See below the function I have created to force this fault using these fixed Co-Ordinate variables,

I am using Windows 10, Stemmer Version V13.04.000 (32 bit).

It can happen after a couple of cycles.

Model’s and Image Link:  
[SF Models](https://ims.torus-group.com/downloads/SFModels.zip)

```auto
SF sf[10];
IMG sfIMG;
CString Filepath

Filepath.Format(_T("C:\\TMS\\SFModels\\Test.png"));
LoadImageFile(CT2A(Filepath), sfIMG);

for(int Item = 0; Item < 10; Item++)
{
  Filepath.Format(_T("C:\\TMS\\SFModels\\%d.sf2"), Item);
   LoadSF(sf[Item], CT2A(Filepath));
}

cvbdim_t X1 = 550;
cvbdim_t Y1 = 137;
cvbdim_t X2 = 863;	
cvbdim_t Y2 = 300;

TSFSolution solution;
int CycleCount = 0

// Search on the through all the models in the system
do
{
  for(int Item = 0; Item < 10; Item++)
  {
    if(IsSF2(sf[Item]))
    {
      PIXELLIST solutions = NULL;
				
      // Fix value search each time
      if(SF2SearchEx(sf[Item], sfIMG, 0, X1, Y1, X2, Y2, SP_TolerantModel, solutions))
      {
        if(PixelListCount(solutions))
        {
          for(int k = 0; k < PixelListCount(solutions); k++)
            ListPixelEx(solutions, k, (double*)&solution);
        }
      }
      ReleaseObject(solutions);
    }
  }
  CycleCount++;
}	while(true);

```

---

<div class="post-metadata">

### Author: ![Austin](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@Austin](https://forum.commonvisionblox.com/u/Austin)
#### Post date: [August 5, 2021, 2:24pm UTC](https://forum.commonvisionblox.com/t/sf2searchex-access-violation-when-passing-in-image-co-ordinates/1468/2 "2021-08-05T14:24:16Z")

</div>

I can pinpoint exactly how to get this fault

When create a array of SF objects. (In this case 10) and pass in image Co-ordinates you get a access violation:

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/56a8e96ebc3e4f2c9311f2ec3091a5e9be1a66ce.png)

The other test I have run (over 100,000 cycles each) and this fault does not occur are

1. Single SF object maximum image co-ordinates
2. Single SF object and apply random image co-ordinates each time (With the image size)
3. Array of SF Objects and maximum image co-ordinates

I can provide code for a simple command line app that I have been running this test on.

I am initialising the array as follows:

```auto
SF sf[10]

for (int Item = 0; Item < 10; Item++)
  LoadSF(sf[Item], Format("C:\\sfTest\\%d.sf2", Item));

```

---

<div class="post-metadata">

### Author: ![illusive](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/illusive/32/61_2.png) [@illusive](https://forum.commonvisionblox.com/u/illusive)
#### Post date: [August 6, 2021, 11:31am UTC](https://forum.commonvisionblox.com/t/sf2searchex-access-violation-when-passing-in-image-co-ordinates/1468/3 "2021-08-06T11:31:59Z")

</div>

Hi @Austin

Thank you very much for the concise description, that was very helpful! I can confirm what you describe and I already have an idea what the reason is. If my assumptions turn out to be correct then I think I’ll be able to provide a fix early next week.

---

<div class="post-metadata">

### Author: ![illusive](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/illusive/32/61_2.png) [@illusive](https://forum.commonvisionblox.com/u/illusive)
#### Post date: [August 6, 2021, 3:49pm UTC](https://forum.commonvisionblox.com/t/sf2searchex-access-violation-when-passing-in-image-co-ordinates/1468/4 "2021-08-06T15:49:56Z")

</div>

Hi @Austin

I will have a new build for you on Monday. The problem is now understood and has to do with the fact that the AOI is extending beyond the bottom edge of the image (image height is 300 - highest acceptable value for bottom is therefore 299) and will only occur if the search is limited to an AOI (as opposed to searching in the entire image). Of course this kind of situation must be handled gracefully, but due to a misconception in the creation of the internally used accumulator and a rounding effect the access does in fact touch unallocated memory, which gives rise to the access violation you saw.

Luckily the fix was not too complicated (correct the malformed generation of the accumulator so that it is robust versus the aforementioned rounding effect). As soon as the fixed version has passed the build and test farm I’ll post it here.

---

<div class="post-metadata">

### Author: ![illusive](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/illusive/32/61_2.png) [@illusive](https://forum.commonvisionblox.com/u/illusive)
#### Post date: [August 9, 2021, 6:28am UTC](https://forum.commonvisionblox.com/t/sf2searchex-access-violation-when-passing-in-image-co-ordinates/1468/5 "2021-08-09T06:28:52Z")

</div>

Here we go:

- [SF\_3.0.9.465.zip (win32)](https://forum.commonvisionblox.com/uploads/short-url/cGeg6uh5rKPUyd7d3KeSXiHlTGj.zip) (213.1 KB)
- MD5 for SF.dll from zip file: 399467cda8ff6d2c2e96d40f58c271c3

This should fix the problem you’ve reported.

---

<div class="post-metadata">

### Author: ![Austin](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@Austin](https://forum.commonvisionblox.com/u/Austin)
#### Post date: [August 9, 2021, 11:18am UTC](https://forum.commonvisionblox.com/t/sf2searchex-access-violation-when-passing-in-image-co-ordinates/1468/6 "2021-08-09T11:18:00Z")

</div>

@illusive

Thanks for the quick update on this, I have ran it through thousands of cycles on the test app I create and it has not faulted.

We are using this update on version 13.03.002 due to the problems with different user accounts.

[https://forum.commonvisionblox.com/t/problems-opening-genicam-vin-driver-in-different-user-accounts/1454?u=austin](https://forum.commonvisionblox.com/t/problems-opening-genicam-vin-driver-in-different-user-accounts/1454)

---

<div class="post-metadata">

### Author: ![illusive](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/illusive/32/61_2.png) [@illusive](https://forum.commonvisionblox.com/u/illusive)
#### Post date: [August 9, 2021, 1:06pm UTC](https://forum.commonvisionblox.com/t/sf2searchex-access-violation-when-passing-in-image-co-ordinates/1468/7 "2021-08-09T13:06:15Z")

</div>

Thanks @Austin for the feedback. The SF.dll I posted will work with practially any CVB version down to 13.0.
