The increasing complexity of the aperture geometry of the future space- and ground based-telescopes will limit the performance of the next generation of coronagraphic instruments for high contrast imaging of exoplanets. We propose here a new closed-loop optimization technique using two deformable mirrors to correct for the effects of complex apertures on coronagraph performance, alternative to the ACAD technique previously developed by our group. This technique, ACAD-OSM, allows the use of any coronagraphs designed for continuous apertures, with complex, segmented, apertures, maintaining high performance in contrast and throughput. We show the capabilities of this technique on several pupil geometries (segmented LUVOIR type aperture, WFIRST, ELTs) for which we obtained high contrast levels with several deformable mirror setups (size, number of actuators, separation between them), coronagraphs (apodized pupil Lyot and vortex coronagraphs) and spectral bandwidths, which will help us present recommendations for future coronagraphic instruments. We show that this active technique handles, without any revision to the algorithm, changing or unknown optical aberrations or discontinuities in the pupil, including optical design misalignments, missing segments and phase errors.