# AddObserver on DataObject

**URL:** <https://discourse.itk.org/t/addobserver-on-dataobject/3275>\
**Category:** Beginner Questions\
**Tags:** dataobject, observer, event\
**Created:** [July 9, 2020, 8:17pm UTC](https://discourse.itk.org/t/addobserver-on-dataobject/3275 "2020-07-09T20:17:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Come](https://discourse.itk.org/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@Come](https://discourse.itk.org/u/Come)\
**Post date:** [July 9, 2020, 8:17pm UTC](https://discourse.itk.org/t/addobserver-on-dataobject/3275/1 "2020-07-09T20:17:04Z")

</div>

Hi,

I want to add an observer to an image to know when it is modified. I tried the code below based on this [example](https://itk.org/ITKExamples/src/Core/Common/ObserveAnEvent/Documentation.html).

```auto
#include <itkImage.h>
#include <itkRandomImageSource.h>
#include <itkCommand.h>

class MyCommand : public itk::Command
{
public:
	itkNewMacro(MyCommand);

	void
		Execute(itk::Object * caller, const itk::EventObject & event) override
	{
		Execute((const itk::Object *)caller, event);
	}

	void
		Execute(const itk::Object * caller, const itk::EventObject & event) override
	{
		std::cout << "Event happened" << std::endl;
	}
};

int main(int, char *[])
{
	using ImageType = itk::Image<unsigned char, 3>;
	ImageType::Pointer image = ImageType::New();

	MyCommand::Pointer myCommand = MyCommand::New();
	image->AddObserver(itk::ModifiedEvent(), myCommand);

	ImageType::IndexType start;
	start.Fill(0);

	ImageType::SizeType size;
	size.Fill(100);

	ImageType::RegionType region;
	region.SetSize(size);
	region.SetIndex(start);

	image->SetRegions(region);
	image->Allocate();

	//using RandomImageSourceType = itk::RandomImageSource<ImageType>;
	//RandomImageSourceType::Pointer randomImageSource = RandomImageSourceType::New();
	//randomImageSource->SetNumberOfWorkUnits(1);
	//randomImageSource->SetSize(size);
	//randomImageSource->Update();
	//image = randomImageSource->GetOutput();

	return EXIT_SUCCESS;
}

```

The output of this code prints twice `Event happened` but I don’t understand why. And if I uncomment the `RandomImageSource` part I have the same output as before.

I think the problem comes from the fact that it is not the good event I am observing but when does this event triggers then? Also, I found standard `EventObjects` in the `itkEventObject.h` file and from what I understand, the events are declared using the `itkEventMacroDeclaration` but who is invoking those events (for example who is invoking the `ModifiedEvent`).

Thank you for your help!  
Côme

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [July 9, 2020, 8:31pm UTC](https://discourse.itk.org/t/addobserver-on-dataobject/3275/2 "2020-07-09T20:31:50Z")

</div>

Hello,

I would recommend building ITK and your code in debug mode. Then launch your debugger and set a break point at the Execute method of your command. When the code is executed it’ll stop at the command’s Execute and you will be able to see the call stack in the debug to answer your questions.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [July 10, 2020, 3:42pm UTC](https://discourse.itk.org/t/addobserver-on-dataobject/3275/3 "2020-07-10T15:42:45Z")

</div>

> [@Come](#):
>
> ```auto
> image->SetRegions(region);
> image->Allocate();
> 
> ```

I think these two calls invoke `modified` event. `randomImageSource->Update();` does not need to update or otherwise modify the `image` because `image` is already up to date.

---

<div class="post-metadata">

**Author:** ![Come](https://discourse.itk.org/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@Come](https://discourse.itk.org/u/Come)\
**Post date:** [July 10, 2020, 5:51pm UTC](https://discourse.itk.org/t/addobserver-on-dataobject/3275/4 "2020-07-10T17:51:49Z")

</div>

Yes, I found the functions that invoke the event using the debugger (thanks @blowekamp). It comes from the `itk::ImageBase` when `SetBufferedRegion` and `SetLargestPossibleRegion` are called (when using `image->SetRegion(region)`) which then run the `Modified` method that invokes the envent `ModifiedEvent`.

That helped me a lot, thank you very much for your answers!!
