I made a Replace fuction that works while I debug (Vs 2005 ) the compiled file closes due to some some

any help would be appreciated
this is my code
Code:
char* _replace(char* source_str,char* search_str,char* replace_str,char* ostr)
{
char *data = source_str;
char *orgdata = data;
int ser_sz = strlen(search_str);
int rep_sz = strlen(replace_str);
char _k = 0;
ostr = malloc(strlen(source_str));
ostr[0] = '\0'; /* remove garbage value */
if (rep_sz >= ser_sz){ _k = 1; }
do{
char *p = strstr(data,search_str); /* get the pointer to the first occurrence */
if (!p){
strcat(ostr,orgdata); /*append the rest*/
break;
}
if (!_k) {
realloc(ostr,(strlen(ostr) + rep_sz-ser_sz));
}
strncat(ostr,orgdata,p - orgdata); /*append the rest of the string*/
data = p + ser_sz; /*ignore the search term*/
strncat(ostr,replace_str,rep_sz); /* place the replace string*/
orgdata = p + ser_sz; /* save the current data*/
}while(1);
return ostr;
}