I see where the problem is.
you can't call dynamic_sidebar("mega menu") inside a function which is connected to add_filter('wp_nav_menu_items', 'your_custom_menu_item', 10, 2);
I recently wrote a Plugin having nearly the same issue.
You need to
function your_custom_menu_item($items, $args) {
if ($args->theme_location == 'primary') {
$items2=$items; //save $items to different variable
$items='';//empty items to latter fill them
// here you can write whatever you want to add
// in the style $items. = "<li>add something</li>"
foreach ($items2 as $item){
$link_page_id = (int)($item->object_id);
if ($current_id == $link_page_id){$cur=" current-menu-item current_page_item";}else{$cur="";
}
$items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page'.$cur.'"><a href="'.$item->url.'">'.$item->title.'</a></li>';
}
// write something here
// same like above
}else{
// what should happen if it is not primary? maybe the same without custom code.
}
}
The plugin where this is from is here http://wordpress.org/plugins/get-different-menus/ maybe playing around with it could help you to better understand how the menus work!